不能用于类型的参数类型'MyEnum'的表达 [英] Expression of type 'MyEnum' cannot be used for parameter of type
问题描述
我创建枚举ToFrendlyString功能为我的枚举,但我不能在LINQ的使用。
公共枚举MyEnum
{
排队= 0,
[说明(进行中)]
IN_PROGRESS = 2,
[说明(无应答)]
No_answer = 6,
}
公共静态类EnumToFrendlyString
{
公共静态字符串ToFrendlyString(此枚举值)
{
返回value.GetEnumDescription();
}
公共静态字符串GetEnumDescription(此枚举值)
{
字段信息网络= value.GetType()。getfield命令(value.ToString( ));
无功属性=
(DescriptionAttribute [])fi.GetCustomAttributes(
typeof运算(DescriptionAttribute),
假);
如果(attributes.Length大于0)
返回属性[0]。说明;
返回value.ToString();
}
}
当我尝试使用LINQ的这个功能,即时通讯得到错误
VAR解析度= collection.AsQueryable()式。(p => p.UserID ==用户名).OrderByDescending (p => p.DateCreated)。选择(p =>新建MyClass的
{
日期= p.DateCreated.ToString(),
状态= p.Status.ToFrendlyString() ,
})采取(10).ToList();
如果我做在同一个类中的另一个功能,如
私人字符串MyStatusToString(MyEnum状态)
{
返回status.ToFrendlyString();
}
和改变我的LINQ到使用此功能,则一切正常。
错误
不能用于参数类型'DAL.MyEnum'的表达类型的方法'System.Enum'System.String ToFrendlyString(System.Enum)'
我不知道,你可以使用枚举
作为类型来换取这样的扩展方法 - 试试这个来代替。我已经采取了整理码了一点的自由,随意忽略这些变化:)
公共静态类EnumToFrendlyString
{
公共静态字符串ToFrendlyString< T>(这件T值)
,其中T:结构
{
返回value.GetEnumDescription();
}
公共静态字符串GetEnumDescription< T>(这件T值)
,其中T:结构
{
返回EnumDescriptionCache< T> .Descriptions [值];
}
私有静态类EnumDescriptionCache< T>
,其中T:结构
{
公共静态字典< T,串>说明=
Enum.GetValues(typeof运算(T))
.Cast< T>()
.ToDictionary(
值=>价值
值=> ; value.GetEnumDescriptionForCache());
}
私人静态字符串GetEnumDescriptionForCache< T>(这件T值)
,其中T:结构
{
如果(typeof运算(T)! IsEnum)
{
抛出新的ArgumentException(值)只有枚举使用;
}
VAR descriptionAttribute = typeof运算(T)
.GetField(value.ToString())
.GetCustomAttributes(typeof运算(DescriptionAttribute),FALSE)
.Cast< DescriptionAttribute>()
.FirstOrDefault();
回报率(descriptionAttribute!= NULL)
? descriptionAttribute.Description
:value.ToString();
}
}
我添加了一个私人的,泛型类缓存您枚举成员的描述,以避免大量的运行时使用反射的。它看起来和退出类第一缓存有点奇怪爆裂然后检索值,但它应该很好地工作:)
我的这个答案仍然适用 - 传递给字典枚举值未通过验证,所以你可以通过调用((MyEnum)5367372).ToFrendlyString()$ C $它崩溃C>。
I created Enum ToFrendlyString function for my enums, but i cant use in Linq.
public enum MyEnum
{
Queued = 0,
[Description("In progress")]
In_progress = 2,
[Description("No answer")]
No_answer = 6,
}
public static class EnumToFrendlyString
{
public static string ToFrendlyString(this Enum value)
{
return value.GetEnumDescription();
}
public static string GetEnumDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
var attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes.Length > 0)
return attributes[0].Description;
return value.ToString();
}
}
When i try to use this function in Linq, im getting error
var res = collection.AsQueryable().Where(p => p.UserID == UserID).OrderByDescending(p=> p.DateCreated).Select(p => new MyClass
{
Date = p.DateCreated.ToString(),
Status = p.Status.ToFrendlyString(),
}).Take(10).ToList();
If i make another function in same class, like
private string MyStatusToString(MyEnum status)
{
return status.ToFrendlyString();
}
and change my Linq to use this function, then everything works.
Error
Expression of type 'DAL.MyEnum' cannot be used for parameter of type 'System.Enum' of method 'System.String ToFrendlyString(System.Enum)'
I'm not sure you can use Enum
as the Type for an extension method like that - try this instead. I've taken the liberty of tidying the code up a bit, feel free to ignore those changes :)
public static class EnumToFrendlyString
{
public static string ToFrendlyString<T>(this T value)
where T : struct
{
return value.GetEnumDescription();
}
public static string GetEnumDescription<T>(this T value)
where T : struct
{
return EnumDescriptionCache<T>.Descriptions[value];
}
private static class EnumDescriptionCache<T>
where T : struct
{
public static Dictionary<T, string> Descriptions =
Enum.GetValues(typeof(T))
.Cast<T>()
.ToDictionary(
value => value,
value => value.GetEnumDescriptionForCache());
}
private static string GetEnumDescriptionForCache<T>(this T value)
where T : struct
{
if (!typeof(T).IsEnum)
{
throw new ArgumentException("Only use with enums", "value");
}
var descriptionAttribute = typeof(T)
.GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.Cast<DescriptionAttribute>()
.FirstOrDefault();
return (descriptionAttribute != null)
? descriptionAttribute.Description
: value.ToString();
}
}
I've added a private, generic class to cache the descriptions for your enum members to avoid lots of runtime use of reflection. It looks a bit odd popping in and out of the class to first cache then retrieve the values, but it should work fine :)
The warning I gave in this answer still applies - the enum value passed to the dictionary isn't validated, so you could crash it by calling ((MyEnum)5367372).ToFrendlyString()
.
这篇关于不能用于类型的参数类型'MyEnum'的表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!