Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetProperty("pname") returns null

Tags:

I want to get value for a dynamic property of a dynamic object. Here is my Code..

public string ReturnProperty(object ob, string prop) {                 Type type = ob.GetType();     PropertyInfo pr = type.GetProperty(prop);      //Here pr is null..Dont know whats wrong      return pr.GetValue(ob, null).ToString(); } 
like image 981
incomplete Avatar asked Feb 22 '12 09:02

incomplete


1 Answers

My guess is that either it isn't a public property, or you've got the name wrong, or it isn't a property at all (but a public field).

It's impossible to say more without knowing what the actual type is, but that should be a start.

You mention that this is a "dynamic object" but that's not really very descriptive. Bear in mind that the CLR itself doesn't know anything about the DLR - if you mean this is a type which implements IDynamicMetaObjectProvider or extends DynamicObject, then you won't be able to get at the properties with "normal" reflection like this.

like image 156
Jon Skeet Avatar answered Sep 24 '22 01:09

Jon Skeet