【发布时间】:2011-02-22 05:58:48
【问题描述】:
我有 A、B 和 C 三个班级, 有些属性在所有三个类中,有些则不在
public class A
{
public string Firstname {get; set;}
public string Lastname {get; set;}
public int ID {get; set;}
public int xxx {get; set} // only in class A
public int yyy{get; set} // only in class A
...
}
public class B
{
public string Firstname {get; set;}
public string Lastname {get; set;}
public int ID {get; set;}
public int aaa {get; set} // only in class B
public int bbb {get; set} // only in class B
...
}
public class C
{
public string Firstname {get; set;}
public string Lastname {get; set;}
public int ID {get; set;}
public int kkk {get; set} // only in class C
public int ppp {get; set} // only in class C
...
}
我想调用 XYZ 类的 Execute 方法...
public class XYZ
{
public override Execute<T>() where T: Generic_T, new()
{
T abc = new T();
...
Debug.WriteLine(abc.Firstname + ”, “ + abc.Lastname + “, “ + abc.ID);
}
}
...包含所有三个类,例如:
XYZ x1 = new XYZ();
XYZ.Execute<A>();
XYZ x2 = new XYZ();
XYZ.Execute<B>();
XYZ x3 = new XYZ();
XYZ.Execute<C>();
我的想法行不通:
public class Generic_T
{
public static implicit operator A(Generic_T x)
{
return (A)x.MemberwiseClone();
}
}
错在哪里?
提前致谢!
【问题讨论】:
-
我修正了你的代码并重新格式化,但代码根本无效,它不会编译。有几个错误,所以我可以说出你想要回答的内容。
-
请提供一个真实的例子。你显然是在这里输入的。复制/粘贴实际代码
标签: c# operators constraints