具有Enum参数的扩展类无法编译 [英] Extension class with Enum parameter won't compile

查看:96
本文介绍了具有Enum参数的扩展类无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法使用扩展类GetDifference进行编译.它期望将枚举作为唯一参数,而这似乎是我要传递的参数,但出现错误:

The following code fails to compile using the extension class GetDifference. It''s expecting an Enum as the only parameter and that appears to be what I''m passing it, but I get the error:

The type arguments for method 'DesignPatterns.EnumClass.GetDifference<t>(System.Enum, System.Enum)' cannot be inferred from the usage. Try specifying the type arguments explicitly.



有什么想法为什么它不能编译?谢谢!



Any ideas why it won''t compile? Thanks!

static class EnumClass
{
[Flags]
public enum MyColor
{
	None = 0,
	Red = 1,
}

public static void TestEnums()
{
	MyColor myColor1 = MyColor;
	MyColor myColor2 = MyColor;

	myColor1.GetDifference(myColor2);
}

public static FlagsEnumDifference<T> GetDifference<T>(this Enum source, Enum compare) where T : struct
{
	IEnumerable<T> sourceValues = source.GetIndividualValues<T>();
	IEnumerable<T> compareValues = compare.GetIndividualValues<T>();

	IEnumerable<T> added = compareValues.Where(value => !sourceValues.Contains(value));
	IEnumerable<T> removed = sourceValues.Where(value => !compareValues.Contains(value));
	return new FlagsEnumDifference<T>(added, removed);
}

推荐答案

这不是定义和使用扩展方法的方式

我无法修复您的代码,因为您没有在代码示例中显示扩展类的定义,但是在使用扩展的代码中未使用扩展类的名称.这就是重点.

语法非常易于理解和使用.请参阅:
http://msdn.microsoft.com/en-us/library/bb383977.aspx [ ^ ].

—SA
This is not how extension methods are defined and used

I cannot fix your code because you did not show the extension class definition in your code sample, but the name of extension class is not used in the code using extensions; this is the whole point.

The syntax is fairly simple to understand and use. Please see:
http://msdn.microsoft.com/en-us/library/bb383977.aspx[^].

—SA


这篇关于具有Enum参数的扩展类无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆