Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ICustomTypeDescriptor在实现时引发argumentexception_C#_.net_Propertygrid_Icustomtypedescriptor - Fatal编程技术网

C# ICustomTypeDescriptor在实现时引发argumentexception

C# ICustomTypeDescriptor在实现时引发argumentexception,c#,.net,propertygrid,icustomtypedescriptor,C#,.net,Propertygrid,Icustomtypedescriptor,我想从PropertyGrid中的可浏览属性中排除属性MiddleName 当我挂起Person类上的接口ICustomTypeDescriptor时,我在启动应用程序时遇到此异常 我错了什么 System.ArgumentException: 无法绑定到数据源上的属性或列TestNamefür。 参数名称:dataMember bei System.Windows.Forms.BindToObject.CheckBinding() bei System.Windows.Forms.Bindin

我想从PropertyGrid中的可浏览属性中排除属性MiddleName

当我挂起Person类上的接口ICustomTypeDescriptor时,我在启动应用程序时遇到此异常

我错了什么

System.ArgumentException: 无法绑定到数据源上的属性或列TestNamefür。 参数名称:dataMember bei System.Windows.Forms.BindToObject.CheckBinding() bei System.Windows.Forms.Binding.SetListManager(BindingManagerBase BindingManagerBase) bei System.Windows.Forms.ListManagerBindingsCollection.AddCore(绑定数据绑定)

公共类人物:ICustomTypeDescriptor
{
公共字符串TestName{get;set;}
公共字符串名{get;set;}
公共字符串MiddleName{get;set;}
公共字符串LastName{get;set;}
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
返回TypeDescriptor.GetAttributes(this,true);
}
字符串ICustomTypeDescriptor.GetClassName()
{
返回TypeDescriptor.GetClassName(此为true);
}
字符串ICustomTypeDescriptor.GetComponentName()
{
返回TypeDescriptor.GetComponentName(此为true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
返回TypeDescriptor.GetConverter(此为true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
返回TypeDescriptor.GetDefaultEvent(this,true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
返回TypeDescriptor.GetDefaultProperty(this,true);
}
对象ICustomTypeDescriptor.GetEditor(类型editorBaseType)
{
返回TypeDescriptor.GetEditor(this,editorBaseType,true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(属性[]属性)
{
返回TypeDescriptor.GetEvents(this,attributes,true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
返回TypeDescriptor.GetEvents(this,true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(属性[]属性)
{
Print(“GetProperties()”);
打印(“属性为{0}null”,属性==null?”:“非”);
PropertyDescriptorCollection origCol=TypeDescriptor.GetProperties(this,attributes,true);
bool wantBrowsable=attributes.Contains


Marc Gravell的最后一个解决方案非常有效!

我在测试中测试了您的代码。也许我们需要更多的代码来了解问题所在

无论如何,如果您的唯一目的只是从
Propertygrid
中隐藏
MiddleName
属性,为什么不简单地在该属性上添加
[Browsable(false)
属性,而不是实现
ICustomTypeDescriptor

这会让你从大量的代码中解脱出来

编辑:

我的意思是,像这样的代码必须有效:


并且应该正确地从属性网格中隐藏
MiddleName
property…

我忘了说=>如果我在TestName上使用了browseable属性,我会得到同样的异常:/n即使你从person类中删除
ICustomTypeDescriptor
实现,我还是觉得很奇怪……person类就是你发布它的方式吗?@digEmAll通过实现ICustomTypeDescriptor,我在哪里说过我不想让TestName作为DataMember?顺便说一句。这个类只是一个伪类,我不能在这里发布真实代码。(不允许)好的,我发现了错误:设计器文件中有一个旧的数据绑定,但不再使用。我删除了整个bindingsource,并删除了当前的异常。我现在看到的问题是,我的propertyGrid绑定到person对象及其某些属性。同时,我在propertyGrid之外还有绑定到Property的控件我想躲在楼房里有人知道解决办法吗?
public class Person : ICustomTypeDescriptor
{
    public string TestName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
      return TypeDescriptor.GetAttributes(this, true);
    }
    string ICustomTypeDescriptor.GetClassName()
    {
      return TypeDescriptor.GetClassName(this, true);
    }
    string ICustomTypeDescriptor.GetComponentName()
    {
      return TypeDescriptor.GetComponentName(this, true);
    }
    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
      return TypeDescriptor.GetConverter(this, true);
    }
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
      return TypeDescriptor.GetDefaultEvent(this, true);
    }
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
      return TypeDescriptor.GetDefaultProperty(this, true);
    }
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
      return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
      return TypeDescriptor.GetEvents(this, attributes, true);
    }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
      return TypeDescriptor.GetEvents(this, true);
    }
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
      Debug.Print("GetProperties()");
      Print("Attributes is {0}null", attributes == null ? "" : "not ");
      PropertyDescriptorCollection origCol = TypeDescriptor.GetProperties(this, attributes, true);
      bool wantBrowsable = attributes.Contains<Attribute>(new BrowsableAttribute(true));
      Debug.Print("Wants Browsable: {0}", wantBrowsable);
      List<PropertyDescriptor> newCol = new List<PropertyDescriptor>();
      foreach (PropertyDescriptor pd in origCol)
      {
        System.Diagnostics.Debug.Print("Property Name: {0}", pd.Name);
        if (pd.Name != "MiddleName")
        {
          System.Diagnostics.Debug.Print("Property {0} is included.", pd.Name);
          newCol.Add(pd);
        }
      }
      return new PropertyDescriptorCollection(newCol.ToArray());
    }
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
      return ((ICustomTypeDescriptor)this).GetProperties(null);
    }
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
      return this;
    }
}
public class Person
{
    public string TestName { get; set; }

    public string FirstName { get; set; }

    [Browsable(false)]
    public string MiddleName { get; set; }

    public string LastName { get; set; }
}