在C#中记录对象的所有属性.如何记录内部对象的属性呢? [英] logging all properties of an object in c#. how to log inner object properties as well?

查看:53
本文介绍了在C#中记录对象的所有属性.如何记录内部对象的属性呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试(1)记录对象的所有属性,以及(2)内特定对象类型的所有属性.我可以做(1),但不能做(2).

现在就是这种情况.

  foreach(TypeDescriptor.GetProperties(object1)中的PropertyDescriptor描述符){字符串名称=描述符.名称;对象值=描述符.GetValue(object1);logger.Debug(String.Format("{0} = {1}",名称,值));} 

我需要的是类似的东西

  foreach(TypeDescriptor.GetProperties(object1)中的PropertyDescriptor描述符){字符串名称=描述符.名称;对象值=描述符.GetValue(object1);logger.Debug(String.Format("{0} = {1}",名称,值));//TODO检查object1的当前属性是否为object2类型,如何?如果 (...) {//TODO对object2重复该过程foreach(TypeDescriptor.GetProperties(object2)中的PropertyDescriptor内部描述符){字符串innername = innerdescriptor.Name;对象innervalue = innerdescriptor.GetValue(object2);logger.Debug(String.Format("{0} = {1}",内部名称,内部值));}}//万一} 

但是,无论我尝试什么,这第二件事都不起作用.所以,请帮忙.

更新我对支票有一个答案(@Alex Art.)

  if(descriptor.PropertyType == typeof(您期望的类型)){...} 

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

解决方案

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

  public void LogProps(Object object1){var objType = object1.GetType();IList< PropertyInfo>属性=新列表< PropertyInfo>(objType.GetProperties());foreach(属性中的PropertyInfo属性){var propValue = prop.GetValue(object1,null);if(prop.PropertyType == typeof(yourTypeHere)){LogProps(propValue);}别的{logger.Debug(String.Format("{0} = {1}",prop.Name,propValue));}}} 

我在这里也使用了递归,如果您的层次结构比较长,也可能会出现问题

关于您的解决方案:

//TODO检查object1的当前属性是否为object2类型,怎么样?

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

 对象值=描述符.GetValue(object1);if(descriptor.PropertyType == typeof(您期望的类型)){foreach(TypeDescriptor.GetProperties(value)中的PropertyDescriptor内部描述符{字符串innername = innerdescriptor.Name;对象innervalue = innerdescriptor.GetValue(object2);logger.Debug(String.Format("{0} = {1}",内部名称,内部值));}}//万一 

i am trying to (1) log all properties of an object, and (2) all properties of a specific object type within. i can do the (1) but not (2).

this is the case now.

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

what i need is something like:

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
}

however, this second thing doesn't work no matter what i try. so, please help.

update i have an answer (by @Alex Art.) to the check

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

now the only thing that remains is the inner object properties logger!

解决方案

I think it can be achieved by using reflection (But you should be aware of performance penalty):

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));
       }
   }
}

I also used a recursion here which is also could be problematic if you have some long hierarchy

Regarding your solution:

// TODO check if the current property of object1 is of type object2, how?

Did you try using 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

这篇关于在C#中记录对象的所有属性.如何记录内部对象的属性呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆