Writing all properties of an object in C #. how to register properties of internal objects?

I am trying to (1) write all properties of an object and (2) all properties of a specific type of object inside. i can do (1) but not (2).

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

      

i need 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.) On the check

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

      

now the only thing that remains is the logger of the properties of internal objects!

+3


source to share


2 answers


I think this can be achieved with reflection (but you should be aware of the performance limitation):

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 recursion, which can also be problematic if you have a long hierarchy

As for your solution:



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

Have you tried 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

      

+2


source


If it is necessary to register the state of an object at runtime, you will probably find it easier to serialize the entire object as JSON and store that instead. This has the added benefit of being human readable and more flexible than a conventional solution.

To ignore certain properties, apply the IgnoreDataMemberAttribute to them.



i.e.

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

      

+1


source







All Articles