小编Ioa*_*pas的帖子

C#构造函数base关键字

我正在使用反射器进入Microsoft.Practices.Prism程序集,并为DelagateCommand的构造函数遇到以下定义:

public DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod) 
    : base(action, func)
{
    Action<object> action = null;
    Func<object, bool> func = null;
    if (action == null)
    {
        action = o => executeMethod();
    }
    if (func == null)
    {
        func = o => canExecuteMethod();
    }
    if ((executeMethod == null) || (canExecuteMethod == null))
    {
        throw new ArgumentNullException(
            "executeMethod", 
            Resources.DelegateCommandDelegatesCannotBeNull);
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译,因为它: base(action, func)指向ctor中的前两个变量.

是否有可能复制这种行为?也许是通过使用匿名方法?

提前感谢您的意见.

Reflector IL用于此方法:

.method public hidebysig specialname rtspecialname instance void .ctor(class [mscorlib]System.Action executeMethod, class [mscorlib]System.Func`1<bool> canExecuteMethod) cil …
Run Code Online (Sandbox Code Playgroud)

c# prism-4

5
推荐指数
1
解决办法
568
查看次数

标签 统计

c# ×1

prism-4 ×1