【问题标题】:CodeDom generic type constraintCodeDom 泛型类型约束
【发布时间】:2009-01-04 14:29:31
【问题描述】:

有没有办法用 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");

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

【问题讨论】:

    标签: c# generics codedom


    【解决方案1】:

    似乎没有明确的方法来指定该约束。既不是for the "struct" constraint

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

    其余部分使用 CodeTypeReference,如 this msdn example

    【讨论】:

      【解决方案2】:

      我还使用零宽度空格 ("\x200Bclass") 代替普通空格。然后我用空字符串替换它在最终字符串中:.Replace("\x200B", string.Empty);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多