我刚刚为 Jon Skeet 的 Unconstrained Melody 安装了 nuget 包项目,但是当我尝试使用它时,编译时出现错误:
Type parameter 'T' inherits conflicting constraints 'UnconstrainedMelody.IEnumConstraint' and 'System.ValueType'
函数定义:
public void SetEnum<T>() where T : struct, IEnumConstraint {}
我错过了什么吗?我不应该使用 nuget 包吗?
最佳答案
我可能是错的,但看起来这个库使用 IEnumConstraint
在内部,并让它与文章中描述的构建后步骤一起工作,它不会为您提供任何魔力来消费 IEnumConstraint
直接用于您自己的方法。
GetValues<T>
帖子中描述的方法是 UnconstrainedMelody.Enums
提供的几种方法之一。类(class)。还有其他可用的对象和方法。
如果您想将自己的泛型方法限制为枚举,您可以按照 Jon 用于构建此库的相同步骤,但在您自己的库上。还有this example在关于如何使用 PostSharp 执行此操作的评论中。
关于c# - 不受约束的旋律错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16340390/