Пример #1
0
        internal static Delegate InternalCreateDelegate(Type type, object firstArgument, MethodInfo method)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            RuntimeMethodInfo info = method as RuntimeMethodInfo;

            if (info == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method");
            }
            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.BindToMethodInfo(firstArgument, info.MethodHandle.GetMethodInfo(), info.GetDeclaringTypeInternal(), DelegateBindingFlags.RelaxedSignature | DelegateBindingFlags.SkipSecurityChecks))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(delegate2);
        }
Пример #2
0
        internal static Delegate?CreateDelegateInternal(
            RuntimeType rtType,
            RuntimeMethodInfo rtMethod,
            object?firstArgument,
            DelegateBindingFlags flags
            )
        {
            Delegate d = InternalAlloc(rtType);

            if (
                d.BindToMethodInfo(
                    firstArgument,
                    rtMethod,
                    rtMethod.GetDeclaringTypeInternal(),
                    flags
                    )
                )
            {
                return(d);
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
        internal static Delegate CreateDelegate(Type type, object target, RuntimeMethodHandle method)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method.IsNullHandle())
            {
                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.BindToMethodInfo(target, method.GetMethodInfo(), RuntimeMethodHandle.GetDeclaringType(method.GetMethodInfo()), DelegateBindingFlags.RelaxedSignature))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(delegate2);
        }
Пример #4
0
        // V1 API.
        public static Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(method is RuntimeMethodInfo))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method");
            }
            Type c = type.BaseType;

            if (c == null || c != typeof(MulticastDelegate))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }

            // Initialize the delegate...
            Delegate d = InternalAlloc(type.TypeHandle);

            // This API existed in v1/v1.1 and only expected to create closed
            // instance delegates. Constrain the call to BindToMethodInfo to
            // open delegates only for backwards compatibility. But we'll allow
            // relaxed signature checking and open static delegates because
            // there's no ambiguity there (the caller would have to explicitly
            // pass us a static method or a method with a non-exact signature
            // and the only change in behavior from v1.1 there is that we won't
            // fail the call).
            if (!d.BindToMethodInfo(null, method.MethodHandle, method.DeclaringType.TypeHandle,
                                    DelegateBindingFlags.OpenDelegateOnly |
                                    DelegateBindingFlags.RelaxedSignature))
            {
                if (throwOnBindFailure)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
                }
                d = null;
            }
            return(d);
        }
Пример #5
0
        //
        // internal implementation details (FCALLS and utilities)
        //

        // V2 internal API.
        internal static Delegate CreateDelegateNoSecurityCheck(
            Type type,
            object?target,
            RuntimeMethodHandle method
            )
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (method.IsNullHandle())
            {
                throw new ArgumentNullException(nameof(method));
            }

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

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

            // Initialize the method...
            Delegate d = InternalAlloc(rtType);

            // This is a new internal API added in Whidbey. Currently it's only
            // used by the dynamic method code to generate a wrapper delegate.
            // Allow flexible binding options since the target method is
            // unambiguously provided to us.

            if (
                !d.BindToMethodInfo(
                    target,
                    method.GetMethodInfo(),
                    RuntimeMethodHandle.GetDeclaringType(method.GetMethodInfo()),
                    DelegateBindingFlags.RelaxedSignature
                    )
                )
            {
                throw new ArgumentException(SR.Arg_DlgtTargMeth);
            }
            return(d);
        }
Пример #6
0
        [System.Security.SecurityCritical]  // auto-generated
        internal unsafe static Delegate CreateDelegateNoSecurityCheck(Type type, Object target, RuntimeMethodHandle method)
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Contract.EndContractBlock();

            if (method.IsNullHandle())
            {
                throw new ArgumentNullException("method");
            }

            RuntimeType rtType = type as RuntimeType;

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

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

            // Initialize the method...
            Delegate d = InternalAlloc(rtType);

            // This is a new internal API added in Whidbey. Currently it's only
            // used by the dynamic method code to generate a wrapper delegate.
            // Allow flexible binding options since the target method is
            // unambiguously provided to us.
            // <

            if (!d.BindToMethodInfo(target,
                                    method.GetMethodInfo(),
                                    RuntimeMethodHandle.GetDeclaringType(method.GetMethodInfo()),
                                    DelegateBindingFlags.RelaxedSignature | DelegateBindingFlags.SkipSecurityChecks))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(d);
        }
Пример #7
0
        // V2 API.
        public static Delegate CreateDelegate(Type type, Object firstArgument, MethodInfo method, bool throwOnBindFailure)
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(method is RuntimeMethodInfo))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method");
            }
            Type c = type.BaseType;

            if (c == null || c != typeof(MulticastDelegate))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }

            // Initialize the method...
            Delegate d = InternalAlloc(type.TypeHandle);

            if (!d.BindToMethodInfo(firstArgument, method.MethodHandle, method.DeclaringType.TypeHandle,
                                    DelegateBindingFlags.RelaxedSignature))
            {
                if (throwOnBindFailure)
                {
                    throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
                }
                d = null;
            }
            return(d);
        }
Пример #8
0
        public static Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            RuntimeMethodInfo info = method as RuntimeMethodInfo;

            if (info == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method");
            }
            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.BindToMethodInfo(null, info.MethodHandle.GetMethodInfo(), info.GetDeclaringTypeInternal().TypeHandle.GetRuntimeType(), DelegateBindingFlags.RelaxedSignature | DelegateBindingFlags.OpenDelegateOnly))
            {
                return(delegate2);
            }
            if (throwOnBindFailure)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(null);
        }
Пример #9
0
        // Caution: this method is intended for deserialization only, no security checks are performed.
        internal static Delegate InternalCreateDelegate(Type type, Object firstArgument, MethodInfo method)
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            Type c = type.BaseType;

            if (c == null || c != typeof(MulticastDelegate))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }

            // Initialize the method...
            Delegate d = InternalAlloc(type.TypeHandle);

            // This API is used by the formatters when deserializing a delegate.
            // They pass us the specific target method (that was already the
            // target in a valid delegate) so we should bind with the most
            // relaxed rules available (the result will never be ambiguous, it
            // just increases the chance of success with minor (compatible)
            // signature changes). We explicitly skip security checks here --
            // we're not really constructing a delegate, we're cloning an
            // existing instance which already passed its checks.
            if (!d.BindToMethodInfo(firstArgument, method.MethodHandle, method.DeclaringType.TypeHandle,
                                    DelegateBindingFlags.SkipSecurityChecks |
                                    DelegateBindingFlags.RelaxedSignature))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }

            return(d);
        }
Пример #10
0
        internal static Delegate UnsafeCreateDelegate(RuntimeType rtType, RuntimeMethodInfo rtMethod, Object firstArgument, DelegateBindingFlags flags)
        {
#if FEATURE_LEGACYNETCF
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
            {
                if (rtMethod.IsGenericMethodDefinition)
                {
                    throw new MissingMethodException(rtMethod.DeclaringType.FullName, rtMethod.Name);
                }
            }
#endif

            Delegate d = InternalAlloc(rtType);

            if (d.BindToMethodInfo(firstArgument, rtMethod, rtMethod.GetDeclaringTypeInternal(), flags))
            {
                return(d);
            }
            else
            {
                return(null);
            }
        }
Пример #11
0
        //
        // internal implementation details (FCALLS and utilities)
        //

        // V2 internal API.
        internal unsafe static Delegate CreateDelegate(Type type, Object target, RuntimeMethodHandle method)
        {
            // Validate the parameters.
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            if (method.IsNullHandle())
            {
                throw new ArgumentNullException("method");
            }

            Type c = type.BaseType;

            if (c == null || c != typeof(MulticastDelegate))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type");
            }

            // Initialize the method...
            Delegate d = InternalAlloc(type.TypeHandle);

            // This is a new internal API added in Whidbey. Currently it's only
            // used by the dynamic method code to generate a wrapper delegate.
            // Allow flexible binding options since the target method is
            // unambiguously provided to us.
            if (!d.BindToMethodInfo(target, method, method.GetDeclaringType(), DelegateBindingFlags.RelaxedSignature))
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(d);
        }
Пример #12
0
        public static Delegate CreateDelegate(Type type, object firstArgument, MethodInfo method, bool throwOnBindFailure)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!(type is RuntimeType))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }
            if (!(method is RuntimeMethodInfo))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method");
            }
            Type baseType = type.BaseType;

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

            if (delegate2.BindToMethodInfo(firstArgument, method.MethodHandle, method.DeclaringType.TypeHandle, DelegateBindingFlags.RelaxedSignature))
            {
                return(delegate2);
            }
            if (throwOnBindFailure)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth"));
            }
            return(null);
        }