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
 
Ad

Advertisements

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

Advertisements


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

Similar Threads

CodeDom VB code generation 5
CodeDom and @ 0
CodeDOM question: explicit conversion operator generation 1
slow CodeDom 0
EnvDTE of CodeDom 1
CodeDom Question 3
CodeDom and enums 0
CodeDOM question 0

Top