sqlnewbie
sqlnewbie

Reputation: 867

Can we use Reflection to read COM object properties info?

Can we use Reflection to read COM object properties info?

I am trying to get Property Name and Value of a COM object using the below code.

But it fails.

here connection is my COM object.

foreach (PropertyInfo info in connection.GetType().GetProperties())
        {

        MyProperty property = new MyProperty();
        property.Name = info.Name;
        property.Value = info.GetValue(connection, null);
    }

Can anyone help me out?

Upvotes: 3

Views: 310

Answers (1)

Maxence
Maxence

Reputation: 13329

You can not use Reflection, but you can use System.ComponentModel.TypeDescriptor :

PropertyDescriptor[] properties = TypeDescriptor.GetProperties(connection)
  .Cast<PropertyDescriptor>().ToArray();
PropertyDescriptor propertyDescriptor = properties
  .Single(p => p.DisplayName == "NameOfProperty");
object propertyValue = propertyDescriptor.GetValue(connection);

Upvotes: 0

Related Questions