Domanda

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.

È stato utile?

Soluzione

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);

Altri suggerimenti

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top