[英]Generic c# property type
I have three classes, two of which inherit from a base class, and the third which I would like to reference one of the other two depending on the state of the application. 我有三个类,其中两个继承自基类,第三个我想引用另外两个中的一个,具体取决于应用程序的状态。
public class Batch
{
public Batch() { }
}
public class RequestBatch : Batch
{
public RequestBatch(string batchJobType) : base(batchJobType) { }
public override int RecordCount
{
get { return Lines.Count; }
}
}
public class ResponseBatch : Batch
{
public ResponseBatch(string batchJobType) : base(batchJobType) { }
public ResponseBatch(int BatchJobRunID)
{ }
}
Sometimes I have an instance of Child1 instantiated, and sometimes I need Child2. 有时我有一个实例化Child1的实例,有时我需要Child2。 However, I have model that I want to pass around my application to keep everything in one place, but I want a way to make the property that holds Child1 and Child2 generic, for example:
但是,我有一个模型,我想传递我的应用程序,以保持一切在一个地方,但我想要一种方法来使属性,包含Child1和Child2通用,例如:
public class BatchJob {
public List<Batch> Batches { get; set; }
}
And then later do this 然后再这样做
public List<RequestBatch> GetBatches(...) {}
var BatchJob = new BatchJob();
BatchJob.Batches = GetBatches(...);
However, the compiler yells at me saying it can't implicitly convert Child1 to (its base type) Parent. 但是,编译器对我大吼大叫说它不能隐式地将Child1转换为(它的基类型)Parent。
I get red squiggles under "= GetBatches(...." saying "Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' 我在“= GetBatches(....”说“无法隐式地将类型'System.Collections.Generic.List'转换为'System.Collections.Generic.List'下的红色波形
Is there a way to generify the Property so it can take any abstract of type Parent? 有没有办法生成属性,所以它可以采取父类型的任何摘要?
Thanks! 谢谢!
The code snipped you show does work. 剪切你显示的代码确实有效。 There is no compiler error:
没有编译器错误:
class Program
{
static void Main()
{
var rj = new RunningJob();
rj.Property = new Child1();
rj.Property = new Child2();
}
}
public class RunningJob {
public Parent Property { get; set; }
}
public class Parent { }
public class Child1 : Parent { }
public class Child2 : Parent { }
The only issue that comes with this code is that Property
is of type Parent
. 此代码附带的唯一问题是
Property
属于Parent
类型。 So you cannot call methods that are specific for Child1/Child2
. 因此,您无法调用特定于
Child1/Child2
。 This can be done using constraints on generic type parameters on class RunningJob
: 这可以使用类
RunningJob
上的泛型类型参数的约束来完成:
public class RunningJob<TParent> where TParent : Parent
{
public TParent Property { get; set; }
}
Hence, now it is ensured that Property
is of type Parent
or any derived types. 因此,现在确保
Property
是Parent
类型或任何派生类型。
One option... 一种选择......
public new IEnumerable<RequestBatch> GetBatches(...) {
get
{
return base.GetBatches(...).OfType<RequestBatch>();
}
}
Another... 另一个...
If you don't need to modify the collection then just change from List<T>
to IEnumerable<T>
如果您不需要修改集合,则只需从
List<T>
更改为IEnumerable<T>
More Info... 更多信息...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.