【发布时间】:2015-02-10 18:49:36
【问题描述】:
我有一个名为 Mod 的静态类和一个名为 Map 的静态函数:
public static class Mod<TModel>
{
public static string Map<TValue>(Expression<Func<TModel, TValue>> expression)
{
throw new Exception("Not implemented");
}
}
我可以执行这样的方法(没问题):
var test = Mod<string>.Map<string>(x => x.ToLower());
我的构造函数上有一个带有字符串参数的属性:
public class MyTestAttribute : Attribute
{
public MyTestAttribute(string label)
{
}
}
为什么我不能这样称呼它?
[Attributes.MyTest(Attributes.Mod<string>.Map<string>(x => x.ToLower()))]
public string SomeProperty { get; set; }
我收到错误“表达式不能包含匿名方法或 lambda 表达式”。但是为什么呢?
我很清楚 lambda 表达式在属性中不起作用...但是为什么带有 lambda 的静态类/静态方法不能作为属性的参数(...如果这是我遇到的问题)?
谢谢
【问题讨论】:
标签: c# custom-attributes lambda