CodeDom - Howto generate Constructor with initializer

L

Larry

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

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

Thanks,
Larry
 
A

Andrew Gnenny

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top