/// <summary> /// 使用指定的第一个参数,创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="delegateType">要创建的委托的类型。</param> /// <param name="firstArgument">如果是实例方法(非构造函数),则作为委托要绑定到的对象; /// 否则将作为方法的第一个参数。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="delegateType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="delegateType"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> /// <seealso cref="Delegate.CreateDelegate(Type, object, MethodInfo)"/> public static Delegate CreateDelegate(this MethodBase method, Type delegateType, object firstArgument) { CommonExceptions.CheckArgumentNull(method, "method"); CommonExceptions.CheckArgumentNull(delegateType, "delegateType"); Contract.Ensures(Contract.Result <Delegate>() != null); CommonExceptions.CheckDelegateType(delegateType, "delegateType"); CommonExceptions.CheckUnboundGenParam(method, "method"); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam("method"); } Delegate dlg = CreateClosedDelegate(method, delegateType, firstArgument, false); if (dlg == null) { throw CommonExceptions.BindTargetMethod("method"); } return(dlg); }
/// <summary> /// 使用针对绑定失败的指定行为,创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="delegateType">要创建的委托的类型。</param> /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="method"/> /// 时引发异常;否则为 <c>false</c>。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>如果是实例方法(非构造函数),需要将实例对象作为委托的第一个参数。 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="delegateType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="delegateType"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/> /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> public static Delegate CreateDelegate(this MethodBase method, Type delegateType, bool throwOnBindFailure) { CommonExceptions.CheckArgumentNull(method, "method"); CommonExceptions.CheckArgumentNull(delegateType, "delegateType"); Contract.EndContractBlock(); CommonExceptions.CheckDelegateType(delegateType, "delegateType"); CommonExceptions.CheckUnboundGenParam(method, "method"); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam("method"); } Delegate dlg = CreateOpenDelegate(method, delegateType); if (dlg == null && throwOnBindFailure) { throw CommonExceptions.BindTargetMethod("method"); } return(dlg); }
/// <summary> /// 创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="delegateType">要创建的委托的类型。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>如果是实例方法(非构造函数),需要将实例对象作为委托的第一个参数。 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="delegateType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="delegateType"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> public static Delegate CreateDelegate(this MethodBase method, Type delegateType) { CommonExceptions.CheckArgumentNull(method, nameof(method)); CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType)); Contract.Ensures(Contract.Result <Delegate>() != null); CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType)); CommonExceptions.CheckUnboundGenParam(method, nameof(method)); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam(nameof(method)); } var dlg = CreateOpenDelegate(method, delegateType); if (dlg == null) { throw CommonExceptions.BindTargetMethod(nameof(method)); } return(dlg); }
/// <summary> /// 使用指定的第一个参数和针对绑定失败的指定行为,创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="delegateType">要创建的委托的类型。</param> /// <param name="firstArgument">如果是实例方法(非构造函数),则作为委托要绑定到的对象; /// 否则将作为方法的第一个参数。</param> /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="method"/> /// 时引发异常;否则为 <c>false</c>。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="delegateType"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><paramref name="delegateType"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/> /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> /// <seealso cref="Delegate.CreateDelegate(Type, object, MethodInfo, bool)"/> public static Delegate CreateDelegate(this MethodBase method, Type delegateType, object firstArgument, bool throwOnBindFailure) { CommonExceptions.CheckArgumentNull(method, nameof(method)); CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType)); Contract.EndContractBlock(); CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType)); CommonExceptions.CheckUnboundGenParam(method, nameof(method)); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam(nameof(method)); } var dlg = CreateClosedDelegate(method, delegateType, firstArgument, false); if (dlg == null && throwOnBindFailure) { throw CommonExceptions.BindTargetMethod(nameof(method)); } return(dlg); }
/// <summary> /// 使用针对绑定失败的指定行为,创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="method"/> /// 时引发异常;否则为 <c>false</c>。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>如果是实例方法(非构造函数),需要将实例对象作为委托的第一个参数。 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/> /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> public static TDelegate CreateDelegate <TDelegate>(this MethodBase method, bool throwOnBindFailure) where TDelegate : class { CommonExceptions.CheckArgumentNull(method, "method"); Contract.EndContractBlock(); Type type = typeof(TDelegate); CommonExceptions.CheckDelegateType(type); CommonExceptions.CheckUnboundGenParam(method, "method"); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam("method"); } Delegate dlg = CreateOpenDelegate(method, type); if (dlg == null && throwOnBindFailure) { throw CommonExceptions.BindTargetMethod("method"); } return(dlg as TDelegate); }
/// <summary> /// 使用指定的第一个参数,创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <param name="firstArgument">如果是实例方法(非构造函数),则作为委托要绑定到的对象; /// 否则将作为方法的第一个参数。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> /// <seealso cref="Delegate.CreateDelegate(Type, object, MethodInfo)"/> public static TDelegate CreateDelegate <TDelegate>(this MethodBase method, object firstArgument) where TDelegate : class { CommonExceptions.CheckArgumentNull(method, "method"); Contract.Ensures(Contract.Result <TDelegate>() != null); Type type = typeof(TDelegate); CommonExceptions.CheckDelegateType(type); CommonExceptions.CheckUnboundGenParam(method, "method"); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam("method"); } Delegate dlg = CreateClosedDelegate(method, type, firstArgument, false); if (dlg == null) { throw CommonExceptions.BindTargetMethod("method"); } return(dlg as TDelegate); }
/// <summary> /// 创建用于表示指定静态或实例方法的指定类型的委托。 /// </summary> /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam> /// <param name="method">描述委托要表示的静态或实例方法的 <see cref="MethodBase"/>。</param> /// <returns>指定类型的委托,表示指定的静态或实例方法。</returns> /// <remarks>如果是实例方法(非构造函数),需要将实例对象作为委托的第一个参数。 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception> /// <exception cref="ArgumentException">无法绑定 <paramref name="method"/>。</exception> /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="method"/>。</exception> /// <overloads> /// <summary> /// 创建用于表示指定静态或实例方法、字段或属性的指定类型的委托。 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。 /// </summary> /// </overloads> public static TDelegate CreateDelegate <TDelegate>(this MethodBase method) where TDelegate : class { CommonExceptions.CheckArgumentNull(method, nameof(method)); Contract.Ensures(Contract.Result <TDelegate>() != null); var type = typeof(TDelegate); CommonExceptions.CheckDelegateType(type); CommonExceptions.CheckUnboundGenParam(method, nameof(method)); if (method.ContainsGenericParameters && !method.IsGenericMethodDefinition) { throw CommonExceptions.UnboundGenParam(nameof(method)); } var dlg = CreateOpenDelegate(method, type); if (dlg == null) { throw CommonExceptions.BindTargetMethod(nameof(method)); } return(dlg as TDelegate); }