コード例 #1
0
        /// <summary>
        /// Creates a function that gets the value of the specified static field.
        /// </summary>
        /// <param name="field">The static field to create the getter function for.</param>
        /// <returns>A function that gets the static field value.</returns>
        public static Func <object> CreateStaticGetter(this FieldInfo field)
        {
            if (field is null)
            {
                throw new ArgumentNullException(nameof(field));
            }
            if (!field.IsStatic)
            {
                throw new ArgumentException("Field must be static.", nameof(field));
            }

            var getter = new StaticFieldGetter(field);

            QueueUserWorkItem(getter, g => g.SetOptimizedFunc());
            return(getter.GetValue);
        }
コード例 #2
0
        /// <summary>
        /// Creates a function that gets the value of the specified static field.
        /// </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 static field to create the getter function for.</param>
        /// <returns>A function that gets the static field value.</returns>
        public static Func <TFieldType> CreateStaticGetter <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));
            }
            if (!field.IsStatic)
            {
                throw new ArgumentException("Field must be static.", nameof(field));
            }

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

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