泛型类型的集合
我有一个对象(表单),其中包含一个集合(.Fields),我想包含通用类(FormField)的实例。
FormField 简单地定义如下:
public class FormField<T>
{
private Form Form;
public T Value { get; set; }
public string Name { get; set; }
public void Process()
{
// do something
}
public FormField(Form form, string name, T value)
{
this.Name = name;
this.Value = value;
this.Form = form;
}
}
这允许我拥有 FormField、FormField 等,并且该部分工作得很好。 我想要的是“Formfields”的集合,无论类型如何,但我被迫定义一个类型(看起来),例如:
public class Form
{
string Code { get; set; }
string Title { get; set; }
int Year { get; set; }
Guid ClientID { get; set; }
ICollection<FormField<int>> Fields { get; set; }
}
我认为我想要的是一个允许我抽象类型信息的接口,从而将集合键入为(例如)IFormField 而不是 FormField<> 的实例
但如果不在界面中强烈键入集合,我看不到如何定义它......
任何帮助(包括任何替代解决方案!)将不胜感激!
谢谢,本
I have an object (form) which contains a collection (.Fields) which I want to contain instances of a generic class (FormField).
The FormField, simply, is defined as such:
public class FormField<T>
{
private Form Form;
public T Value { get; set; }
public string Name { get; set; }
public void Process()
{
// do something
}
public FormField(Form form, string name, T value)
{
this.Name = name;
this.Value = value;
this.Form = form;
}
}
This allows me to have FormField, FormField etc. and that part works great.
What I want is a collection of "Formfields" regardless of the type, but I am forced into defining a type (it seems) such as:
public class Form
{
string Code { get; set; }
string Title { get; set; }
int Year { get; set; }
Guid ClientID { get; set; }
ICollection<FormField<int>> Fields { get; set; }
}
What, I think, I want is an interface that allows me to abstract the type information and thus type the collection as instances of (for exxample) IFormField not FormField<>
But I can't see how to define this without strongly typing the collection in the interface...
Any help (including any alternative solutions!) would be greatly appreciated!
Thanks, Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一些完成乔恩答案的代码:
并以您的形式:
Here's some code to complete Jon's answer:
And in your form:
创建一个非泛型接口或基类,其中可能包括
FormField
所做的所有事情, 除了特定于类型的位。然后您就可以拥有一个ICollection
。显然,就所使用的字段类型而言,您将无法以强类型的方式使用它 - 但您可以使用它的所有非特定于类型的位(例如名称和形式)。Create a non-generic interface or base class, which probably includes everything
FormField
does except the type-specific bits. Then you can have anICollection<IFormField>
. Obviously you won't be able to use this in a strongly-typed way, in terms of the type of field being used - but you can use all the non-type-specific bits of it (e.g. the name and the form).另一种选择(Jon 的答案的替代方案)是应用适配器模式,在以下情况下很有用:
当您想要公开特定于类型的位时,您可以有效必须创建一个非通用包装器。一个简短的例子:
在基类型中实现这种非通用行为将有效地打破 里氏替换原则,这就是为什么我更喜欢包装方法 我也在我的博文中争论。
Another option (an alternative to Jon's answer) is to apply the adapter pattern, which can be useful when:
When you want to expose type-specific bits, you effectively have to create a non-generic wrapper. A short example:
Implementing this non-generic behavior in a base-type would effectively break the Liskov substitution principle, which is why I prefer the wrapper approach as I also argue in my blog post.