CodeDom - Howto generate Constructor with initializer

  • Thread starter Thread starter Larry
  • Start date Start date
L

Larry

Does anyone know how to generate the following code using CodeDom?

class ChildClass {
public ChildClass(string sometext) : base(sometext) {
}
}

Thanks,
Larry
 
Hi,

Here is a code example:
/////////////////////////////////////////////////////////////////
CodeTypeDeclaration cls = new CodeTypeDeclaration("ChildClass");

cls.IsClass = true;

cls.BaseTypes.Add("BaseClass");

CodeConstructor cons = new CodeConstructor();

cons.Attributes = MemberAttributes.Public;

cons.Parameters.Add(new CodeParameterDeclarationExpression(typeof(string),
"sometext"));

cons.BaseConstructorArgs.Add(new
CodeArgumentReferenceExpression("sometext"));

cls.Members.Add(cons);

/////////////////////////////////////////////////////////////////

It produces code like this:

public class ChildClass : BaseClass
{

public ChildClass(string sometext) :
base(sometext)
{
}
}
 
Back
Top