简体   繁体   English

C#构造函数基本关键字

[英]C# Constructor base keyword

I was poking into Microsoft.Practices.Prism assembly using reflector and came across the following definition for the constructor of DelagateCommand: 我正在使用反射器插入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);
    }
}

This code does not compile since : base(action, func) points to the first two variables in the ctor. 这段代码无法编译,因为: base(action, func)指向ctor中的前两个变量。

Is it possible to replicate this kind of behavior? 是否可以复制这种行为? perhaps by the use of anon methods? 也许通过使用匿名方法?

Thank you in advance for your input. 预先感谢您的输入。

Reflector IL for this method: 此方法的反射器IL:

.method public hidebysig specialname rtspecialname instance void .ctor(class [mscorlib]System.Action executeMethod, class [mscorlib]System.Func`1<bool> canExecuteMethod) cil managed
{
    .maxstack 5
    .locals init (
        [0] class [mscorlib]System.Action`1<object> action,
        [1] class [mscorlib]System.Func`2<object, bool> func,
        [2] class Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6 class2)
    L_0000: ldnull 
    L_0001: stloc.0 
    L_0002: ldnull 
    L_0003: stloc.1 
    L_0004: newobj instance void Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::.ctor()
    L_0009: stloc.2 
    L_000a: ldloc.2 
    L_000b: ldarg.1 
    L_000c: stfld class [mscorlib]System.Action Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::executeMethod
    L_0011: ldloc.2 
    L_0012: ldarg.2 
    L_0013: stfld class [mscorlib]System.Func`1<bool> Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::canExecuteMethod
    L_0018: ldarg.0 
    L_0019: ldloc.0 
    L_001a: brtrue.s L_0029
    L_001c: ldloc.2 
    L_001d: ldftn instance void Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::<.ctor>b__2(object)
    L_0023: newobj instance void [mscorlib]System.Action`1<object>::.ctor(object, native int)
    L_0028: stloc.0 
    L_0029: ldloc.0 
    L_002a: ldloc.1 
    L_002b: brtrue.s L_003a
    L_002d: ldloc.2 
    L_002e: ldftn instance bool Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::<.ctor>b__3(object)
    L_0034: newobj instance void [mscorlib]System.Func`2<object, bool>::.ctor(object, native int)
    L_0039: stloc.1 
    L_003a: ldloc.1 
    L_003b: call instance void Microsoft.Practices.Prism.Commands.DelegateCommandBase::.ctor(class [mscorlib]System.Action`1<object>, class [mscorlib]System.Func`2<object, bool>)
    L_0040: ldloc.2 
    L_0041: ldfld class [mscorlib]System.Action Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::executeMethod
    L_0046: brfalse.s L_0050
    L_0048: ldloc.2 
    L_0049: ldfld class [mscorlib]System.Func`1<bool> Microsoft.Practices.Prism.Commands.DelegateCommand/<>c__DisplayClass6::canExecuteMethod
    L_004e: brtrue.s L_0060
    L_0050: ldstr "executeMethod"
    L_0055: call string Microsoft.Practices.Prism.Properties.Resources::get_DelegateCommandDelegatesCannotBeNull()
    L_005a: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string, string)
    L_005f: throw 
    L_0060: ret 
}

Also after looking into the actual source code at codeplex the method definition is as follows: 同样,在codeplex上查看了实际的源代码之后,方法定义如下:

 public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)
            : base((o) => executeMethod((T)o), (o) => canExecuteMethod((T)o))
        {
            if (executeMethod == null || canExecuteMethod == null)
                throw new ArgumentNullException("executeMethod", Resources.DelegateCommandDelegatesCannotBeNull);

#if !WINDOWS_PHONE
            Type genericType = typeof(T);

            // DelegateCommand allows object or Nullable<>.  
            // note: Nullable<> is a struct so we cannot use a class constraint.
            if (genericType.IsValueType)
            {
                if ((!genericType.IsGenericType) || (!typeof(Nullable<>).IsAssignableFrom(genericType.GetGenericTypeDefinition())))
                {
                    throw new InvalidCastException(Resources.DelegateCommandInvalidGenericPayloadType);
                }
            }
#endif
        }

I think this is probably just a reflector bug, trying to represent the way that the backing delegate for lambdas are cached. 我认为这可能只是一个反射器错误,试图表示对lambda的后备委托进行缓存的方式。 The real code is most likely: 真正的代码很可能是:

: base(o => executeMethod(), o => canExecuteMethod())

Do you have the IL to hand? 你有IL吗?


Edit: Hmmm... I can't repro that. 编辑:嗯...我不能复制。 There are two other options, though: in both C++ and IL you can do pretty much anything. 但是,还有其他两种选择:在C ++和IL中,您几乎可以做任何事情。 That code looks... funky, though. 该代码看起来...很时髦。

IMHO, it's Reflector's fail. 恕我直言,这是Reflector的失败。 Why don't you look into source code of Prism? 您为什么不查看Prism的源代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM