我在我的应用程序中实现了ICustomTypeDescriptor,以便能够在运行时定义自定义属性。我的基本实现看起来像: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.Windows.Forms.BindingContext.UpdateBinding(的BindingContext newBindingContext,装订装订)对象与目标类型不匹配。
在System.Reflection.RuntimeMethodInfo.CheckConsistency(对象 靶)在System.Reflection.RuntimeMethodInfo.Invoke在 (对象OBJ, 的BindingFlags invokeAttr,粘结剂粘结剂,对象[]参数, CultureInfo的文化,布尔skipVisibilityChecks) System.Reflection.RuntimeMethodInfo.Invoke在 System.ComponentModel(对象OBJ,的BindingFlags invokeAttr,粘结剂粘结剂,对象[]参数,CultureInfo的培养物)
在System.ComponentModel.ReflectEventDescriptor.AddEventHandler(对象 成分,代表值)。 ReflectPropertyDescriptor.AddValueChanged(Object componen吨,事件处理程序的处理程序)处 System.Windows.Forms.ListManagerBindingsCollection.AddCore在 System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase System.Windows.Forms.BindToObject.CheckBinding())(绑定 数据绑定)在System.Windows.Forms.BindingsCollection.Add在 System.Windows.Forms.Control.UpdateBindings()
该绑定工作,如果不是活页夹,我使用ext对象。我在ICustomTypeDescriptor实现中遗漏了什么?
SelectedVersion属性返回并位于DynamicClass类中的是什么?你有没有设法解决这个问题? – Martin 2012-03-29 21:08:44