非常简单的例子,它可以被构建,并且在PropertyGrid中你会看到一些东西。 您需要阅读有关ICustomTypeDescriptor和PropertyDescriptor的信息。
在窗体的Load:
propertyGrid1.SelectedObject = new MyType(new[] { "Property1", "Property2" });
类型:
public class MyType : ICustomTypeDescriptor
{
private string[] _properties;
public MyType(string[] properties)
{
_properties = properties;
}
public AttributeCollection GetAttributes()
{
return null;
}
public string GetClassName()
{
return nameof(MyType);
}
public string GetComponentName()
{
throw new NotImplementedException();
}
public TypeConverter GetConverter()
{
return null;
}
public EventDescriptor GetDefaultEvent()
{
throw new NotImplementedException();
}
public PropertyDescriptor GetDefaultProperty()
{
return null;
}
public object GetEditor(Type editorBaseType)
{
return null;
}
public EventDescriptorCollection GetEvents()
{
throw new NotImplementedException();
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
throw new NotImplementedException();
}
public PropertyDescriptorCollection GetProperties()
{
return GetProperties(null);
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
var props = new PropertyDescriptor[_properties.Length];
for (int i = 0; i < _properties.Length; i++)
props[i] = new CustomPropertyDescriptor(_properties[i],
new Attribute[]
{
new DisplayNameAttribute(@"Displ Value " + i),
new CategoryAttribute("Category" + i%2)
});
return new PropertyDescriptorCollection(props);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
}
public class CustomPropertyDescriptor : PropertyDescriptor
{
public CustomPropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs)
{
}
public override bool CanResetValue(object component)
{
return true;
}
public override object GetValue(object component)
{
return "1";
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
throw new NotImplementedException();
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType { get; }
public override bool IsReadOnly { get { return false; } }
public override Type PropertyType { get { return typeof (string); } }
}
使用搜索......这是不可能的,但你可以使用TypeDescriptor,例如。 – Spawn
不是没有构建一个新的类型或做某种形式的IL编织。属性在编译时定义并编译到程序集中。 –
@Spawn任何示例如何使用TypeDescriptor?我做了TypeDescriptor对象,但不知道如何将它与任何变量绑定。 –