c# - Getting specific properties from an object child -


This question has been struggled to come in a decent way to ask / title, but I will try and I can do the best.

I am working with a data structure like this:

  public fu {public times {get; Set;}} Public Bar {public subtype A Type A {get; Set;} public subtype B type B {get; Set;} ...} public subtype {public perfection status {get; Set;} ...}  

Note that I can not change the data structure for it. There are many different types in the

bar category, all of which have different properties, but for all of them, the normal status .

What do I need to do, it has been given an object of type foo , each time the status of each item in the bar object is recorded SubType is not going to be charged every time, something can be empty.

I can use a recursive function to manage the kind of loop through everything like below. This is not ideal, although I do not think the loop can be quite big because there can be many properties on each SubType

  Private Zero GetProperties (type classType, Object example) {foreach (propertyInfo.GetProperties () in propertyInfo property} {object value = property.GetValue (example, empty); if (value! = Null) {if (property.Name == "status") {records ( Class type, value);} GetProperties (property.PropertyType, value);}}}  

Is this the only approach that is for such problem?

No. Admit: With the answer given by Selman 22, I have come up with another issue in which I am trying to create an unknown object which is based on the name of the situation and the object.

  Var z = instance.GetType () .GetProperties (). Select (x = & gt; new {status = x.GetValue (example) .GetType (). GetProperty ("status"). GetValue (x, null) , Name = x.Name}) .Olist ();  

It's throwing an error of object does not match the target type. When trying to retrieve the value. Is it possible in 1 liner?

type in category GetProperty (string name, bindflag method ) which you can use to retrieve a specific property. Instead of looping through each property, use this method instead.

  // MyClass type type object type myType = typeof (MyClass); // Get property information by passing the property name and specifying BindingFlags. PropertyInfo myPropInfo = myType.GetProperty ("MyProperty", Binding Flags. Public; BindingFlag.Instance);  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -