开发者

How can get untyped object's values?

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


  1. 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;
    
  2. 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);
    
  3. If you don't know the name of the property, use Type.GetProperties to get an array of all the properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜