diff options
author | Jiyoung Yun <jy910.yun@samsung.com> | 2017-04-13 14:17:19 +0900 |
---|---|---|
committer | Jiyoung Yun <jy910.yun@samsung.com> | 2017-04-13 14:17:19 +0900 |
commit | a56e30c8d33048216567753d9d3fefc2152af8ac (patch) | |
tree | 7e5d979695fc4a431740982eb1cfecc2898b23a5 /src/mscorlib/src/System/Delegate.cs | |
parent | 4b11dc566a5bbfa1378d6266525c281b028abcc8 (diff) | |
download | coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.gz coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.tar.bz2 coreclr-a56e30c8d33048216567753d9d3fefc2152af8ac.zip |
Imported Upstream version 2.0.0.11353upstream/2.0.0.11353
Diffstat (limited to 'src/mscorlib/src/System/Delegate.cs')
-rw-r--r-- | src/mscorlib/src/System/Delegate.cs | 220 |
1 files changed, 103 insertions, 117 deletions
diff --git a/src/mscorlib/src/System/Delegate.cs b/src/mscorlib/src/System/Delegate.cs index fb9dc4b110..de0ff6532c 100644 --- a/src/mscorlib/src/System/Delegate.cs +++ b/src/mscorlib/src/System/Delegate.cs @@ -2,8 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace System { - +namespace System +{ using System; using System.Reflection; using System.Runtime; @@ -18,7 +18,7 @@ namespace System { [Serializable] [ClassInterface(ClassInterfaceType.AutoDual)] [System.Runtime.InteropServices.ComVisible(true)] - public abstract class Delegate : ICloneable, ISerializable + public abstract class Delegate : ICloneable, ISerializable { // _target is the object we will invoke on internal Object _target; @@ -30,7 +30,7 @@ namespace System { // _methodPtr is a pointer to the method we will invoke // It could be a small thunk if this is a static or UM call internal IntPtr _methodPtr; - + // In the case of a static method passed to a delegate, this field stores // whatever _methodPtr would have stored: and _methodPtr points to a // small thunk which removes the "this" pointer before going on @@ -39,15 +39,15 @@ namespace System { // This constructor is called from the class generated by the // compiler generated code - protected Delegate(Object target,String method) + protected Delegate(Object target, String method) { if (target == null) throw new ArgumentNullException(nameof(target)); - + if (method == null) throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); - + // 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 @@ -57,19 +57,19 @@ namespace System { if (!BindToMethodName(target, (RuntimeType)target.GetType(), method, DelegateBindingFlags.InstanceMethodOnly | DelegateBindingFlags.ClosedDelegateOnly)) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); } - + // This constructor is called from a class to generate a // delegate based upon a static method name and the Type object // for the class defining the method. - protected unsafe Delegate(Type target,String method) + protected unsafe Delegate(Type target, String method) { if (target == null) throw new ArgumentNullException(nameof(target)); if (target.IsGenericType && target.ContainsGenericParameters) - throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), nameof(target)); + throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target)); if (method == null) throw new ArgumentNullException(nameof(method)); @@ -77,7 +77,7 @@ namespace System { RuntimeType rtTarget = target as RuntimeType; if (rtTarget == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(target)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target)); // This API existed in v1/v1.1 and only expected to create open // static delegates. Constrain the call to BindToMethodName to such @@ -91,7 +91,7 @@ namespace System { DelegateBindingFlags.OpenDelegateOnly | DelegateBindingFlags.CaselessMatching); } - + // Protect the default constructor so you can't build a delegate private Delegate() { @@ -108,7 +108,7 @@ namespace System { return DynamicInvokeImpl(args); } - protected virtual object DynamicInvokeImpl(object[] args) + protected virtual object DynamicInvokeImpl(object[] args) { RuntimeMethodHandleInternal method = new RuntimeMethodHandleInternal(GetInvokeMethod()); RuntimeMethodInfo invoke = (RuntimeMethodInfo)RuntimeType.GetMethodBase((RuntimeType)this.GetType(), method); @@ -116,16 +116,16 @@ namespace System { return invoke.UnsafeInvoke(this, BindingFlags.Default, null, args, null); } - + public override bool Equals(Object obj) - { + { if (obj == null || !InternalEqualTypes(this, obj)) - return false; + return false; - Delegate d = (Delegate) obj; + Delegate d = (Delegate)obj; // do an optimistic check first. This is hopefully cheap enough to be worth - if (_target == d._target && _methodPtr == d._methodPtr && _methodPtrAux == d._methodPtrAux) + if (_target == d._target && _methodPtr == d._methodPtr && _methodPtrAux == d._methodPtrAux) return true; // even though the fields were not all equals the delegates may still match @@ -154,7 +154,7 @@ namespace System { return false; */ - if (_methodPtrAux == d._methodPtrAux) + if (_methodPtrAux == d._methodPtrAux) return true; // fall through method handle check } @@ -163,9 +163,8 @@ namespace System { // if (_methodBase == null || d._methodBase == null || !(_methodBase is MethodInfo) || !(d._methodBase is MethodInfo)) return Delegate.InternalEqualMethodHandles(this, d); - else + else return _methodBase.Equals(d._methodBase); - } public override int GetHashCode() @@ -188,37 +187,37 @@ namespace System { if ((Object)a == null) // cast to object for a more efficient test return b; - return a.CombineImpl(b); + return a.CombineImpl(b); } - + public static Delegate Combine(params Delegate[] delegates) { if (delegates == null || delegates.Length == 0) return null; - + Delegate d = delegates[0]; for (int i = 1; i < delegates.Length; i++) - d = Combine(d,delegates[i]); - + d = Combine(d, delegates[i]); + return d; } - + public virtual Delegate[] GetInvocationList() { Delegate[] d = new Delegate[1]; d[0] = this; return d; } - + // This routine will return the method public MethodInfo Method - { - get - { + { + get + { return GetMethodImpl(); } } - + protected virtual MethodInfo GetMethodImpl() { if ((_methodBase == null) || !(_methodBase is MethodInfo)) @@ -226,12 +225,12 @@ namespace System { 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)) + if (RuntimeTypeHandle.IsGenericTypeDefinition(declaringType) || RuntimeTypeHandle.HasInstantiation(declaringType)) { bool isStatic = (RuntimeMethodHandle.GetAttributes(method) & MethodAttributes.Static) != (MethodAttributes)0; - if (!isStatic) + if (!isStatic) { - if (_methodPtrAux == (IntPtr)0) + if (_methodPtrAux == (IntPtr)0) { // 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 @@ -271,9 +270,9 @@ namespace System { } _methodBase = (MethodInfo)RuntimeType.GetMethodBase(declaringType, method); } - return (MethodInfo)_methodBase; + return (MethodInfo)_methodBase; } - + public Object Target { get @@ -281,19 +280,19 @@ namespace System { return GetTarget(); } } - - + + public static Delegate Remove(Delegate source, Delegate value) { if (source == null) return null; - + if (value == null) return source; - + if (!InternalEqualTypes(source, value)) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTypeMis")); - + throw new ArgumentException(SR.Arg_DlgtTypeMis); + return source.RemoveImpl(value); } @@ -302,7 +301,7 @@ namespace System { Delegate newDelegate = null; do - { + { newDelegate = source; source = Remove(source, value); } @@ -310,29 +309,29 @@ namespace System { return newDelegate; } - - protected virtual Delegate CombineImpl(Delegate d) + + protected virtual Delegate CombineImpl(Delegate d) { - throw new MulticastNotSupportedException(Environment.GetResourceString("Multicast_Combine")); + throw new MulticastNotSupportedException(SR.Multicast_Combine); } - + protected virtual Delegate RemoveImpl(Delegate d) { return (d.Equals(this)) ? null : this; } - - + + public virtual Object Clone() { return MemberwiseClone(); } - + // V1 API. public static Delegate CreateDelegate(Type type, Object target, String method) { return CreateDelegate(type, target, method, false, true); } - + // V1 API. public static Delegate CreateDelegate(Type type, Object target, String method, bool ignoreCase) { @@ -352,9 +351,9 @@ namespace System { RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),nameof(type)); + throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type)); Delegate d = InternalAlloc(rtType); // This API existed in v1/v1.1 and only expected to create closed @@ -371,19 +370,19 @@ namespace System { (ignoreCase ? DelegateBindingFlags.CaselessMatching : 0))) { if (throwOnBindFailure) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); d = null; } - + return d; } - + // V1 API. public static Delegate CreateDelegate(Type type, Type target, String method) { return CreateDelegate(type, target, method, false, true); } - + // V1 API. public static Delegate CreateDelegate(Type type, Type target, String method, bool ignoreCase) { @@ -394,11 +393,11 @@ namespace System { public static Delegate CreateDelegate(Type type, Type target, String method, bool ignoreCase, bool throwOnBindFailure) { if (type == null) - throw new ArgumentNullException(nameof(type)); + 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)); + throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target)); if (method == null) throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); @@ -406,11 +405,11 @@ namespace System { RuntimeType rtType = type as RuntimeType; RuntimeType rtTarget = target as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type)); if (rtTarget == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(target)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(target)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),nameof(type)); + 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 @@ -423,15 +422,15 @@ namespace System { (ignoreCase ? DelegateBindingFlags.CaselessMatching : 0))) { if (throwOnBindFailure) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); d = null; } - + return d; } - + // V1 API. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable + [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure) { // Validate the parameters. @@ -443,14 +442,14 @@ namespace System { RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type)); RuntimeMethodInfo rmi = method as RuntimeMethodInfo; if (rmi == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); + throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(method)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type)); + throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type)); // This API existed in v1/v1.1 and only expected to create closed // instance delegates. Constrain the call to BindToMethodInfo to @@ -469,11 +468,11 @@ namespace System { ref stackMark); if (d == null && throwOnBindFailure) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); return d; } - + // V2 API. public static Delegate CreateDelegate(Type type, Object firstArgument, MethodInfo method) { @@ -481,7 +480,7 @@ namespace System { } // V2 API. - [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable + [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod public static Delegate CreateDelegate(Type type, Object firstArgument, MethodInfo method, bool throwOnBindFailure) { // Validate the parameters. @@ -493,14 +492,14 @@ namespace System { RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type)); RuntimeMethodInfo rmi = method as RuntimeMethodInfo; if (rmi == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); + throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(method)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type)); + throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type)); // This API is new in Whidbey and allows the full range of delegate // flexability (open or closed delegates binding to static or @@ -516,7 +515,7 @@ namespace System { ref stackMark); if (d == null && throwOnBindFailure) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); return d; } @@ -525,18 +524,18 @@ namespace System { { if ((Object)d1 == null) return (Object)d2 == null; - + return d1.Equals(d2); } - - public static bool operator != (Delegate d1, Delegate d2) + + public static bool operator !=(Delegate d1, Delegate d2) { if ((Object)d1 == null) return (Object)d2 != null; - + return !d1.Equals(d2); } - + // // Implementation of ISerializable // @@ -563,11 +562,11 @@ namespace System { RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); - + throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type)); + if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type)); - + 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 @@ -579,7 +578,7 @@ namespace System { method.GetMethodInfo(), RuntimeMethodHandle.GetDeclaringType(method.GetMethodInfo()), DelegateBindingFlags.RelaxedSignature | DelegateBindingFlags.SkipSecurityChecks)) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); return d; } @@ -596,11 +595,11 @@ namespace System { RuntimeMethodInfo rtMethod = method as RuntimeMethodInfo; if (rtMethod == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); + throw new ArgumentException(SR.Argument_MustBeRuntimeMethodInfo, nameof(method)); if (!type.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type)); - + throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(type)); + // 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 @@ -614,7 +613,7 @@ namespace System { DelegateBindingFlags.RelaxedSignature); if (d == null) - throw new ArgumentException(Environment.GetResourceString("Arg_DlgtTargMeth")); + throw new ArgumentException(SR.Arg_DlgtTargMeth); return d; } @@ -629,19 +628,6 @@ namespace System { { Debug.Assert((flags & DelegateBindingFlags.SkipSecurityChecks) == 0); -#if FEATURE_APPX - bool nonW8PMethod = (rtMethod.InvocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0; - bool nonW8PType = (rtType.InvocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0; - if (nonW8PMethod || nonW8PType) - { - RuntimeAssembly caller = RuntimeAssembly.GetExecutingAssembly(ref stackMark); - if (caller != null && !caller.IsSafeForReflection()) - throw new InvalidOperationException( - Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext", - nonW8PMethod ? rtMethod.FullName : rtType.FullName)); - } -#endif - return UnsafeCreateDelegate(rtType, rtMethod, firstArgument, flags); } @@ -661,13 +647,13 @@ namespace System { [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern bool BindToMethodName(Object target, RuntimeType methodType, String method, DelegateBindingFlags flags); - + [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern bool BindToMethodInfo(Object target, IRuntimeMethodInfo method, RuntimeType methodType, DelegateBindingFlags flags); - + [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern static MulticastDelegate InternalAlloc(RuntimeType type); - + [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static MulticastDelegate InternalAllocLike(Delegate d); @@ -703,20 +689,20 @@ namespace System { } [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal extern static bool CompareUnmanagedFunctionPtrs (Delegate d1, Delegate d2); + internal extern static bool CompareUnmanagedFunctionPtrs(Delegate d1, Delegate d2); } // These flags effect the way BindToMethodInfo and BindToMethodName are allowed to bind a delegate to a target method. Their // values must be kept in sync with the definition in vm\comdelegate.h. internal enum DelegateBindingFlags { - StaticMethodOnly = 0x00000001, // Can only bind to static target methods - InstanceMethodOnly = 0x00000002, // Can only bind to instance (including virtual) methods - OpenDelegateOnly = 0x00000004, // Only allow the creation of delegates open over the 1st argument - ClosedDelegateOnly = 0x00000008, // Only allow the creation of delegates closed over the 1st argument - NeverCloseOverNull = 0x00000010, // A null target will never been considered as a possible null 1st argument - CaselessMatching = 0x00000020, // Use case insensitive lookup for methods matched by name - SkipSecurityChecks = 0x00000040, // Skip security checks (visibility, link demand etc.) - RelaxedSignature = 0x00000080, // Allow relaxed signature matching (co/contra variance) + StaticMethodOnly = 0x00000001, // Can only bind to static target methods + InstanceMethodOnly = 0x00000002, // Can only bind to instance (including virtual) methods + OpenDelegateOnly = 0x00000004, // Only allow the creation of delegates open over the 1st argument + ClosedDelegateOnly = 0x00000008, // Only allow the creation of delegates closed over the 1st argument + NeverCloseOverNull = 0x00000010, // A null target will never been considered as a possible null 1st argument + CaselessMatching = 0x00000020, // Use case insensitive lookup for methods matched by name + SkipSecurityChecks = 0x00000040, // Skip security checks (visibility, link demand etc.) + RelaxedSignature = 0x00000080, // Allow relaxed signature matching (co/contra variance) } } |