簡體   English   中英

如何創建從另一個繼承並在CodeDom中傳遞類型參數的類?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM