在c#中记录对象的所有属性。如何记录内部对象属性?

时间:2014-12-06 11:02:48

标签: c# properties inner-classes

我试图(1)记录对象的所有属性,以及(2)特定对象类型的所有属性。我可以做(1)而不是(2)。

现在就是这种情况。

foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(object1))
{
     string name = descriptor.Name;
     object value = descriptor.GetValue(object1);
     logger.Debug(String.Format("{0} = {1}", name, value));
}

我需要的是:

foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(object1))
{
     string name = descriptor.Name;
     object value = descriptor.GetValue(object1);
     logger.Debug(String.Format("{0} = {1}", name, value));

     // TODO check if the current property of object1 is of type object2, how?
     if (...) {
     // TODO repeat the process for object2

     foreach (PropertyDescriptor innerdescriptor in TypeDescriptor.GetProperties(object2))
     {
          string innername = innerdescriptor.Name;
          object innervalue = innerdescriptor.GetValue(object2);
          logger.Debug(String.Format("     {0} = {1}", innername, innervalue));
     }

     } // end if
}
然而,无论我尝试什么,第二件事都行不通。所以,请帮忙。

更新 我有一个答案(@Alex Art。)给支票

if (descriptor.PropertyType == typeof(the type that you expecting) )  { ... }

现在唯一剩下的就是内部对象属性记录器!

2 个答案:

答案 0 :(得分:2)

我认为可以通过使用反射来实现(但你应该注意性能损失):

public void LogProps(Object object1)
{
   var objType = object1.GetType();

   IList<PropertyInfo> properties = new List<PropertyInfo>(objType.GetProperties());

   foreach (PropertyInfo prop in properties)
   {
       var propValue = prop.GetValue(object1, null);
       if(prop.PropertyType == typeof(yourTypeHere))
       {  
          LogProps(propValue);
       }
       else
       {           
           logger.Debug(String.Format("{0} = {1}", prop.Name, propValue));
       }
   }
}

我还在这里使用了一个递归,如果你有一个很长的层次结构也可能会有问题

关于您的解决方案:

  

// TODO检查object1的当前属性是否为object2类型,   如何?

您是否尝试使用PropertyDescriptor.PropertyType?:

 object value = descriptor.GetValue(object1);

 if (descriptor.PropertyType == typeof(the type that you expecting) ) 
 {

    foreach (PropertyDescriptor innerdescriptor in TypeDescriptor.GetProperties(value) 
    {
         string innername = innerdescriptor.Name;
         object innervalue = innerdescriptor.GetValue(object2);
         logger.Debug(String.Format("     {0} = {1}", innername, innervalue));
    }

 } // end if

答案 1 :(得分:1)

如果这是为了在运行时记录对象的状态,您可能会发现serialize the whole object as JSON更容易并存储它。与自定义解决方案相比,这具有人类可读性和灵活性的额外好处。

要忽略某些属性,请将IgnoreDataMemberAttribute应用于它们。

[DataContract]
public class MyObjectParent
{
    [DataMember]
    public int ImportantValue { get; set; }

    [DataMember]
    public MyObjectChild Child { get; set; }

    [IgnoreDataMember]
    public AnotherClassThatIWantToIgnore IgnoreMe { get; set; }
}

[DataContract]
public class MyObjectChild
{
    [DataMember]
    public string ImportantValue { get; set; }   
}

public string ObjectAsJson<T>(T obj) where T : class
{
    var serializer = new DataContractJsonSerializer(typeof(T));
    using (var stream = new MemoryStream())
    {
       serializer.WriteObject(stream, obj);
       return Encoding.Default.GetString(stream.ToArray());
    }
}