2

I've got COM object attached to property grid.

Type typeObj = Type.GetTypeFromProgID(progIdService);
var obj = Activator.CreateInstance(typeObj);
propertyGrid1.SelectedObject = obj;

Now I need some way to translate object fields into my language using some translator. I was trying to use wrapper around object but with COM object I have no PropertyInfo, I have only PropertyDescription so I'm still looking for all the possible variants of doing it.

cnd
  • 32,616
  • 62
  • 183
  • 313

2 Answers2

3

What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

like this:

DynamicTypeDescriptor dtp = new DynamicTypeDescriptor(typeObj);

// get current property definition and remove it
var current = dtp.Properties["ThePropertyToChange"];
dtp.RemoveProperty("ThePropertyToChange");

// add a new one, but change its display name
DynamicTypeDescriptor.DynamicProperty prop = new DynamicTypeDescriptor.DynamicProperty(dtp, current, obj);
prop.SetDisplayName("MyNewPropertyName");
dtp.AddProperty(prop);

propertyGrid1.SelectedObject = dtp.FromComponent(obj);
Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • changed it to `DynamicTypeDescriptor dtp = new DynamicTypeDescriptor(typeObj, pd);` and `OriginalProperties = TypeDescriptor.GetProperties(obj);` and now it works great! thank you. – cnd Aug 08 '13 at 08:56
0

I think you can use reflection to get the property names, although I haven't tried with COM objects yet. Be sure to include the System.Reflection namespace, then you can use it like that:

var props = myComObject.GetType().GetProperties();
foreach (var prop in props)
{
    MessageBox(prop.Name);
}
Rob
  • 11,492
  • 14
  • 59
  • 94
  • `.GetProperties()` is empty for COM object. As I said I can operate only with `PropertyDescription` ( `TypeDescriptor.GetProperties(instance)` ) but still I don't know what to do with it. – cnd Aug 08 '13 at 06:43
  • You're right, that's because of the Late Binding. Please take a look at this post for details: http://stackoverflow.com/questions/9735394/reflection-on-com-interop-objects – Rob Aug 08 '13 at 07:04
  • so I need another way. – cnd Aug 08 '13 at 07:21