-2

I have a below Object

public class ResTemplate
{
    public string country { get; set; }
    public int jobCode { get; set; }
    public Attributes attributes { get; set; }
}

public class Attributes
{
    public string state { get; set; }
    public string region { get; set; }
}

now i want to convert this to data table without using property name to read the values

output expected

country  jobcode  state   region
US       001      IL      Chicago
SSK
  • 783
  • 3
  • 18
  • 42
  • 1
    _"without specifying Attributes anywhere"_ -- meaning what, exactly? Why don't you want to specify "Attributes"? Do you mean just the type, or the field name as well? Why do you want to use reflection at all? It's _way_ slower, and usually there are better ways to accomplish your goal, _whatever_ that is. See [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You will get better answers if you ask the question regarding your broader goal, instead of this one. – Peter Duniho Mar 09 '17 at 07:52
  • the thing is i want to make it more dynamic, instead of specifying the property name, if it can be read by indexes – SSK Mar 09 '17 at 08:21
  • _"read by indexes"_ -- meaning what, exactly? I'm afraid you are not expressing your question clearly. You need to show more context and explain why using reflection is an acceptable tradeoff to you. You can certainly walk the object tree looking for any property of the given name, but how is this useful? How would you use it? Please provide more detail. – Peter Duniho Mar 09 '17 at 08:26
  • And what do you want to happen if, for example, both `ResTemplate` and `Attributes` have a property with the same name? Your example doesn't suffer from this, but you're asking for a general solution, and a general solution needs to work, well...generally. In the meantime, see posts like http://stackoverflow.com/questions/33322132/recursively-find-object-property-via-reflection and http://stackoverflow.com/questions/6547113/how-to-loop-through-class-properties-tree, which may address your question or at least point you in the right direction. – Peter Duniho Mar 09 '17 at 09:06
  • Basically i want to convert a given nested object to Datatable, without writing much code and putting in the property names to access their values – SSK Mar 09 '17 at 10:09

2 Answers2

0

Why not try

myObj.GetType().GetProperty("attributes ").GetType().GetProperty("state") 

and then just process the value.

You can also use

PropertyInfo[] properties = myObj.GetType().GetProperties();

Then run a for-loop and access the other properties and their value using: roperties[i];

  • I have already said i dont want to use the property name "attribute" here. Since i want it to make dynamic – SSK Mar 09 '17 at 08:22
  • i tried it, but it doesn't work. properties[0].Name returns "Capacity" – SSK Mar 09 '17 at 11:20
0

stateValue will give you the desire result.

var objectValue =  myObj.GetType().GetProperty("attributes").GetValue(myObj, null);
var stateValue = objectValue.GetType().GetProperty("state").GetValue(objectValue, null);
Venu prasad H S
  • 231
  • 1
  • 8
  • I have already said i dont want to use the property name "attribute" here. Since i want it to make dynamic – SSK Mar 09 '17 at 08:22
  • can you explain how you want to select the values without passing the property names. I'm afraid you did not complete the question. – Venu prasad H S Mar 10 '17 at 07:24