Я реализовал ICustomTypeDescriptor в своем приложении, чтобы иметь возможность определять пользовательские свойства во время выполнения. Моя базовая реализация выглядит так:
public class DynamicClass <T> : ICustomTypeDescriptor
{
private readonly T _object;
public DynamicClass(T trackedObject)
{
_object = trackedObject;
}
// Collection to code add dynamic properties
public KeyedCollection<string, DynamicProperty> Properties
{
get;
private set;
}
// ICustomTypeDescriptor implementation
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(_object, true);
}
public string GetClassName()
{
return TypeDescriptor.GetClassName(_object, true);
}
public string GetComponentName()
{
return TypeDescriptor.GetComponentName(_object, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(_object, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(_object, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(_object, true);
}
public object GetEditor(Type editorBaseType)
{
throw new NotImplementedException();
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(_object, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(_object, attributes, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(_object, true);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(_object, attributes, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return _object;
}
}
Проблема в том, что теперь, когда я связываю объект с текстовым полем, используя связыватель DynamicClass, он больше не работает.
Я использую это так:
DynamicClass<ExtensionModel> binder = new DynamicClass<ExtensionModel>(ext);
_versionLabel.DataBindings.Add("Text", binder, "SelectedVersion", false, DataSourceUpdateMode.OnPropertyChanged);
и я получил исключение: «Объект не соответствует типу цели.»
Объект не соответствует типу цели.
в System.Reflection.RuntimeMethodInfo.CheckConsistency (Object
target) в System.Reflection.RuntimeMethodInfo.Invoke (Object obj,
BindingFlags invokeAttr, Binder binder, Object [] параметры,
КультураИнфо-культура, логическое скипVisibilityChecks) в
System.Reflection.RuntimeMethodInfo.Invoke (Object obj, BindingFlags
invokeAttr, Binder Binder, Object [] параметры, CultureInfo culture)
at System.ComponentModel.ReflectEventDescriptor.AddEventHandler (Object
компонент, значение делегата) в
System.ComponentModel.ReflectPropertyDescriptor.AddValueChanged (Объект
компонент, обработчик EventHandler) в
System.Windows.Forms.BindToObject.CheckBinding () в
System.Windows.Forms.Binding.SetListManager (BindingManagerBase
bindingManagerBase) в
System.Windows.Forms.ListManagerBindingsCollection.AddCore (Binding
dataBinding) в System.Windows.Forms.BindingsCollection.Add (Binding
обязательный) в
System.Windows.Forms.BindingContext.UpdateBinding (BindingContext
newBindingContext, Binding привязка) в
System.Windows.Forms.Control.UpdateBindings ()
Привязка работает, если вместо связующего я использую объект ext. Я что-то пропустил в реализации ICustomTypeDescriptor?