Created
May 18, 2012 21:31
-
-
Save gabrielgreen/2727697 to your computer and use it in GitHub Desktop.
Reflection Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ReflectionExtensions | |
{ | |
public static Type UnderlyingType(this MemberInfo member) | |
{ | |
Type type; | |
switch (member.MemberType) | |
{ | |
case MemberTypes.Field: | |
type = ((FieldInfo)member).FieldType; | |
break; | |
case MemberTypes.Property: | |
type = ((PropertyInfo)member).PropertyType; | |
break; | |
case MemberTypes.Event: | |
type = ((EventInfo)member).EventHandlerType; | |
break; | |
default: | |
throw new ArgumentException("member must be if type FieldInfo, PropertyInfo or EventInfo", "member"); | |
} | |
return Nullable.GetUnderlyingType(type) ?? type; | |
} | |
public static object Value(this MemberInfo member, object target) | |
{ | |
if (member is PropertyInfo) | |
{ | |
return (member as PropertyInfo).GetValue(target, null); | |
} | |
else if (member is FieldInfo) | |
{ | |
return (member as FieldInfo).GetValue(target); | |
} | |
else | |
{ | |
throw new Exception("member must be either PropertyInfo or FieldInfo"); | |
} | |
} | |
public static void Assign(this MemberInfo member, object target, object value) | |
{ | |
if (member is PropertyInfo) | |
{ | |
(member as PropertyInfo).SetValue(target, value, null); | |
} | |
else if (member is FieldInfo) | |
{ | |
(member as FieldInfo).SetValue(target, value); | |
} | |
else | |
{ | |
throw new Exception("destinationMember must be either PropertyInfo or FieldInfo"); | |
} | |
} | |
public static T MemberValue<T>(this object source, string memberName) | |
{ | |
return (T)source.GetType().GetMember(memberName)[0].Value(source); | |
} | |
public static bool HasAttribute<T>(this MethodInfo method) | |
{ | |
return (method.GetCustomAttributes(typeof(T), false).Length > 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment