![](/img/trans.png)
[英]How to create a generic class using CodeDOM whose generic parameter is a type that I created?
[英]How do I create a class that inherits from another and passes a type parameter in CodeDom?
這是我想要的結果類聲明的樣子:
public sealed partial class Refund : DataObjectBase<Refund>
{
}
}
這段代碼(剪掉):
targetClass = new CodeTypeDeclaration(className);
targetClass.IsClass = true;
targetClass.TypeAttributes =
TypeAttributes.Public | TypeAttributes.Sealed;
targetClass.IsPartial = true; //partial so that genn'ed code can be safely modified
targetClass.TypeParameters.Add(new CodeTypeParameter{ Name=className});
targetClass.BaseTypes.Add(new CodeTypeReference { BaseType = "DataObjectBase", Options = CodeTypeReferenceOptions.GenericTypeParameter });
生成此類聲明:
public sealed partial class Refund<Refund> : DataObjectBase
{
}
我究竟做錯了什么?
我認為BaseType的以下字符串應該可以做到(未經測試):
"DataObjectBase`1[[Refund]]"
您可能需要為Refund
提供一個完全限定的名稱,至少包括程序集名稱:
"DataObjectBase`1[[Refund, RefundAssembly]]"
然后你應該刪除targetClass.TypeParameters.Add(...)
。
如果您使用Expressions到CodeDOM,它可能是
var cls = Define.Class("Refund",
TypeAttributes.Public | TypeAttributes.Sealed, true)
.Inherits(CodeDom.TypeRef("DataObjectBase","Refund"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.