I want to get an object typed properties values. Here is my code:
Type tip = Type.GetType(pair.Key.GetType().ToString());
object uretilenNesne = Activator.CreateInstance(tip);
uretilenNesne has correct type but I want to access uretilenNesne's properties values. Do you have开发者_StackOverflow any idea?
KR,
Dakmaz
Do you know the name of the property you want to access at compile time? If yes, then you can use the
dynamic
data type:Type tip = Type.GetType(pair.Key.GetType().ToString()); dynamic uretilenNesne = Activator.CreateInstance(tip); var x = uretilenNesne.someProperty;
If you know the name of the property at run time, you can use reflection: Type.GetProperty will return a property with a given signature that can be accessed with PropertyInfo.GetValue or SetValue. Example:
Type tip = Type.GetType(pair.Key.GetType().ToString()); object uretilenNesne = Activator.CreateInstance(tip); PropertyInfo pinfo = tip.GetProperty("someProperty"); object x = pinfo.GetValue(uretilenNesne, null);
If you don't know the name of the property, use Type.GetProperties to get an array of all the properties.
精彩评论