2010-12-10 36 views
23

智能感知告诉我“表达式不能包含匿名方法或lambda表达式。”真?我不知道这个强加的限制。它是否正确?我想我在这里寻找理智检查...C#方法属性不能包含Lambda表达式?

 
public delegate bool Bar(string s); 

[AttributeUsage(AttributeTargets.All)] 
public class Foo : Attribute 
{ 
    public readonly Bar bar; 

    public Foo(Bar bar) 
    { 
     this.bar = bar; 
    } 
} 

public class Usage 
{ 
    [Foo(b => b == "Hello World!")]  // IntelliSense Complains here 
    public Usage() 
    { 
    } 
} 
+3

我很好奇你会期望这样做。 – FrustratedWithFormsDesigner 2010-12-10 16:42:11

+2

我的目的只是为了某些配置目的而允许一个简单的映射。 – Didaxis 2010-12-10 16:45:48

+0

在这个例子中,什么会映射到什么? 'Usage'方法会映射到'Foo'属性中的lambda? lambda何时执行?这并不清楚,但这可能是一个有趣的想法...... – FrustratedWithFormsDesigner 2010-12-10 16:55:48

回答

27

是的,这是正确的。属性值被限制为以下类型的

  • 简单类型(布尔,字节,字符,短,整型,长整型,浮点,双)
  • 的System.Type
  • 枚举常数
  • 对象(的参数类型的对象的属性参数必须是上述类型之一的恒定值。)上述任何类型的
  • 一维阵列

参考:http://msdn.microsoft.com/en-us/library/aa288454(VS.71).aspx

+6

谢谢。这太糟糕了。 – Didaxis 2010-12-10 16:44:51

+0

呃。在那里,我想我终于想出了一个解决方案,我发现INotifyPropertyChanged可以接受。好吧,回到制图板。 – tobriand 2015-03-29 21:50:09