【问题标题】:Can get property value by ExpandProperty but not by direct access可以通过 ExpandProperty 获取属性值,但不能通过直接访问
【发布时间】:2018-09-28 01:31:01
【问题描述】:

我有一个 System.Data.DataSet 对象,该对象已被序列化,然后反序列化为 PSObject。

我正在尝试访问从表中标题为 Properties 的列中填充的属性。

虽然Get-Member 显示对象有一个名为Properties 的属性,并且我可以使用select -ExpandProperties 来获取该值,但我无法直接将其作为对象上的属性进行访问。

更新: 实际的 SQL 查询在服务器上运行,结果使用Export-CliXml 进行序列化并放置在可访问的共享中。使用Import-CliXml 对结果进行再水化,这会产生一个以Deserialized 为前缀的对象类型,如下面的Mötz 所述。对此的解释可以在here找到。

$> $res.Tables[0] | Get-Member

TypeName: Deserialized.System.Data.DataRow

Name       MemberType Definition
----       ---------- ----------
GetType    Method     type GetType()
ToString   Method     string ToString(), string ToString(string format, System.IFormatProvider formatProvider), string IFormattable.ToString(string format, System.IFormatProvider formatProvider)
Properties Property   System.String {get;set;}

$> $res.Tables[0].Properties
$> $res.Tables[0]."Properties"
$> $res.Tables[0] | select -ExpandProperty "Properties"
<object type= .... > .... </object>

【问题讨论】:

  • PowerShell 5.1 版

标签: powershell


【解决方案1】:

我们需要更多的帮助来帮助您。我们不知道您的数据源是什么样的、表结构和其他重要细节。

我刚刚针对我的一个数据库做了一个快速示例,结果看起来有点不同。

$SqlConnection = new-object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "data source=.;Initial catalog=db;Trusted_Connection=True;"

$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = "SELECT * FROM dbo.Table"
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand

$dataset = new-object System.Data.Dataset
$DataAdapter.Fill($dataset)

你的类型说:“Deserialized.System.Data.DataRow”,而我的类型说“System.Data.DataRow”。

PS C:\windows\system32> $dataset.Tables[0] | Get-Member


   TypeName: System.Data.DataRow

Name                      MemberType            Definition
----                      ----------            ----------
AcceptChanges             Method                void AcceptChanges()
BeginEdit                 Method                void BeginEdit()
CancelEdit                Method                void CancelEdit()
ClearErrors               Method                void ClearErrors()
Delete                    Method                void Delete()
EndEdit                   Method                void EndEdit()
Equals                    Method                bool Equals(System.Object obj)
GetChildRows              Method                System.Data.DataRow[] GetChildRows(string relationName), System.Data...
GetColumnError            Method                string GetColumnError(int columnIndex), string GetColumnError(string...
GetColumnsInError         Method                System.Data.DataColumn[] GetColumnsInError()
GetHashCode               Method                int GetHashCode()
GetParentRow              Method                System.Data.DataRow GetParentRow(string relationName), System.Data.D...
GetParentRows             Method                System.Data.DataRow[] GetParentRows(string relationName), System.Dat...
GetType                   Method                type GetType()
HasVersion                Method                bool HasVersion(System.Data.DataRowVersion version)
IsNull                    Method                bool IsNull(int columnIndex), bool IsNull(string columnName), bool I...
RejectChanges             Method                void RejectChanges()
SetAdded                  Method                void SetAdded()
SetColumnError            Method                void SetColumnError(int columnIndex, string error), void SetColumnEr...
SetModified               Method                void SetModified()
SetParentRow              Method                void SetParentRow(System.Data.DataRow parentRow), void SetParentRow(...
ToString                  Method                string ToString()
Item                      ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(s...
ACCOUNTTYPE               Property              int ACCOUNTTYPE {get;set;}
AUTOINFO                  Property              int AUTOINFO {get;set;}
AUTOLOGOFF                Property              int AUTOLOGOFF {get;set;}
AUTOUPDATE                Property              int AUTOUPDATE {get;set;}
CLIENTACCESSLOGLEVEL      Property              int CLIENTACCESSLOGLEVEL {get;set;}
COMPANY                   Property              string COMPANY {get;set;}
COMPILERWARNINGLEVEL      Property              int COMPILERWARNINGLEVEL {get;set;}
CONFIRMDELETE             Property              int CONFIRMDELETE {get;set;}
CONFIRMUPDATE             Property              int CONFIRMUPDATE {get;set;}
CREDENTIALRECID           Property              long CREDENTIALRECID {get;set;}
DEBUGGERPOPUP             Property              int DEBUGGERPOPUP {get;set;}
...                       ...                   ...

所以我可用的方法列表超出了您的列表。那是第一次。接下来是我的所有属性都映射到表中的一列。

所以我猜你需要分享更多关于如何将数据填充到数据集对象的代码,以便我们了解你面临的问题。

【讨论】:

  • 我想这对我来说应该不足为奇。序列化的原因是数据来源于生产服务器。我们有一个在服务器上执行代码的框架,它被序列化为一个文本文件,可以从我们的开发框访问。这些文件可以在本地重新水化,以便我们可以操纵结果。不幸的是,我很可能无法提供更多有用的信息(由于缺乏框架知识或适当性)。
  • 该框架是公知/可用的框架吗?你能与我们分享一个经过清理的文件以及你用来加载它的代码吗?
  • 不是,它实际上只是一些 PowerShell 编排。数据在服务器端使用 Export-CliXml 进行序列化,在客户端使用 Import-CliXml 进行反序列化。我做了一些挖掘,类型名称的差异是由于该过程。它更像是一个前缀而不是一个实际的命名空间,它表明它是一个部分对象。见:blogs.msdn.microsoft.com/powershell/2010/01/07/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-17
  • 2022-01-14
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多