protected virtual MethodInfo GetMethodImpl() { if ((this._methodBase == null) || !(this._methodBase is MethodInfo)) { IRuntimeMethodInfo method = this.FindMethodHandle(); RuntimeType declaringType = RuntimeMethodHandle.GetDeclaringType(method); if ((RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType)) && ((RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static) == MethodAttributes.PrivateScope)) { if (!(this._methodPtrAux == IntPtr.Zero)) { declaringType = (RuntimeType)base.GetType().GetMethod("Invoke").GetParameters()[0].ParameterType; } else { Type type = this._target.GetType(); Type genericTypeDefinition = declaringType.GetGenericTypeDefinition(); while (true) { if (type.IsGenericType && (type.GetGenericTypeDefinition() == genericTypeDefinition)) { break; } type = type.BaseType; } declaringType = type as RuntimeType; } } this._methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method); } return((MethodInfo)this._methodBase); }
protected virtual MethodInfo GetMethodImpl() { if (_methodBase == null) { RuntimeMethodHandle method = FindMethodHandle(); RuntimeTypeHandle declaringType = method.GetDeclaringType(); // need a proper declaring type instance method on a generic type if (declaringType.IsGenericTypeDefinition() || declaringType.HasInstantiation()) { bool isStatic = (method.GetAttributes() & MethodAttributes.Static) != (MethodAttributes)0; if (!isStatic) { if (_methodPtrAux == (IntPtr)0) { declaringType = _target.GetType().TypeHandle; // may throw NullRefException if the this is null but that's ok } else { // it's an open one, need to fetch the first arg of the instantiation MethodInfo invoke = this.GetType().GetMethod("Invoke"); declaringType = invoke.GetParameters()[0].ParameterType.TypeHandle; } } } _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method); } return((MethodInfo)_methodBase); }
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); }
protected override MethodInfo GetMethodImpl() { if (this._invocationCount != (IntPtr)0 && this._invocationList != null) { object[] objArray = this._invocationList as object[]; if (objArray != null) { int index = (int)this._invocationCount - 1; return(((Delegate)objArray[index]).Method); } MulticastDelegate multicastDelegate = this._invocationList as MulticastDelegate; if (multicastDelegate != null) { return(multicastDelegate.GetMethodImpl()); } } else if (this.IsUnmanagedFunctionPtr()) { if (this._methodBase == null || !(this._methodBase is MethodInfo)) { IRuntimeMethodInfo methodHandle = this.FindMethodHandle(); RuntimeType runtimeType = RuntimeMethodHandle.GetDeclaringType(methodHandle); if (RuntimeTypeHandle.IsGenericTypeDefinition(runtimeType) || RuntimeTypeHandle.HasInstantiation(runtimeType)) { runtimeType = this.GetType() as RuntimeType; } this._methodBase = (object)(MethodInfo)RuntimeType.GetMethodBase(runtimeType, methodHandle); } return((MethodInfo)this._methodBase); } return(base.GetMethodImpl()); }
internal static RuntimeType GetDeclaringType(IRuntimeMethodInfo method) { RuntimeType declaringType = RuntimeMethodHandle.GetDeclaringType(method.Value); GC.KeepAlive(method); return(declaringType); }
protected virtual MethodInfo GetMethodImpl() { if ((_methodBase == null) || !(_methodBase is MethodInfo)) { IRuntimeMethodInfo method = FindMethodHandle(); RuntimeType? declaringType = RuntimeMethodHandle.GetDeclaringType(method); // need a proper declaring type instance method on a generic type if (RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType)) { bool isStatic = (RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static) != (MethodAttributes)0; if (!isStatic) { if (_methodPtrAux == IntPtr.Zero) { // The target may be of a derived type that doesn't have visibility onto the // target method. We don't want to call RuntimeType.GetMethodBase below with that // or reflection can end up generating a MethodInfo where the ReflectedType cannot // see the MethodInfo itself and that breaks an important invariant. But the // target type could include important generic type information we need in order // to work out what the exact instantiation of the method's declaring type is. So // we'll walk up the inheritance chain (which will yield exactly instantiated // types at each step) until we find the declaring type. Since the declaring type // we get from the method is probably shared and those in the hierarchy we're // walking won't be we compare using the generic type definition forms instead. Type?currentType = _target !.GetType(); Type targetType = declaringType.GetGenericTypeDefinition(); while (currentType != null) { if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == targetType) { declaringType = currentType as RuntimeType; break; } currentType = currentType.BaseType; } // RCWs don't need to be "strongly-typed" in which case we don't find a base type // that matches the declaring type of the method. This is fine because interop needs // to work with exact methods anyway so declaringType is never shared at this point. Debug.Assert(currentType != null || _target.GetType().IsCOMObject, "The class hierarchy should declare the method"); } else { // it's an open one, need to fetch the first arg of the instantiation MethodInfo invoke = this.GetType().GetMethod("Invoke") !; declaringType = (RuntimeType)invoke.GetParameters()[0].ParameterType; } } } _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method) !; } return((MethodInfo)_methodBase); }
// // 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); }
[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); }
protected override MethodInfo GetMethodImpl() { if (_invocationCount != (IntPtr)0 && _invocationList != null) { // multicast case Object[] invocationList = _invocationList as Object[]; if (invocationList != null) { int index = (int)_invocationCount - 1; return(((Delegate)invocationList[index]).Method); } MulticastDelegate innerDelegate = _invocationList as MulticastDelegate; if (innerDelegate != null) { // must be a secure/wrapper delegate return(innerDelegate.GetMethodImpl()); } } else if (IsUnmanagedFunctionPtr()) { // we handle unmanaged function pointers here because the generic ones (used for WinRT) would otherwise // be treated as open delegates by the base implementation, resulting in failure to get the MethodInfo if ((_methodBase == null) || !(_methodBase is MethodInfo)) { IRuntimeMethodInfo method = FindMethodHandle(); RuntimeType declaringType = RuntimeMethodHandle.GetDeclaringType(method); // need a proper declaring type instance method on a generic type if (RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType)) { // we are returning the 'Invoke' method of this delegate so use this.GetType() for the exact type RuntimeType reflectedType = GetType() as RuntimeType; declaringType = reflectedType; } _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method); } return((MethodInfo)_methodBase); } // Otherwise, must be an inner delegate of a SecureDelegate of an open virtual method. In that case, call base implementation return(base.GetMethodImpl()); }
// this method is a big perf hit, so don't call unnecessarily internal static MethodInfo GetMethodInfo(RuntimeMethodHandle rmh) { if(rmh.IsNullHandle()) return null; #if _DEBUG try { #endif // Assert here because reflection will check grants and if we fail the check, // there will be an infinite recursion that overflows the stack. PermissionSet.s_fullTrust.Assert(); RuntimeTypeHandle rth = rmh.GetDeclaringType(); return(System.RuntimeType.GetMethodBase(rth, rmh) as MethodInfo); #if _DEBUG } catch(Exception) { return null; } #endif }
// // 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); }
internal static Delegate CreateDelegate(Type type, object target, RuntimeMethodHandle method) { 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 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(target, method, method.GetDeclaringType(), DelegateBindingFlags.RelaxedSignature)) { throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); } return delegate2; }
// // 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; }