Danil
Danil

Reputation: 1893

how to get/set value from Com interop object using dynamic by property name string

I have following code:

Type type = Type.GetTypeFromProgID("interopTypeName");
dynamic obj = Activator.CreateInstance(type);
string propertyName = "somePropertyName";
// how to get somePropertyName from obj?

It is possible to do it without dynamic. Here is Marc Gravell's solution how to do it. However it is interesting if it is possible to do it using dynamic.

Upvotes: 1

Views: 547

Answers (1)

jbtule
jbtule

Reputation: 31799

Mark Gravell has an opensource project called FastMember that makes it easy to access properties using the code the api dynamic generates.

var wrapped = ObjectAccessor.Create(obj); 
var result = wrapped[somePropertyName];

Upvotes: 1

Related Questions