public static bool CanSetMemberValue(MemberInfo member, bool nonPublic, bool canSetReadOnly) { switch (TypeExtensions.MemberType(member)) { case MemberTypes.Field: FieldInfo fieldInfo = (FieldInfo)member; return((!fieldInfo.IsInitOnly || canSetReadOnly) && (nonPublic || fieldInfo.IsPublic)); case MemberTypes.Property: PropertyInfo propertyInfo = (PropertyInfo)member; if (!propertyInfo.CanWrite) { return(false); } if (nonPublic) { return(true); } else { return(propertyInfo.GetSetMethod(nonPublic) != null); } default: return(false); } }
public static bool CanReadMemberValue(MemberInfo member, bool nonPublic) { switch (TypeExtensions.MemberType(member)) { case MemberTypes.Field: FieldInfo fieldInfo = (FieldInfo)member; return(nonPublic || fieldInfo.IsPublic); case MemberTypes.Property: PropertyInfo propertyInfo = (PropertyInfo)member; if (!propertyInfo.CanRead) { return(false); } if (nonPublic) { return(true); } else { return(propertyInfo.GetGetMethod(nonPublic) != null); } default: return(false); } }
public static MemberInfo GetMemberInfoFromType(Type targetType, MemberInfo memberInfo) { if (TypeExtensions.MemberType(memberInfo) != MemberTypes.Property) { return(Enumerable.SingleOrDefault <MemberInfo>((IEnumerable <MemberInfo>)targetType.GetMember(memberInfo.Name, TypeExtensions.MemberType(memberInfo), BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))); } PropertyInfo propertyInfo = (PropertyInfo)memberInfo; Type[] types = Enumerable.ToArray <Type>(Enumerable.Select <ParameterInfo, Type>((IEnumerable <ParameterInfo>)propertyInfo.GetIndexParameters(), (Func <ParameterInfo, Type>)(p => p.ParameterType))); return((MemberInfo)targetType.GetProperty(propertyInfo.Name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, (Binder)null, propertyInfo.PropertyType, types, (ParameterModifier[])null)); }
public static void SetMemberValue(MemberInfo member, object target, object value) { ValidationUtils.ArgumentNotNull((object)member, "member"); ValidationUtils.ArgumentNotNull(target, "target"); switch (TypeExtensions.MemberType(member)) { case MemberTypes.Field: ((FieldInfo)member).SetValue(target, value); break; case MemberTypes.Property: ((PropertyInfo)member).SetValue(target, value, (object[])null); break; default: throw new ArgumentException(StringUtils.FormatWith("MemberInfo '{0}' must be of type FieldInfo or PropertyInfo", (IFormatProvider)CultureInfo.InvariantCulture, (object)member.Name), "member"); } }
public static Type GetMemberUnderlyingType(MemberInfo member) { ValidationUtils.ArgumentNotNull((object)member, "member"); switch (TypeExtensions.MemberType(member)) { case MemberTypes.Event: return(((EventInfo)member).EventHandlerType); case MemberTypes.Field: return(((FieldInfo)member).FieldType); case MemberTypes.Property: return(((PropertyInfo)member).PropertyType); default: throw new ArgumentException("MemberInfo must be of type FieldInfo, PropertyInfo or EventInfo", "member"); } }
private static bool IsOverridenGenericMember(MemberInfo memberInfo, BindingFlags bindingAttr) { switch (TypeExtensions.MemberType(memberInfo)) { case MemberTypes.Field: case MemberTypes.Property: Type declaringType = memberInfo.DeclaringType; if (!TypeExtensions.IsGenericType(declaringType)) { return(false); } Type genericTypeDefinition = declaringType.GetGenericTypeDefinition(); if (genericTypeDefinition == null) { return(false); } MemberInfo[] member = genericTypeDefinition.GetMember(memberInfo.Name, bindingAttr); return(member.Length != 0 && ReflectionUtils.GetMemberUnderlyingType(member[0]).IsGenericParameter); default: throw new ArgumentException("Member must be a field or property."); } }
public static object GetMemberValue(MemberInfo member, object target) { ValidationUtils.ArgumentNotNull((object)member, "member"); ValidationUtils.ArgumentNotNull(target, "target"); switch (TypeExtensions.MemberType(member)) { case MemberTypes.Field: return(((FieldInfo)member).GetValue(target)); case MemberTypes.Property: try { return(((PropertyInfo)member).GetValue(target, (object[])null)); } catch (TargetParameterCountException ex) { throw new ArgumentException(StringUtils.FormatWith("MemberInfo '{0}' has index parameters", (IFormatProvider)CultureInfo.InvariantCulture, (object)member.Name), (Exception)ex); } default: throw new ArgumentException(StringUtils.FormatWith("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo", (IFormatProvider)CultureInfo.InvariantCulture, (object)CultureInfo.InvariantCulture, (object)member.Name), "member"); } }