[英]How to use a nested variable which is a generic class based member
i am trying to create a general class which will have a nested member class which will be somewhat template. 我试图创建一个通用类,它将具有一个嵌套的成员类,这将是一些模板。 I am using generics on the derived member but not sure how to ensure the main class should be defined properly to allow generics to be used through out.
我在派生成员上使用泛型,但不确定如何确保正确定义主类以允许使用泛型。
My first implementation was object type which works but I was wanting to use generics through out the definition. 我的第一个实现是可以工作的对象类型,但我想在整个定义中使用泛型。
provided is the current 3 class 提供的是当前的3类
structure hierarchy is, similar to a look aside record. 结构层次结构类似于旁听记录。
{ event template } ---> {event context } --> {event record } {事件模板} ---> {事件上下文}-> {事件记录}
// templates //模板
public class EventTemplate1
{
public string name { get; set; }
// ..... other members
public string template1 { get; set; }
}
public class EventTemplate2
{
public string name { get; set; }
// ..... other members
public string template2 { get; set; }
}
// context //上下文
public class EventData<T>
{
protected T _context;
// ..... other
public EventData(T t)
{
this._context = t;
}
// ..... other members
public T Context
{
get { return _context; }
set { _context = value; }
}
}
record details (with object) 记录详细信息(带有对象)
public class writeClass
{
// ..... other members
public object data { get; set; }
}
// calling code with object //用对象调用代码
Template1 temp1 = new Template1();
temp1.name = "test 1";
writeClass writeEvent = new writeClass();
writeEvent.data = temp1;
changing to support generics in writeClass 更改以支持writeClass中的泛型
public class writeClass <T> where T : EventData<T>
{
public writeClass(T t)
{
data = t;
}
// ..... other members
public EventData<T> data { get; set; }
}
When I try to call this it doesn't quite work as expected so I am missing some important here. 当我尝试调用它时,它并没有达到预期的效果,因此在这里我错过了一些重要信息。
Problem 问题
public class writeClass <T> where T : EventData<T>
resolves to 决心
public class writeClass <EventData<EventData<EventData<EventData<EventData....>>>> where T : EventData<T>
Solution 解
Make a BaseClass for your EventData 为您的EventData创建一个BaseClass
public class BaseEventTemplate {
public string name { get; set; }
}
public class EventTemplate1 : BaseEventTemplate {
// ..... other members
public string template1 { get; set; }
}
public class EventTemplate2: BaseEventTemplate {
// ..... other members
public string template2 { get; set; }
}
public class EventData<T> where T : BaseEventTemplate {
protected T _context;
// ..... other
public EventData(T t)
{
this._context = t;
}
// ..... other members
public T Context
{
get { return _context; }
set { _context = value; }
}
} }
Change the constraint in your writeClass like the following 更改您的writeClass中的约束,如下所示
public class writeClass <T> where T : EventData<BaseEventTemplate> {
public writeClass(T t)
{
data = t;
}
// ..... other members
public EventData<BaseEventTemplate> data { get; set; }
}
calling the object 调用对象
Template1 temp1 = new Template1();
temp1.name = "test 1";
writeClass writeEvent = new writeClass(temp1);
Foo foo = writeEvent.data;
Solution2 解决方案2
Make a specific implementation of writeClass for every derived type, by creating an interface of writeClass 通过创建writeClass接口,为每个派生类型实现writeClass的特定实现。
public interface IWriteClass<T> : where T : EventData<BaseEventTemplate>
public class WriteTemplate1 : IWriteClass<EventData<Template1>
public class WriteTemplate2 : IWriteClass<EventData<Template2>
Solution2 is more or less an addition to Solution1. Solution2或多或少是Solution1的补充。 But if you don't use an interface you'd have to check the type of your parameter in your writeClass class and cast it into the specific type.
但是,如果不使用接口,则必须检查writeClass类中参数的类型并将其转换为特定类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.