我在我的應用程序中實現了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