Esempio n. 1
0
        // By BJurado, from http://forums.asp.net/t/1709783.aspx/1
        internal static MemberExpression StaticPropertyOrField(IReflect type, string propertyOrFieldName)
        {
            var property = type.GetProperty(propertyOrFieldName, PropertyStaticPublic);

            if (property != null)
            {
                return(Property(null, property));
            }

            var field = type.GetField(propertyOrFieldName, PropertyStaticPublic);

            if (field != null)
            {
                return(Field(null, field));
            }

            property = type.GetProperty(propertyOrFieldName, PropertyStaticNonPublic);
            if (property != null)
            {
                return(Property(null, property));
            }

            field = type.GetField(propertyOrFieldName, PropertyStaticNonPublic);
            if (field != null)
            {
                return(Field(null, field));
            }

            throw new ArgumentException($"{propertyOrFieldName} NotAMemberOfType {type}");
        }
Esempio n. 2
0
        private static object GetInstanceField(IReflect type, object instance, string fieldName)
        {
            const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic;
            var field = type.GetField(fieldName, bindFlags);

            return(field.GetValue(instance));
        }
        private static T GetDeclaredField <T>(IReflect type, object instance, string fieldName)
        {
            const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
                                           | BindingFlags.Static | BindingFlags.DeclaredOnly;
            var field = type.GetField(fieldName, bindFlags);

            return((T)field.GetValue(instance));
        }
Esempio n. 4
0
        /// <summary>
        /// Gets FieldInfo or throws exception
        /// </summary>
        /// <param name="type"></param>
        /// <param name="name"></param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        /// <returns></returns>
        public static FieldInfo GetPrivateFieldInfo(this IReflect type, string name)
        {
            type = type ?? throw new ArgumentNullException(nameof(type));
            name = name ?? throw new ArgumentNullException(nameof(name));

            return(type.GetField(name, BindingFlags.NonPublic | BindingFlags.Instance)
                   ?? throw new ArgumentException($"Private field \"{name}\" is not found"));
        }
Esempio n. 5
0
		private static FieldInfo GetFieldByPropertyName(IReflect viewModelType, string propertyName)
		{
			var charList = new List<char> { char.ToLower(propertyName[0]) };
			charList.AddRange(propertyName.Substring(1));
			var fieldName = new string(charList.ToArray());

			var field = viewModelType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
			return field;
		}
Esempio n. 6
0
        private static FieldInfo GetFieldByPropertyName(IReflect viewModelType, string propertyName)
        {
            var charList = new List <char> {
                char.ToLower(propertyName[0])
            };

            charList.AddRange(propertyName.Substring(1));
            var fieldName = new string(charList.ToArray());

            var field = viewModelType.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);

            return(field);
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the value of the member from an object using the member name
        /// </summary>
        /// <param name="type"></param>
        /// <param name="memberName">Name of the member</param>
        /// <param name="obj">Object to get the member value from</param>
        /// <returns></returns>
        private static object GetMemberValue(this IReflect type, string memberName, object obj)
        {
            var memberInfo = type.GetMember(memberName, DEFAULT_BINDING_FLAGS).FirstOrDefault();

            if (memberInfo is PropertyInfo)
            {
                return(type.GetProperty(memberName, DEFAULT_BINDING_FLAGS).GetValue(obj));
            }
            if (memberInfo is FieldInfo)
            {
                return(type.GetField(memberName, DEFAULT_BINDING_FLAGS).GetValue(obj));
            }
            return(null);
        }
        private static void SetField(IReflect type, object instance, string fieldName, object value, BindingFlags flags)
        {
            var field = type.GetField(fieldName, flags);

            if (field == null && instance != null)
            {
                field = instance.GetType().GetField(fieldName, flags);
            }
            if (field == null)
            {
                throw new ArgumentException($"Target type '{type}' does not contain a definition for field '{fieldName}'.");
            }
            field.SetValue(instance ?? type, value);
        }
        private static bool TryGetField(IReflect type, object instance, string fieldName, BindingFlags flags, out object value)
        {
            var field = type.GetField(fieldName, flags);

            if (field == null && instance != null)
            {
                field = instance.GetType().GetField(fieldName, flags);
            }
            if (field == null)
            {
                value = default;
                return(false);
            }
            value = field.GetValue(instance ?? type);
            return(true);
        }
Esempio n. 10
0
        static FieldInfo GetRouterField(IReflect type, string fieldName, BindingFlags bindingFlags)
        {
            var field = type.GetField(fieldName, bindingFlags | BindingFlags.NonPublic);

            if (field == null)
            {
                throw new SubstituteException("Cannot substitute for non-patched types");
            }

            if (field.FieldType != typeof(object))
            {
                throw new SubstituteException("Unexpected mock data type found on patched type");
            }

            return(field);
        }
Esempio n. 11
0
        private static MemberInfo GetMember(IReflect entityType, string[] memberNames)
        {
            var publicProperties =
                from name in memberNames
                select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) into propertyInfo
                    where propertyInfo != null && IsPropertyOk(propertyInfo)
                select propertyInfo;

            var privateProperties =
                from name in memberNames
                select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic) into propertyInfo
                    where propertyInfo != null && IsPropertyOk(propertyInfo)
                select propertyInfo;

            var publicFields =
                from name in memberNames
                select entityType.GetField(name, BindingFlags.Instance | BindingFlags.Public) into fieldInfo
                    where fieldInfo != null && IsFildOk(fieldInfo)
                select fieldInfo;

            return(publicProperties.OfType <MemberInfo>().Concat(privateProperties).Concat(publicFields).FirstOrDefault());
        }
        private static MemberInfo GetMember(IReflect entityType, string[] memberNames)
        {
            var publicProperties =
                from name in memberNames
                select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.Public) into propertyInfo
                where propertyInfo != null && IsPropertyOk(propertyInfo)
                select propertyInfo;

            var privateProperties =
                from name in memberNames
                select entityType.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic) into propertyInfo
                where propertyInfo != null && IsPropertyOk(propertyInfo)
                select propertyInfo;

            var publicFields =
                from name in memberNames
                select entityType.GetField(name, BindingFlags.Instance | BindingFlags.Public) into fieldInfo
                where fieldInfo != null && IsFildOk(fieldInfo)
                select fieldInfo;

            return publicProperties.OfType<MemberInfo>().Concat(privateProperties).Concat(publicFields).FirstOrDefault();
        }
Esempio n. 13
0
 private static double GetMaxValue(this IReflect type)
 {
     return(Convert.ToDouble(type?.GetField(MaxValueProperty, ReflectionFlags)?.GetValue(null)));
 }
Esempio n. 14
0
 FieldInfo?IReflect.GetField(string name, BindingFlags bindingAttr)
 => publicIReflect.GetField(name, bindingAttr);
Esempio n. 15
0
 private static object GetFieldStatic(this IReflect t, string name) => t.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null);
 private static object GetInstanceField(IReflect type, object instance, string fieldName)
 {
     const BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic;
     var field = type.GetField(fieldName, bindFlags);
     return field.GetValue(instance);
 }
Esempio n. 17
0
 // Methods
 System.Reflection.FieldInfo IReflect.GetField(string name, System.Reflection.BindingFlags bindingAttr)
 {
     return(typeIReflectImplementation.GetField(name, bindingAttr));
 }
 FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
 {
     return(publicIReflect.GetField(name, bindingAttr));
 }
Esempio n. 19
0
        /// <summary>
        /// Sets the field.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="strMethod">The STR method.</param>
        /// <param name="objInstance">The obj instance.</param>
        /// <param name="eFlags">The e flags.</param>
        /// <param name="setValue">The set value.</param>
        /// <returns></returns>
        private static object SetField(IReflect type, string strMethod, object objInstance, BindingFlags eFlags, object setValue)
        {
            FieldInfo field;
            try
            {
                if ((eFlags == InstanceBindingFlags) && (objInstance == null))
                {
                    throw new ArgumentException("The reflection non-static object argument was invalid");
                }
                if ((eFlags == StaticBindingFlags) && (objInstance != null))
                {
                    throw new ArgumentException("The reflection static object argument was invalid");
                }
                if ((objInstance != null) && (objInstance.GetType() != (Type)type) && (objInstance.GetType().BaseType != (Type)type))
                {
                    throw new ArgumentException("The object instance was of type '" + objInstance.GetType() + "' for type '" + type + "'.");
                }
                if (string.IsNullOrEmpty(strMethod))
                {
                    throw new ArgumentException("The reflection method argument was invalid");
                }

                field = type.GetField(strMethod, eFlags);
                if (field == null)
                {
                    throw new ArgumentException("There is no field '" + strMethod + "' for type '" + type + "'.");
                }

                if (setValue.GetType() != field.FieldType)
                {
                    throw new ArgumentException("The value instance was of type '" + setValue.GetType() + "' for field type '" + field.FieldType + "'.");
                }

                field.SetValue(objInstance, setValue);
                var objRet = field.GetValue(objInstance);
                return objRet;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                throw;
            }
        }
Esempio n. 20
0
        // Methods
        FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
        {
            var ret = typeIReflectImplementation.GetField(name, bindingAttr);

            return(ret);
        }
 private static FieldInfo GetField(IReflect type, string fieldName) {
     return type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
 }