How to create a class that inherits from another and passes a type parameter to CodeDom?

Here I want the resulting class declaration to look like this:

public sealed partial class Refund : DataObjectBase<Refund> { } 

}

This code (cut off):

 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 }); 

Creates this class declaration:

 public sealed partial class Refund<Refund> : DataObjectBase { } 

What am I doing wrong?

+3
source share
2 answers

I think that the next line for BaseType should do the trick (untested):

 "DataObjectBase`1[[Refund]]" 

You may need to provide the full name for the Refund , at least, including the name of the assembly:

 "DataObjectBase`1[[Refund, RefundAssembly]]" 

And you need to remove the line targetClass.TypeParameters.Add(...) .

+2
source

If you use expressions in CodeDOM , it can be

 var cls = Define.Class("Refund", TypeAttributes.Public | TypeAttributes.Sealed, true) .Inherits(CodeDom.TypeRef("DataObjectBase","Refund")) 
+1
source

All Articles