views:

331

answers:

2

Is there a way to generate a class constraint with CodeDom.

Because when I use something like

var method = new CodeMemberMethod();
var genericParam = new CodeTypeParameter("InterfaceType");
genericParam.Constraints.Add("class");
method.TypeParameters.Add(genericParam);

the generated code is like

private InterfaceType GetImpl<InterfaceType>()
    where InterfaceType : @class
{
}

The best workaround i found is to use a leading whitespace before the class

genericParam.Constraints.Add(" class");

But this seems to be at best a workaround.

+3  A: 

It seems that there is no straigntforward way to specify that constraint. Neither for the "struct" constraint.

For the "T : new()" constraint use the flag HasConstructorConstraint

For the rest use CodeTypeReference as in this msdn example.

Yacoder
A: 

CodeDom does not like class names starting with lower-case letter. It is general guideline to use title-cased class names (i.e. "Class" instead of "class") and there will be no '@' then.

Regent