有没有办法用CodeDom生成类约束。

因为当我使用像

这样的东西时
var method = new CodeMemberMethod();
var genericParam = new CodeTypeParameter("InterfaceType");
genericParam.Constraints.Add("class");
method.TypeParameters.Add(genericParam);

生成的代码就像

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

我找到的最佳解决方法是在课程之前使用前导空格

genericParam.Constraints.Add(" class");

但这似乎充其量只是一种解决方法。

有帮助吗?

解决方案

似乎没有直接的方法来指定该约束。对于“结构”而言,都没有。约束

对于“T:new()”约束使用标志 HasConstructorConstraint

其余使用CodeTypeReference,如此msdn示例

其他提示

我还使用零宽度空间(&quot; \ x200Bclass&quot; )而不是普通空间。然后我在最后一个字符串中用空字符串替换它: .Replace(&quot; \ x200B&quot;,string.Empty);

scroll top