【问题标题】:What is BindingFlags.Default equivalent to?BindingFlags.Default 等价于什么?
【发布时间】:2014-02-19 23:09:13
【问题描述】:

我记得在某处读过,当使用反射和接受BindingFlags 位掩码的GetMethod 的重载时,BindingFlags.Default 相当于BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance 或其他东西。谁能告诉我BindingFlags.Default 中具体包含哪些值? MSDN documentation 没有说,我也找不到任何地方的答案。

【问题讨论】:

    标签: c# reflection msdn bindingflags


    【解决方案1】:

    如果你“去定义”它,你会看到:

    public enum BindingFlags
    {
        // Summary:
        //     Specifies no binding flag.
        Default = 0,
        //
        // Summary:
        //     Specifies that the case of the member name should not be considered when
        //     binding.
        IgnoreCase = 1,
        //
        // Summary:
        //     Specifies that only members declared at the level of the supplied type's
        //     hierarchy should be considered. Inherited members are not considered.
        DeclaredOnly = 2,
        //
        // Summary:
        //     Specifies that instance members are to be included in the search.
        Instance = 4,
        //
        // Summary:
        //     Specifies that static members are to be included in the search.
        Static = 8,
        //
        // Summary:
        //     Specifies that public members are to be included in the search.
        Public = 16,
        //
        // Summary:
        //     Specifies that non-public members are to be included in the search.
        NonPublic = 32,
        //
        // Summary:
        //     Specifies that public and protected static members up the hierarchy should
        //     be returned. Private static members in inherited classes are not returned.
        //     Static members include fields, methods, events, and properties. Nested types
        //     are not returned.
        FlattenHierarchy = 64,
        //
        // Summary:
        //     Specifies that a method is to be invoked. This must not be a constructor
        //     or a type initializer.
        InvokeMethod = 256,
        //
        // Summary:
        //     Specifies that Reflection should create an instance of the specified type.
        //     Calls the constructor that matches the given arguments. The supplied member
        //     name is ignored. If the type of lookup is not specified, (Instance | Public)
        //     will apply. It is not possible to call a type initializer.
        CreateInstance = 512,
        //
        // Summary:
        //     Specifies that the value of the specified field should be returned.
        GetField = 1024,
        //
        // Summary:
        //     Specifies that the value of the specified field should be set.
        SetField = 2048,
        //
        // Summary:
        //     Specifies that the value of the specified property should be returned.
        GetProperty = 4096,
        //
        // Summary:
        //     Specifies that the value of the specified property should be set. For COM
        //     properties, specifying this binding flag is equivalent to specifying PutDispProperty
        //     and PutRefDispProperty.
        SetProperty = 8192,
        //
        // Summary:
        //     Specifies that the PROPPUT member on a COM object should be invoked. PROPPUT
        //     specifies a property-setting function that uses a value. Use PutDispProperty
        //     if a property has both PROPPUT and PROPPUTREF and you need to distinguish
        //     which one is called.
        PutDispProperty = 16384,
        //
        // Summary:
        //     Specifies that the PROPPUTREF member on a COM object should be invoked. PROPPUTREF
        //     specifies a property-setting function that uses a reference instead of a
        //     value. Use PutRefDispProperty if a property has both PROPPUT and PROPPUTREF
        //     and you need to distinguish which one is called.
        PutRefDispProperty = 32768,
        //
        // Summary:
        //     Specifies that types of the supplied arguments must exactly match the types
        //     of the corresponding formal parameters. Reflection throws an exception if
        //     the caller supplies a non-null Binder object, since that implies that the
        //     caller is supplying BindToXXX implementations that will pick the appropriate
        //     method.
        ExactBinding = 65536,
        //
        // Summary:
        //     Not implemented.
        SuppressChangeType = 131072,
        //
        // Summary:
        //     Returns the set of members whose parameter count matches the number of supplied
        //     arguments. This binding flag is used for methods with parameters that have
        //     default values and methods with variable arguments (varargs). This flag should
        //     only be used with System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]).
        OptionalParamBinding = 262144,
        //
        // Summary:
        //     Used in COM interop to specify that the return value of the member can be
        //     ignored.
        IgnoreReturn = 16777216,
    }
    

    所以,它看起来像BindingFlags.Default != BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance

    【讨论】:

    • Derp,我没想过这样做。
    • 我去看了类的定义,读到了,你怎么知道Default 相当于tho?
    • 它的值为0,所以它不等同于其他选项的任何组合。
    【解决方案2】:

    BindingFlags.Default 不指定绑定标志。由枚举的用户选择要做什么。 System.Type.GetMethods() 例如返回所有公共方法。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 2010-10-28
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2017-11-19
    相关资源
    最近更新 更多