C# 为什么func<&燃气轮机;现场初始化器静态?

C# 为什么func<&燃气轮机;现场初始化器静态?,c#,wpf,func,idataerrorinfo,C#,Wpf,Func,Idataerrorinfo,在实现IDataErrorInfo时,以下代码编译并正常工作: public string Error { get { return null; } } public string this[string columnName] { get { switch (columnName) {

在实现IDataErrorInfo时,以下代码编译并正常工作:

public string Error
        {
            get { return null; }
        }

        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "MemberId":            return validate(MemberId,           0, RegexLibrary.NonEmptyRegex);
                    case "PolicyType":          return validate(PolicyType,         1, RegexLibrary.NonEmptyRegex);
                    case "EffectiveDateFrom":   return validate(EffectiveDateFrom,  2, RegexLibrary.DateRegex);
                    case "EffectiveDateTo":     return validate(EffectiveDateTo,    3, RegexLibrary.DateRegex);
                    case "Phone":               return validate(Phone,              4, RegexLibrary.PhoneRegex);
                    case "CompanyName":         return validate(CompanyName,        5, RegexLibrary.NonEmptyRegex);
                }
                // string.Empty is no error.
                return string.Empty;
            }
        }

 public string validate(string column, int position, string regex)
        {
            string invalid = string.Format("{0} is invalid.", column);
            if (column == null)
            {
                SetErrorStatus(1, position);
                return invalid;
            }

            Match match = Regex.Match(column, regex);
            if (!match.Success)
            {
                SetErrorStatus(1, position);
                return invalid;
            }

            SetErrorStatus(0, position);
            return string.Empty;
        }
但是,如果将validate(…)定义为如下函数:

Func<string, int, string, string> validate = (column, position, regex) =>
        {
            string invalid = string.Format("{0} is invalid.", column);
            if (column == null)
            {
                SetErrorStatus(1, position);
                return invalid;
            }

            Match match = Regex.Match(column, regex);
            if (!match.Success)
            {
                SetErrorStatus(1, position);
                return invalid;
            }

            SetErrorStatus(0, position);
            return string.Empty;

        };
Func validate=(列、位置、正则表达式)=>
{
字符串无效=字符串。格式(“{0}无效。”,列);
if(列==null)
{
设置错误状态(1,位置);
返回无效;
}
Match=Regex.Match(列,Regex);
如果(!match.Success)
{
设置错误状态(1,位置);
返回无效;
}
设置错误状态(0,位置);
返回字符串。空;
};
编译器将validate(…)func定义为static。为什么?


TIA

在这种情况下,
验证
不是一种方法,而是一个字段。该字段是实例成员。分配给该字段的对象是引用匿名方法的委托。该方法没有理由成为实例成员,因为它不引用当前实例。因此,您有一个引用静态方法的实例字段。

编译器在调用类而不是实例类上生成一个
静态
方法这一事实是编译器的一个实现细节,该细节可能会发生更改,并且随着Roslyn的发布而实际发生了更改。您现在可以看到,编译器甚至为非捕获委托生成一个显示类


我不明白这与在类中声明任何内容为
静态
有什么关系。

验证
lambda不使用
,因此,没有理由为匿名方法生成实例方法。@PetSerAl func validate可以强制为非静态吗?使委托的支持方法成为非静态的有什么意义?您总是可以自己编写方法并从中创建委托。顺便说一句,由于您不能在字段初始值设定项中引用
,因此您不能在字段初始值设定项中从当前实例的非静态方法创建委托,它必须在构造函数中完成。@PetSerAl Initialy,在初始值设定项中使用func validate时,它迫使我从函数中进行静态引用。。。导致实例属性也是静态的……这是我不想做的。:)谢谢。因此,您必须在构造函数中初始化
validate
字段,而不是字段初始值设定项。func validate是否可以设置为非静态?谢谢。@jmcilhinney回答得好,我可以知道这对你来说是C#的概念知识,还是你也是通过艰苦的方式学会的?像艾伦一样,直到今天我才注意到这一点。你让我开始质疑自己。这个方法没有理由成为实例成员,因为它没有引用当前实例。谁说的?这是编译器的一个实现细节,顺便说一句,它已经改变了。真的吗?你们所有人都注意到了实现层吗?上帝我真的需要改变我的学习态度。