예제 #1
0
 /// <summary>
 /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例属性的指定类型的委托。
 /// </summary>
 /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
 /// <param name="delegateType">要创建的委托的类型。</param>
 /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
 /// <remarks>如果是实例属性,需要将实例对象作为委托的第一个参数。
 /// 如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
 /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <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="property"/>。</exception>
 /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
 public static Delegate CreateDelegate(this PropertyInfo property, Type delegateType)
 {
     CommonExceptions.CheckArgumentNull(property, nameof(property));
     CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType));
     Contract.Ensures(Contract.Result <Delegate>() != null);
     CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType));
     CommonExceptions.CheckUnboundGenParam(property, nameof(property));
     return(CreateOpenDelegate(property, delegateType, true));
 }
예제 #2
0
 /// <summary>
 /// 使用指定的第一个参数,创建用于表示获取或设置指定的静态或实例属性的指定类型的委托。
 /// </summary>
 /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
 /// <param name="delegateType">要创建的委托的类型。</param>
 /// <param name="firstArgument">如果是实例属性,则作为委托要绑定到的对象;否则将作为属性的第一个参数。</param>
 /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
 /// <remarks>如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
 /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <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="property"/>。</exception>
 /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
 public static Delegate CreateDelegate(this PropertyInfo property, Type delegateType, object firstArgument)
 {
     CommonExceptions.CheckArgumentNull(property, "property");
     CommonExceptions.CheckArgumentNull(delegateType, "delegateType");
     Contract.Ensures(Contract.Result <Delegate>() != null);
     CommonExceptions.CheckDelegateType(delegateType, "delegateType");
     CommonExceptions.CheckUnboundGenParam(property, "property");
     return(CreateClosedDelegate(property, delegateType, firstArgument, true, false));
 }
예제 #3
0
 /// <summary>
 /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例属性的指定类型的委托。
 /// </summary>
 /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
 /// <param name="delegateType">要创建的委托的类型。</param>
 /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="property"/>
 ///     时引发异常;否则为 <c>false</c>。</param>
 /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
 /// <remarks>如果是实例属性,需要将实例对象作为委托的第一个参数。
 /// 如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
 /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <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="property"/>
 /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
 /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
 public static Delegate CreateDelegate(this PropertyInfo property, Type delegateType, bool throwOnBindFailure)
 {
     CommonExceptions.CheckArgumentNull(property, nameof(property));
     CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType));
     Contract.EndContractBlock();
     CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType));
     CommonExceptions.CheckUnboundGenParam(property, nameof(property));
     return(CreateOpenDelegate(property, delegateType, throwOnBindFailure));
 }
예제 #4
0
 /// <summary>
 /// 使用指定的第一个参数和针对绑定失败的指定行为,创建用于表示获取或设置指定的静态或实例属性的指定类型的委托。
 /// </summary>
 /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
 /// <param name="delegateType">要创建的委托的类型。</param>
 /// <param name="firstArgument">如果是实例属性,则作为委托要绑定到的对象;否则将作为属性的第一个参数。</param>
 /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="property"/>
 ///     时引发异常;否则为 <c>false</c>。</param>
 /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
 /// <remarks>如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
 /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
 /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <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="property"/>
 /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
 /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
 public static Delegate CreateDelegate(this PropertyInfo property, Type delegateType, object firstArgument,
                                       bool throwOnBindFailure)
 {
     CommonExceptions.CheckArgumentNull(property, "property");
     CommonExceptions.CheckArgumentNull(delegateType, "delegateType");
     Contract.EndContractBlock();
     CommonExceptions.CheckDelegateType(delegateType, "delegateType");
     CommonExceptions.CheckUnboundGenParam(property, "property");
     return(CreateClosedDelegate(property, delegateType, firstArgument, throwOnBindFailure, false));
 }
예제 #5
0
        /// <summary>
        /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例属性的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="property"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
        /// <remarks>如果是实例属性,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="property"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this PropertyInfo property, bool throwOnBindFailure)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(property, nameof(property));
            Contract.EndContractBlock();
            var type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(property, nameof(property));
            return(CreateOpenDelegate(property, type, throwOnBindFailure) as TDelegate);
        }
예제 #6
0
        /// <summary>
        /// 使用指定的第一个参数,创建用于表示获取或设置指定的静态或实例属性的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
        /// <param name="firstArgument">如果是实例属性,则作为委托要绑定到的对象;否则将作为属性的第一个参数。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="property"/>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this PropertyInfo property, object firstArgument)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(property, "property");
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            Type type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(property, "property");
            return(CreateClosedDelegate(property, type, firstArgument, true, false) as TDelegate);
        }
예제 #7
0
        /// <summary>
        /// 创建用于表示获取或设置指定静态或实例属性的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
        /// <remarks>如果是实例属性,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="property"/> 。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this PropertyInfo property)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(property, nameof(property));
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            var type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(property, nameof(property));
            return(CreateOpenDelegate(property, type, true) as TDelegate);
        }
예제 #8
0
        /// <summary>
        /// 使用指定的第一个参数和针对绑定失败的指定行为,创建用于表示获取或设置指定的静态或实例属性的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="property">描述委托要表示的静态或实例属性的 <see cref="PropertyInfo"/>。</param>
        /// <param name="firstArgument">如果是实例属性,则作为委托要绑定到的对象;否则将作为属性的第一个参数。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="property"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例属性。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取属性,否则认为是设置属性。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="property"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="property"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this PropertyInfo property, object firstArgument,
                                                           bool throwOnBindFailure)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(property, "property");
            Contract.EndContractBlock();
            Type type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(property, "property");
            return(CreateClosedDelegate(property, type, firstArgument, throwOnBindFailure, false) as TDelegate);
        }
예제 #9
0
        /// <summary>
        /// 使用指定的第一个参数,创建用于表示获取或设置指定的静态或实例字段的指定类型的委托。
        /// </summary>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="delegateType">要创建的委托的类型。</param>
        /// <param name="firstArgument">如果是实例字段,则作为委托要绑定到的对象;否则将作为字段的第一个参数。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <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="field"/>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static Delegate CreateDelegate(this FieldInfo field, Type delegateType, object firstArgument)
        {
            CommonExceptions.CheckArgumentNull(field, nameof(field));
            CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType));
            Contract.Ensures(Contract.Result <Delegate>() != null);
            CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType));
            CommonExceptions.CheckUnboundGenParam(field, nameof(field));
            var dlg = CreateClosedDelegate(field, delegateType, firstArgument);

            if (dlg == null)
            {
                throw CommonExceptions.BindTargetField(nameof(field));
            }
            return(dlg);
        }
예제 #10
0
        /// <summary>
        /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例字段的指定类型的委托。
        /// </summary>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="delegateType">要创建的委托的类型。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果是实例字段,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <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="field"/>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static Delegate CreateDelegate(this FieldInfo field, Type delegateType)
        {
            CommonExceptions.CheckArgumentNull(field, "field");
            CommonExceptions.CheckArgumentNull(delegateType, "delegateType");
            Contract.Ensures(Contract.Result <Delegate>() != null);
            CommonExceptions.CheckDelegateType(delegateType, "delegateType");
            CommonExceptions.CheckUnboundGenParam(field, "field");
            Delegate dlg = CreateOpenDelegate(field, delegateType);

            if (dlg == null)
            {
                throw CommonExceptions.BindTargetField("field");
            }
            return(dlg);
        }
예제 #11
0
        /// <summary>
        /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例字段的指定类型的委托。
        /// </summary>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="delegateType">要创建的委托的类型。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="field"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果是实例字段,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <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="field"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static Delegate CreateDelegate(this FieldInfo field, Type delegateType, bool throwOnBindFailure)
        {
            CommonExceptions.CheckArgumentNull(field, "field");
            CommonExceptions.CheckArgumentNull(delegateType, "delegateType");
            Contract.EndContractBlock();
            CommonExceptions.CheckDelegateType(delegateType, "delegateType");
            CommonExceptions.CheckUnboundGenParam(field, "field");
            Delegate dlg = CreateOpenDelegate(field, delegateType);

            if (dlg == null && throwOnBindFailure)
            {
                throw CommonExceptions.BindTargetField("field");
            }
            return(dlg);
        }
예제 #12
0
        /// <summary>
        /// 使用指定的第一个参数和针对绑定失败的指定行为,创建用于表示获取或设置指定的静态或实例字段的指定类型的委托。
        /// </summary>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="delegateType">要创建的委托的类型。</param>
        /// <param name="firstArgument">如果是实例字段,则作为委托要绑定到的对象;否则将作为字段的第一个参数。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="field"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <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="field"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static Delegate CreateDelegate(this FieldInfo field, Type delegateType, object firstArgument,
                                              bool throwOnBindFailure)
        {
            CommonExceptions.CheckArgumentNull(field, nameof(field));
            CommonExceptions.CheckArgumentNull(delegateType, nameof(delegateType));
            Contract.EndContractBlock();
            CommonExceptions.CheckDelegateType(delegateType, nameof(delegateType));
            CommonExceptions.CheckUnboundGenParam(field, nameof(field));
            var dlg = CreateClosedDelegate(field, delegateType, firstArgument);

            if (dlg == null && throwOnBindFailure)
            {
                throw CommonExceptions.BindTargetField(nameof(field));
            }
            return(dlg);
        }
예제 #13
0
        /// <summary>
        /// 创建用于表示获取或设置指定静态或实例字段的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果是实例字段,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="field"/> 。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this FieldInfo field)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(field, nameof(field));
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            var type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(field, nameof(field));
            var dlg = CreateOpenDelegate(field, type);

            if (dlg == null)
            {
                throw CommonExceptions.BindTargetField(nameof(field));
            }
            return(dlg as TDelegate);
        }
예제 #14
0
        /// <summary>
        /// 使用针对绑定失败的指定行为,创建用于表示获取或设置指定静态或实例字段的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="field"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果是实例字段,需要将实例对象作为委托的第一个参数。
        /// 如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="field"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this FieldInfo field, bool throwOnBindFailure)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(field, "field");
            Contract.EndContractBlock();
            Type type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(field, "field");
            Delegate dlg = CreateOpenDelegate(field, type);

            if (dlg == null && throwOnBindFailure)
            {
                throw CommonExceptions.BindTargetField("field");
            }
            return(dlg as TDelegate);
        }
예제 #15
0
        /// <summary>
        /// 使用指定的第一个参数,创建用于表示获取或设置指定的静态或实例字段的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="firstArgument">如果是实例字段,则作为委托要绑定到的对象;否则将作为字段的第一个参数。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="field"/>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this FieldInfo field, object firstArgument)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(field, "field");
            Contract.Ensures(Contract.Result <TDelegate>() != null);
            Type type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(field, "field");
            Delegate dlg = CreateClosedDelegate(field, type, firstArgument);

            if (dlg == null)
            {
                throw CommonExceptions.BindTargetField("field");
            }
            return(dlg as TDelegate);
        }
예제 #16
0
        /// <summary>
        /// 使用指定的第一个参数和针对绑定失败的指定行为,创建用于表示获取或设置指定的静态或实例字段的指定类型的委托。
        /// </summary>
        /// <typeparam name="TDelegate">要创建的委托的类型。</typeparam>
        /// <param name="field">描述委托要表示的静态或实例字段的 <see cref="FieldInfo"/>。</param>
        /// <param name="firstArgument">如果是实例字段,则作为委托要绑定到的对象;否则将作为字段的第一个参数。</param>
        /// <param name="throwOnBindFailure">为 <c>true</c>,表示无法绑定 <paramref name="field"/>
        /// 时引发异常;否则为 <c>false</c>。</param>
        /// <returns>指定类型的委托,表示获取或设置指定的静态或实例字段。</returns>
        /// <remarks>如果委托具有返回值,则认为是获取字段,否则认为是设置字段。
        /// 支持参数的强制类型转换,参数声明可以与实际类型不同。</remarks>
        /// <exception cref="ArgumentNullException"><paramref name="field"/> 为 <c>null</c>。</exception>
        /// <exception cref="ArgumentException"><typeparamref name="TDelegate"/> 不是委托类型。</exception>
        /// <exception cref="ArgumentException">无法绑定 <paramref name="field"/>
        /// 且 <paramref name="throwOnBindFailure"/> 为 <c>true</c>。</exception>
        /// <exception cref="MethodAccessException">调用方无权访问 <paramref name="field"/>。</exception>
        public static TDelegate CreateDelegate <TDelegate>(this FieldInfo field, object firstArgument,
                                                           bool throwOnBindFailure)
            where TDelegate : class
        {
            CommonExceptions.CheckArgumentNull(field, nameof(field));
            Contract.EndContractBlock();
            var type = typeof(TDelegate);

            CommonExceptions.CheckDelegateType(type);
            CommonExceptions.CheckUnboundGenParam(field, nameof(field));
            var dlg = CreateClosedDelegate(field, type, firstArgument);

            if (dlg == null && throwOnBindFailure)
            {
                throw CommonExceptions.BindTargetField(nameof(field));
            }
            return(dlg as TDelegate);
        }
예제 #17
0
        /// <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);
        }
예제 #18
0
        /// <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);
        }
예제 #19
0
        /// <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);
        }
예제 #20
0
        /// <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);
        }
예제 #21
0
        /// <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);
        }
예제 #22
0
        /// <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);
        }
예제 #23
0
        /// <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);
        }