diff options
Diffstat (limited to 'src/mscorlib/src/System/Delegate.cs')
-rw-r--r-- | src/mscorlib/src/System/Delegate.cs | 107 |
1 files changed, 38 insertions, 69 deletions
diff --git a/src/mscorlib/src/System/Delegate.cs b/src/mscorlib/src/System/Delegate.cs index 110555423c..bca88c18e9 100644 --- a/src/mscorlib/src/System/Delegate.cs +++ b/src/mscorlib/src/System/Delegate.cs @@ -12,6 +12,7 @@ namespace System { using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.Versioning; + using System.Diagnostics; using System.Diagnostics.Contracts; [Serializable] @@ -20,36 +21,31 @@ namespace System { public abstract class Delegate : ICloneable, ISerializable { // _target is the object we will invoke on - [System.Security.SecurityCritical] internal Object _target; // MethodBase, either cached after first request or assigned from a DynamicMethod // For open delegates to collectible types, this may be a LoaderAllocator object - [System.Security.SecurityCritical] internal Object _methodBase; // _methodPtr is a pointer to the method we will invoke // It could be a small thunk if this is a static or UM call - [System.Security.SecurityCritical] 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 // to _methodPtrAux. - [System.Security.SecurityCritical] internal IntPtr _methodPtrAux; // This constructor is called from the class generated by the // compiler generated code - [System.Security.SecuritySafeCritical] // auto-generated protected Delegate(Object target,String method) { if (target == null) - throw new ArgumentNullException("target"); + throw new ArgumentNullException(nameof(target)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); // This API existed in v1/v1.1 and only expected to create closed @@ -67,22 +63,21 @@ namespace System { // 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. - [System.Security.SecuritySafeCritical] // auto-generated protected unsafe Delegate(Type target,String method) { if (target == null) - throw new ArgumentNullException("target"); + throw new ArgumentNullException(nameof(target)); if (target.IsGenericType && target.ContainsGenericParameters) - throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), "target"); + throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), nameof(target)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); RuntimeType rtTarget = target as RuntimeType; if (rtTarget == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target"); + throw new ArgumentException(Environment.GetResourceString("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 @@ -113,7 +108,6 @@ namespace System { return DynamicInvokeImpl(args); } - [System.Security.SecuritySafeCritical] // auto-generated protected virtual object DynamicInvokeImpl(object[] args) { RuntimeMethodHandleInternal method = new RuntimeMethodHandleInternal(GetInvokeMethod()); @@ -123,7 +117,6 @@ namespace System { } - [System.Security.SecuritySafeCritical] // auto-generated public override bool Equals(Object obj) { if (obj == null || !InternalEqualTypes(this, obj)) @@ -227,7 +220,6 @@ namespace System { } } - [System.Security.SecuritySafeCritical] // auto-generated protected virtual MethodInfo GetMethodImpl() { if ((_methodBase == null) || !(_methodBase is MethodInfo)) @@ -292,7 +284,6 @@ namespace System { } - [System.Security.SecuritySafeCritical] // auto-generated public static Delegate Remove(Delegate source, Delegate value) { if (source == null) @@ -350,22 +341,21 @@ namespace System { } // V1 API. - [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("type"); + throw new ArgumentNullException(nameof(type)); if (target == null) - throw new ArgumentNullException("target"); + throw new ArgumentNullException(nameof(target)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); + 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 @@ -402,27 +392,26 @@ namespace System { } // V1 API. - [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("type"); + throw new ArgumentNullException(nameof(type)); if (target == null) - throw new ArgumentNullException("target"); + throw new ArgumentNullException(nameof(target)); if (target.IsGenericType && target.ContainsGenericParameters) - throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), "target"); + throw new ArgumentException(Environment.GetResourceString("Arg_UnboundGenParam"), nameof(target)); if (method == null) - throw new ArgumentNullException("method"); + 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"), "type"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); if (rtTarget == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "target"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(target)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); + 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 @@ -443,27 +432,26 @@ namespace System { } // V1 API. - [System.Security.SecuritySafeCritical] [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public static Delegate CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure) { // Validate the parameters. if (type == null) - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); RuntimeMethodInfo rmi = method as RuntimeMethodInfo; if (rmi == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type"); + throw new ArgumentException(Environment.GetResourceString("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 @@ -494,27 +482,26 @@ namespace System { } // V2 API. - [System.Security.SecuritySafeCritical] [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable public static Delegate CreateDelegate(Type type, Object firstArgument, MethodInfo method, bool throwOnBindFailure) { // Validate the parameters. if (type == null) - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); RuntimeMethodInfo rmi = method as RuntimeMethodInfo; if (rmi == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), "type"); + throw new ArgumentException(Environment.GetResourceString("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 @@ -555,7 +542,6 @@ namespace System { // Implementation of ISerializable // - [System.Security.SecurityCritical] public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); @@ -566,23 +552,22 @@ namespace System { // V2 internal API. // This is Critical because it skips the security check when creating the delegate. - [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"); + throw new ArgumentNullException(nameof(type)); Contract.EndContractBlock(); if (method.IsNullHandle()) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); RuntimeType rtType = type as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), "type"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"), nameof(type)); if (!rtType.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); + throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"), nameof(type)); // Initialize the method... Delegate d = InternalAlloc(rtType); @@ -600,23 +585,22 @@ namespace System { } // Caution: this method is intended for deserialization only, no security checks are performed. - [System.Security.SecurityCritical] // auto-generated internal static Delegate CreateDelegateNoSecurityCheck(RuntimeType type, Object firstArgument, MethodInfo method) { // Validate the parameters. if (type == null) - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); if (method == null) - throw new ArgumentNullException("method"); + throw new ArgumentNullException(nameof(method)); Contract.EndContractBlock(); RuntimeMethodInfo rtMethod = method as RuntimeMethodInfo; if (rtMethod == null) - throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), "method"); + throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeMethodInfo"), nameof(method)); if (!type.IsDelegate()) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDelegate"),"type"); + throw new ArgumentException(Environment.GetResourceString("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 @@ -642,10 +626,9 @@ namespace System { return CreateDelegate(type, method, true); } - [System.Security.SecuritySafeCritical] internal static Delegate CreateDelegateInternal(RuntimeType rtType, RuntimeMethodInfo rtMethod, Object firstArgument, DelegateBindingFlags flags, ref StackCrawlMark stackMark) { - Contract.Assert((flags & DelegateBindingFlags.SkipSecurityChecks) == 0); + Debug.Assert((flags & DelegateBindingFlags.SkipSecurityChecks) == 0); #if FEATURE_APPX bool nonW8PMethod = (rtMethod.InvocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0; @@ -663,7 +646,6 @@ namespace System { return UnsafeCreateDelegate(rtType, rtMethod, firstArgument, flags); } - [System.Security.SecurityCritical] internal static Delegate UnsafeCreateDelegate(RuntimeType rtType, RuntimeMethodInfo rtMethod, Object firstArgument, DelegateBindingFlags flags) { Delegate d = InternalAlloc(rtType); @@ -678,62 +660,49 @@ namespace System { // internal implementation details (FCALLS and utilities) // - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern bool BindToMethodName(Object target, RuntimeType methodType, String method, DelegateBindingFlags flags); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern bool BindToMethodInfo(Object target, IRuntimeMethodInfo method, RuntimeType methodType, DelegateBindingFlags flags); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern static MulticastDelegate InternalAlloc(RuntimeType type); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static MulticastDelegate InternalAllocLike(Delegate d); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static bool InternalEqualTypes(object a, object b); // Used by the ctor. Do not call directly. // The name of this function will appear in managed stacktraces as delegate constructor. - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] private extern void DelegateConstruct(Object target, IntPtr slot); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern IntPtr GetMulticastInvoke(); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern IntPtr GetInvokeMethod(); [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern IRuntimeMethodInfo FindMethodHandle(); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static bool InternalEqualMethodHandles(Delegate left, Delegate right); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern IntPtr AdjustTarget(Object target, IntPtr methodPtr); - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern IntPtr GetCallStub(IntPtr methodPtr); - [System.Security.SecuritySafeCritical] internal virtual Object GetTarget() { return (_methodPtrAux.IsNull()) ? _target : null; } - [System.Security.SecurityCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.InternalCall)] internal extern static bool CompareUnmanagedFunctionPtrs (Delegate d1, Delegate d2); } |