コード例 #1
0
        /// <summary>
        /// Creates a function that gets the value of the specified field. The parameter of the
        /// resulting function takes the object whose field value is to be accessed. If the
        /// specified field is static, the parameter of the resulting function is ignored.
        /// </summary>
        /// <param name="field">The field to create the getter function for.</param>
        /// <returns>A function that gets the field value.</returns>
        public static Func <object, object> CreateGetter(this FieldInfo field)
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }

            var getter = new FieldGetter(field);

            QueueUserWorkItem(getter, g => g.SetOptimizedFunc());
            return(getter.GetValue);
        }
コード例 #2
0
        /// <summary>
        /// Creates a function that gets the value of the specified field. The parameter of the
        /// resulting function takes the object whose field value is to be accessed. If the
        /// specified field is static, the parameter of the resulting function is ignored.
        /// </summary>
        /// <typeparam name="TFieldType">
        /// The return type of the resulting function. This type must be compatible with the
        /// <see cref="FieldInfo.FieldType"/> of the <paramref name="field"/> parameter.
        /// </typeparam>
        /// <param name="field">The field to create the getter function for.</param>
        /// <returns>A function that gets the field value.</returns>
        public static Func <object, TFieldType> CreateGetter <TFieldType>(this FieldInfo field)
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }
            if (!typeof(TFieldType).IsAssignableFrom(field.FieldType))
            {
                throw new ArgumentException("TFieldType must be assignable from field.FieldType", nameof(field));
            }

            var getter = new FieldGetter <TFieldType>(field);

            QueueUserWorkItem(getter, g => g.SetOptimizedFunc());
            return(getter.GetValue);
        }