How do I create a class that inherits from another and passes a type parameter in CodeDom?
Asked Answered
B

2

3

Here's what I want the resulting class declaration to look like:

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

}

This code (snipped):

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

Produces this class declaration:

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

What am I doing wrong?

Bran answered 28/8, 2009 at 16:58 Comment(0)
S
2

I think that the following string for the BaseType should do the trick (untested):

"DataObjectBase`1[[Refund]]"

It may be possible that you need to provide a fully-qualified name for Refund, at least including the assembly name:

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

And you should then remove the line targetClass.TypeParameters.Add(...).

Sorority answered 28/8, 2009 at 17:13 Comment(1)
The correct line ended up being targetClass.BaseTypes.Add(new CodeTypeReference { BaseType = "DataObjectBase`1[Refund]", Options = CodeTypeReferenceOptions.GenericTypeParameter });Bran
T
1

If you use Expressions to CodeDOM it could be

var cls = Define.Class("Refund", 
   TypeAttributes.Public | TypeAttributes.Sealed, true)
   .Inherits(CodeDom.TypeRef("DataObjectBase","Refund"))
Tareyn answered 22/9, 2009 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.