【发布时间】:2011-10-20 16:57:34
【问题描述】:
我可以创建一个接受两种类型的泛型方法吗? attributeType 和 ts_attributeType 不共享任何公共父类,尽管它们具有相同的字段。
这可能吗?或者有什么方法可以实现吗?
private static void FieldWriter<T>(T row)
where T : attributeType, ts_attributeType
{
Console.Write(((T)row).id + "/" + (((T)row).type ?? "NULL") + "/");
}
我看过这个answer from Jon Skeet,但我不确定它是否也适用于我的问题。
一些进一步的背景:
attributeType 和 ts_attributeType 都是使用 xsd.exe 工具创建的;并且是部分类。
【问题讨论】:
-
你能让它们都实现一个共享接口吗?