在运行时添加属性
我有一个类,程序员可以使用它来动态添加新属性。为此,它实现了 ICustomTypeDescriptor
以便能够重写 GetProperties()
方法。
public class DynamicProperty
{
public object Value { get; set; }
public Type Type { get; set; }
public string Name { get; set; }
public Collection<Attribute> Attributes { get; set; }
}
public class DynamicClass : ICustomTypeDescriptor
{
// Collection to code add dynamic properties
public KeyedCollection<string, DynamicProperty> Properties
{
get;
private set;
}
// ICustomTypeDescriptor implementation
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
// Properties founded within instance
PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Fill property collection with founded properties
PropertyDescriptorCollection propsCollection =
new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());
// Fill property collection with dynamic properties (Properties)
foreach (var prop in Properties)
{
// HOW TO?
}
return propsCollection;
}
}
是否可以迭代“属性”列表以将每个属性添加到 PropertyDescriptorCollection
中?
基本上,我希望程序员能够将 DynamicProperty
添加到将由 GetProperties
处理的集合中。类似于:
new DynamicClass()
{
Properties = {
new DynamicProperty() {
Name = "StringProp",
Type = System.String,
Value = "My string here"
},
new DynamicProperty() {
Name = "IntProp",
Type = System.Int32,
Value = 10
}
}
}
现在,只要调用 GetProperties
,这些 Properties
就会被设置为实例属性。我这样想正确吗?
I have a class which the programmer can use to dynamically add new properties. For that it implements the ICustomTypeDescriptor
to be able to override GetProperties()
method.
public class DynamicProperty
{
public object Value { get; set; }
public Type Type { get; set; }
public string Name { get; set; }
public Collection<Attribute> Attributes { get; set; }
}
public class DynamicClass : ICustomTypeDescriptor
{
// Collection to code add dynamic properties
public KeyedCollection<string, DynamicProperty> Properties
{
get;
private set;
}
// ICustomTypeDescriptor implementation
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
// Properties founded within instance
PropertyInfo[] instanceProps = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Fill property collection with founded properties
PropertyDescriptorCollection propsCollection =
new PropertyDescriptorCollection(instanceProps.Cast<PropertyDescriptor>().ToArray());
// Fill property collection with dynamic properties (Properties)
foreach (var prop in Properties)
{
// HOW TO?
}
return propsCollection;
}
}
Is it possible to iterate over the Properties list to add each property to PropertyDescriptorCollection
?
Basically I want the programmer to be able to add a DynamicProperty
to a collection which will be handled by GetProperties
. Something like:
new DynamicClass()
{
Properties = {
new DynamicProperty() {
Name = "StringProp",
Type = System.String,
Value = "My string here"
},
new DynamicProperty() {
Name = "IntProp",
Type = System.Int32,
Value = 10
}
}
}
Now those Properties
would be setted to instance properties whenever GetProperties
is called. Am I thinking this the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在创建这样的集合:
但是您正在创建的集合仅具有现有属性,而没有新属性。
您需要提供由现有属性和新属性连接而成的单个数组。
像这样的事情:
下一个问题:您需要
customProperties
它是PropertyDescriptor
的集合。不幸的是PropertyDescriptor
是一个抽象类,因此您没有一种简单的方法来创建它。不过,我们可以解决这个问题,只需通过从
PropertyDescriptor
派生并实现所有抽象方法来定义您自己的CustomPropertyDescriptor
类即可。像这样的事情:
我还没有填写获取和设置你的属性的调用;这些调用取决于您如何在后台实现动态属性。
然后,您需要创建一个
CustomPropertyDescriptor
数组,其中填充适合您的动态属性的信息,并将其连接到我最初描述的基本属性。PropertyDescriptor
不仅描述您的属性,它还使客户端能够实际获取和设置这些属性的值。这就是重点,不是吗!You are already creating a collection like this:
But the collection you are creating only has the existing properties, not your new properties.
You need to supply a single array concatenated from the existing properties and your new properties.
Something like this:
Next problem: you need
customProperties
which is a collection ofPropertyDescriptor
. UnfortunatelyPropertyDescriptor
is an abstract class so you don't have an easy way to create one.We can fix this though, just define your own
CustomPropertyDescriptor
class by deriving fromPropertyDescriptor
and implementing all the abstract methods.Something like this:
I haven't filled in the calls to get and set your properties; those calls depend on how you've implemented the dynamic properties under the hood.
Then you need to create an array of
CustomPropertyDescriptor
filled in with information appropriate to your dynamic properties, and concatenate it to the basic properties as I described initially.The
PropertyDescriptor
not only describes your properties, it also enables client to actually get and set the values of those properties. And that's the whole point, isn't it!