First of all, sorry for my poor English ... I hope you understand what I want to say.
I have a problem with a little code where I need to get the value of class properties. (This is not my complete project, but the concept of what I want to do. And with this simple code, I'm locked out.)
There is a code: (This sample works correctly.)
using System;
using System.Reflection;
class Example
{
public static void Main()
{
test Group = new test();
BindingFlags bindingFlags = BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.Static;
Group.sub.a = "allo";
Group.sub.b = "lol";
foreach (PropertyInfo property in Group.GetType().GetField("sub").FieldType.GetProperties(bindingFlags))
{
string strName = property.Name;
Console.WriteLine(strName + " = " + property.GetValue(Group.sub, null).ToString());
Console.WriteLine("---------------");
}
}
}
public class test
{
public test2 sub = new test2();
}
public class test2
{
public string a { get; set; }
public string b { get; set; }
}
But I want to replace Group.subwith dynamic access (for example, foreachfrom GetField(Var)where it works). I tried many combinations, but I did not find any solutions.
property.GetValue(property.DeclaringType, null)
or
property.GetValue(Group.GetType().GetField("sub"), null)
or
property.GetValue(Group.GetType().GetField("sub").FieldType, null)
So, I think you understand. I would like to dynamically specify an instance of an object Group.sub. Because in my complete project I have many subclasses.
Any ideas?