Пример #1
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static Delegate CreateDelegate(Type type, Type target, String method, bool ignoreCase, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (target.IsGenericType && target.ContainsGenericParameters)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), nameof(target));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            Contract.EndContractBlock();

            RuntimeType rtType   = type as RuntimeType;
            RuntimeType rtTarget = target as RuntimeType;

            if (rtType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type));
            }
            if (rtTarget == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(target));
            }
            if (!rtType.IsDelegate())
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type));
            }

            Delegate d = InternalAlloc(rtType);

            // This API existed in v1/v1.1 and only expected to create open
            // static delegates. Constrain the call to BindToMethodName to such
            // and don't allow relaxed signature matching (which could make the
            // choice of target method ambiguous) for backwards compatibility.
            if (!d.BindToMethodName(null, rtTarget, method,
                                    DelegateBindingFlags.StaticMethodOnly |
                                    DelegateBindingFlags.OpenDelegateOnly |
                                    (ignoreCase ? DelegateBindingFlags.CaselessMatching : 0)))
            {
                if (throwOnBindFailure)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
                }
                d = null;
            }

            return(d);
        }
Пример #2
0
        // V1 API.
        public static Delegate?CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (target.ContainsGenericParameters)
            {
                throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            if (!(type is RuntimeType rtType))
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
            }
            if (!(target is RuntimeType rtTarget))
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target));
            }

            if (!rtType.IsDelegate())
            {
                throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type));
            }

            Delegate d = InternalAlloc(rtType);

            // This API existed in v1/v1.1 and only expected to create open
            // static delegates. Constrain the call to BindToMethodName to such
            // and don't allow relaxed signature matching (which could make the
            // choice of target method ambiguous) for backwards compatibility.
            if (!d.BindToMethodName(null, rtTarget, method,
                                    DelegateBindingFlags.StaticMethodOnly |
                                    DelegateBindingFlags.OpenDelegateOnly |
                                    (ignoreCase ? DelegateBindingFlags.CaselessMatching : 0)))
            {
                if (throwOnBindFailure)
                {
                    throw new ArgumentException(SR.Arg_DlgtTargMeth);
                }

                return(null);
            }

            return(d);
        }
Пример #3
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public static Delegate CreateDelegate(Type type, Object target, String method, bool ignoreCase, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            Contract.EndContractBlock();

            RuntimeType rtType = type as RuntimeType;

            if (rtType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type));
            }
            if (!rtType.IsDelegate())
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type));
            }

            Delegate d = InternalAlloc(rtType);

            // This API existed in v1/v1.1 and only expected to create closed
            // instance delegates. Constrain the call to BindToMethodName to such
            // and don't allow relaxed signature matching (which could make the
            // choice of target method ambiguous) for backwards compatibility.
            // We never generate a closed over null delegate and this is
            // actually enforced via the check on target above, but we pass
            // NeverCloseOverNull anyway just for clarity.
            if (!d.BindToMethodName(target, (RuntimeType)target.GetType(), method,
                                    DelegateBindingFlags.InstanceMethodOnly |
                                    DelegateBindingFlags.ClosedDelegateOnly |
                                    DelegateBindingFlags.NeverCloseOverNull |
                                    (ignoreCase ? DelegateBindingFlags.CaselessMatching : 0)))
            {
                if (throwOnBindFailure)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
                }
                d = null;
            }

            return(d);
        }
Пример #4
0
        public static Delegate CreateDelegate(Type type, Type target, string method, bool ignoreCase, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (target.IsGenericType && target.ContainsGenericParameters)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), "target");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            if (!(target is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target");
            }
            Type baseType = type.BaseType;

            if ((baseType == null) || (baseType != typeof(MulticastDelegate)))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }
            Delegate delegate2 = InternalAlloc(type.TypeHandle.GetRuntimeType());

            if (delegate2.BindToMethodName(null, target.TypeHandle.GetRuntimeType(), method, (DelegateBindingFlags.OpenDelegateOnly | DelegateBindingFlags.StaticMethodOnly) | (ignoreCase ? DelegateBindingFlags.CaselessMatching : ((DelegateBindingFlags)0))))
            {
                return(delegate2);
            }
            if (throwOnBindFailure)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(null);
        }
Пример #5
0
        public static Delegate CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            Type baseType = type.BaseType;

            if ((baseType == null) || (baseType != typeof(MulticastDelegate)))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }
            Delegate delegate2 = InternalAlloc(type.TypeHandle.GetRuntimeType());

            if (delegate2.BindToMethodName(target, (RuntimeType)target.GetType(), method, (DelegateBindingFlags.NeverCloseOverNull | DelegateBindingFlags.ClosedDelegateOnly | DelegateBindingFlags.InstanceMethodOnly) | (ignoreCase ? DelegateBindingFlags.CaselessMatching : ((DelegateBindingFlags)0))))
            {
                return(delegate2);
            }
            if (throwOnBindFailure)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(null);
        }