项目的集合,使项目的字段作为集合可访问
collection of items which makes the fields of the items as collection accessible
我不知道这是否可能,以及如何可能。我基本上想稍后使用它:
//setting a single item as known.
label1.BackColor=Color.FromARGB(255,255,255);
/*setting multiple items in something like a container (wanted, not yet possible)*/
sContainer.BackColor=Color.FromARGB(255,255,255);
//and that's how it works recently with my class...
sContainer.SetValues("BackColor",Color.FromARGB(255,255,255));
我猜容器将不得不使用泛型和反射。
容器 class 应该 "copy" T-Class(此处为 Control)的所有可设置属性到其自身。因此,如果我使用 SetContainer<Control>
,我可以通过 sContainer1.BackColor=...
设置容器中的所有背景色值。
我的代码直到知道看起来像:
public class SetContainer<T>:IList<T>
{
/*I've implemented all methods of IList by using the elements-List*/
private List<T> elements;
public void SetValues(string propname,object value)
{
FieldInfo f=typeof(T).GetField(propname);
elements.ForEach(x=>f.SetField(x,value);
}
}
不好意思我不知道怎么形容这个很好理解
通过使用这个 的修改版本,您可以将 "link" 通用 class 的属性添加到容器 class。
这使得将一个值应用于整个值集合成为可能。
谢谢@Sagi!
我不知道这是否可能,以及如何可能。我基本上想稍后使用它:
//setting a single item as known.
label1.BackColor=Color.FromARGB(255,255,255);
/*setting multiple items in something like a container (wanted, not yet possible)*/
sContainer.BackColor=Color.FromARGB(255,255,255);
//and that's how it works recently with my class...
sContainer.SetValues("BackColor",Color.FromARGB(255,255,255));
我猜容器将不得不使用泛型和反射。
容器 class 应该 "copy" T-Class(此处为 Control)的所有可设置属性到其自身。因此,如果我使用 SetContainer<Control>
,我可以通过 sContainer1.BackColor=...
设置容器中的所有背景色值。
我的代码直到知道看起来像:
public class SetContainer<T>:IList<T>
{
/*I've implemented all methods of IList by using the elements-List*/
private List<T> elements;
public void SetValues(string propname,object value)
{
FieldInfo f=typeof(T).GetField(propname);
elements.ForEach(x=>f.SetField(x,value);
}
}
不好意思我不知道怎么形容这个很好理解
通过使用这个
这使得将一个值应用于整个值集合成为可能。
谢谢@Sagi!