我試圖獲取對象的所有屬性及其值。 這裏我的代碼給我所有的價值爲我對象的「簡單」的屬性:獲取嵌套對象的屬性
foreach (var prop in dataItem.Value.GetType().GetProperties())
{
if (prop.Name == "CurrentSample")
{
//Doesn't work
var internProperties = prop.GetType().GetProperties();
foreach (var internProperty in internProperties)
{
System.Diagnostics.Debug.WriteLine("internProperty.Name + " : " + internProperty.GetValue(prop, null));
}
}
else
{
System.Diagnostics.Debug.WriteLine(prop.Name + " : "+ prop.GetValue(dataItem.Value, null));
}
}
我的問題是關於我的「CurrentSample」屬性,它包含自己的(時間戳和字符串)的2財產。 我無法找到檢索這些信息的方法。
我試圖應用相同的原則,但我根本得不到正確的信息。我可以通過使用一個簡單的dataItem.Value.CurrentSample.Value或dataItem.Value.CurrentSample.TimeStamp來訪問這些值,但想知道一個更正確的方法來使其工作。
現在,而不是打印我的時間戳和值與自己的價值,我得到屬性的大名單,我想的類屬性的所有屬性:
ReflectedType : MTConnectSharp.DataItem
MetadataToken : 385876007
Module : MTCSharp.dll
PropertyType : MTConnectSharp.DataItemSample
Attributes : None
CanRead : True
CanWrite : False
GetMethod : MTConnectSharp.DataItemSample get_CurrentSample()
SetMethod :
IsSpecialName : False
CustomAttributes : System.Collections.ObjectModel.ReadOnlyCollection`1[System.Reflection.CustomAttributeData]
這些屬性是受保護的/私人的嗎? –
不,他們都是公開的。 – Belterius
好的,那麼錯誤是什麼?你說這不起作用,但爲什麼? –