Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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#.Net Framework 4.6.1 操作系统:Windows 7 Home 64位_C#_Winforms_Dynamic_Propertygrid - Fatal编程技术网

类需要什么才能使属性网格显示其属性? 语言:C#.Net Framework 4.6.1 操作系统:Windows 7 Home 64位

类需要什么才能使属性网格显示其属性? 语言:C#.Net Framework 4.6.1 操作系统:Windows 7 Home 64位,c#,winforms,dynamic,propertygrid,C#,Winforms,Dynamic,Propertygrid,有人能告诉我一个属性网格需要什么样的类才能显示它的属性吗 我只是尝试继承和实现ICustomTypeDescriptor,但什么都没有显示 我希望属性网格也尝试解析这些值,而不是仅仅接受一个字符串,并且我希望能够指定一个类别 这是我的密码 using System; using System.Collections; using System.Dynamic; using System.Collections.Generic; using System.Web.Script.Serializat

有人能告诉我一个属性网格需要什么样的类才能显示它的属性吗

我只是尝试继承和实现ICustomTypeDescriptor,但什么都没有显示

我希望属性网格也尝试解析这些值,而不是仅仅接受一个字符串,并且我希望能够指定一个类别

这是我的密码

using System;
using System.Collections;
using System.Dynamic;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Collections.Specialized;
using System.ComponentModel;

namespace PureCore.Library.Data
{

    public class Property : DynamicObject, ICustomTypeDescriptor
    {
        private Dictionary<string, object> Properties = new Dictionary<string, object>();

        public override bool TryGetMember(GetMemberBinder binder, out object value)
        {
            if (Properties.ContainsKey(binder.Name))
            {
                value = Properties[binder.Name];
                return true;
            }
            return base.TryGetMember(binder, out value);
        }
        public override bool TrySetMember(SetMemberBinder binder,     object value)
        {  
            if (Properties.ContainsKey(binder.Name))
            {
                Properties[binder.Name] = value;
                return true;
            }

            Properties.Add(binder.Name, value);
            return true;
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }
        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(this, attributes, true);
        }
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
    }
}

您必须添加更多的代码,特别是您必须明确地为网格提供一个属性列表,从中派生,如下所示:

public class CustomType : DynamicObject, ICustomTypeDescriptor
{
    private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();

    public AttributeCollection GetAttributes() => new AttributeCollection();
    public string GetClassName() => null;
    public string GetComponentName() => null;
    public TypeConverter GetConverter() => null;
    public EventDescriptor GetDefaultEvent() => null;
    public PropertyDescriptor GetDefaultProperty() => null;
    public object GetEditor(Type editorBaseType) => null;
    public EventDescriptorCollection GetEvents() => GetEvents(null);
    public EventDescriptorCollection GetEvents(Attribute[] attributes) => new EventDescriptorCollection(null);
    public PropertyDescriptorCollection GetProperties() => GetProperties(null);
    public object GetPropertyOwner(PropertyDescriptor pd) => this;
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) => new PropertyDescriptorCollection(_properties.Select(p => new CustomProperty(p.Key, p.Value, "Mycats")).ToArray());

    public override bool TryGetMember(GetMemberBinder binder, out object value) => _properties.TryGetValue(binder.Name, out value);
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }
}

public class CustomProperty : PropertyDescriptor
{
    public CustomProperty(string name, object value, string category)
        : base(name, new Attribute[] { new CategoryAttribute(category) })
    {
        Value = value;
    }

    public object Value { get; set; }
    public override Type ComponentType => typeof(CustomType);
    public override bool IsReadOnly => false;
    public override Type PropertyType => Value != null ? Value.GetType() : typeof(object);
    public override bool CanResetValue(object component) => false;
    public override object GetValue(object component) => Value;
    public override void ResetValue(object component) => throw new NotSupportedException();
    public override void SetValue(object component, object value) => Value = value;
    public override bool ShouldSerializeValue(object component) => false;
}
公共类CustomType:DynamicObject,ICustomTypeDescriptor
{
专用只读词典_properties=new Dictionary();
public AttributeCollection GetAttributes()=>new AttributeCollection();
公共字符串GetClassName()=>null;
公共字符串GetComponentName()=>null;
public TypeConverter GetConverter()=>null;
公共事件描述符GetDefaultEvent()=>null;
公共属性描述器GetDefaultProperty()=>null;
公共对象GetEditor(类型editorBaseType)=>null;
public EventDescriptorCollection GetEvents()=>GetEvents(null);
public EventDescriptorCollection GetEvents(Attribute[]attributes)=>new EventDescriptorCollection(null);
公共属性DescriptorCollection GetProperties()=>GetProperties(null);
公共对象GetPropertyOwner(PropertyDescriptor pd)=>此;
public PropertyDescriptorCollection GetProperties(属性[]属性)=>new PropertyDescriptorCollection(_properties.Select(p=>new CustomProperty(p.Key,p.Value,“Mycats”)).ToArray();
public override bool TryGetMember(GetMemberBinder binder,out object value)=>\u properties.TryGetValue(binder.Name,out value);
public override bool TrySetMember(SetMemberBinder绑定器,对象值)
{
_属性[binder.Name]=值;
返回true;
}
}
公共类CustomProperty:PropertyDescriptor
{
公共CustomProperty(字符串名称、对象值、字符串类别)
:base(名称,新属性[]{new CategoryAttribute(category)})
{
价值=价值;
}
公共对象值{get;set;}
公共覆盖类型ComponentType=>typeof(CustomType);
公共覆盖bool IsReadOnly=>false;
公共重写类型PropertyType=>Value!=null?Value.GetType():typeof(对象);
公共覆盖布尔CanResetValue(对象组件)=>false;
公共覆盖对象GetValue(对象组件)=>Value;
public override void ResetValue(对象组件)=>抛出新的NotSupportedException();
public override void SetValue(对象组件,对象值)=>value=value;
公共重写bool ShouldSerializeValue(对象组件)=>false;
}

哇,真管用!而这个类型实际上被解析为一个实类型。如何将属性添加到除misc之外的类别?@Shadowblitz16-category是PropertyDescriptor上的自定义属性
public class CustomType : DynamicObject, ICustomTypeDescriptor
{
    private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();

    public AttributeCollection GetAttributes() => new AttributeCollection();
    public string GetClassName() => null;
    public string GetComponentName() => null;
    public TypeConverter GetConverter() => null;
    public EventDescriptor GetDefaultEvent() => null;
    public PropertyDescriptor GetDefaultProperty() => null;
    public object GetEditor(Type editorBaseType) => null;
    public EventDescriptorCollection GetEvents() => GetEvents(null);
    public EventDescriptorCollection GetEvents(Attribute[] attributes) => new EventDescriptorCollection(null);
    public PropertyDescriptorCollection GetProperties() => GetProperties(null);
    public object GetPropertyOwner(PropertyDescriptor pd) => this;
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes) => new PropertyDescriptorCollection(_properties.Select(p => new CustomProperty(p.Key, p.Value, "Mycats")).ToArray());

    public override bool TryGetMember(GetMemberBinder binder, out object value) => _properties.TryGetValue(binder.Name, out value);
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _properties[binder.Name] = value;
        return true;
    }
}

public class CustomProperty : PropertyDescriptor
{
    public CustomProperty(string name, object value, string category)
        : base(name, new Attribute[] { new CategoryAttribute(category) })
    {
        Value = value;
    }

    public object Value { get; set; }
    public override Type ComponentType => typeof(CustomType);
    public override bool IsReadOnly => false;
    public override Type PropertyType => Value != null ? Value.GetType() : typeof(object);
    public override bool CanResetValue(object component) => false;
    public override object GetValue(object component) => Value;
    public override void ResetValue(object component) => throw new NotSupportedException();
    public override void SetValue(object component, object value) => Value = value;
    public override bool ShouldSerializeValue(object component) => false;
}