| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- namespace Abp.Reflection
- {
- /// <summary>
- /// Defines helper methods for reflection.
- /// </summary>
- internal static class ReflectionHelper
- {
- /// <summary>
- /// Checks whether <paramref name="givenType"/> implements/inherits <paramref name="genericType"/>.
- /// </summary>
- /// <param name="givenType">Type to check</param>
- /// <param name="genericType">Generic type</param>
- public static bool IsAssignableToGenericType(Type givenType, Type genericType)
- {
- var givenTypeInfo = givenType.GetTypeInfo();
- if (givenTypeInfo.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
- {
- return true;
- }
- foreach (var interfaceType in givenType.GetInterfaces())
- {
- if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == genericType)
- {
- return true;
- }
- }
- if (givenTypeInfo.BaseType == null)
- {
- return false;
- }
- return IsAssignableToGenericType(givenTypeInfo.BaseType, genericType);
- }
- /// <summary>
- /// Gets a list of attributes defined for a class member and it's declaring type including inherited attributes.
- /// </summary>
- /// <param name="inherit">Inherit attribute from base classes</param>
- /// <param name="memberInfo">MemberInfo</param>
- public static List<object> GetAttributesOfMemberAndDeclaringType(MemberInfo memberInfo, bool inherit = true)
- {
- var attributeList = new List<object>();
- attributeList.AddRange(memberInfo.GetCustomAttributes(inherit));
- if (memberInfo.DeclaringType != null)
- {
- attributeList.AddRange(memberInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(inherit));
- }
- return attributeList;
- }
- /// <summary>
- /// Gets a list of attributes defined for a class member and type including inherited attributes.
- /// </summary>
- /// <param name="memberInfo">MemberInfo</param>
- /// <param name="type">Type</param>
- /// <param name="inherit">Inherit attribute from base classes</param>
- public static List<object> GetAttributesOfMemberAndType(MemberInfo memberInfo, Type type, bool inherit = true)
- {
- var attributeList = new List<object>();
- attributeList.AddRange(memberInfo.GetCustomAttributes(inherit));
- attributeList.AddRange(type.GetTypeInfo().GetCustomAttributes(inherit));
- return attributeList;
- }
- /// <summary>
- /// Gets a list of attributes defined for a class member and it's declaring type including inherited attributes.
- /// </summary>
- /// <typeparam name="TAttribute">Type of the attribute</typeparam>
- /// <param name="memberInfo">MemberInfo</param>
- /// <param name="inherit">Inherit attribute from base classes</param>
- public static List<TAttribute> GetAttributesOfMemberAndDeclaringType<TAttribute>(MemberInfo memberInfo, bool inherit = true)
- where TAttribute : Attribute
- {
- var attributeList = new List<TAttribute>();
- if (memberInfo.IsDefined(typeof(TAttribute), inherit))
- {
- attributeList.AddRange(memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
- }
- if (memberInfo.DeclaringType != null && memberInfo.DeclaringType.GetTypeInfo().IsDefined(typeof(TAttribute), inherit))
- {
- attributeList.AddRange(memberInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
- }
- return attributeList;
- }
- /// <summary>
- /// Gets a list of attributes defined for a class member and type including inherited attributes.
- /// </summary>
- /// <typeparam name="TAttribute">Type of the attribute</typeparam>
- /// <param name="memberInfo">MemberInfo</param>
- /// <param name="type">Type</param>
- /// <param name="inherit">Inherit attribute from base classes</param>
- public static List<TAttribute> GetAttributesOfMemberAndType<TAttribute>(MemberInfo memberInfo, Type type, bool inherit = true)
- where TAttribute : Attribute
- {
- var attributeList = new List<TAttribute>();
- if (memberInfo.IsDefined(typeof(TAttribute), inherit))
- {
- attributeList.AddRange(memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
- }
- if (type.GetTypeInfo().IsDefined(typeof(TAttribute), inherit))
- {
- attributeList.AddRange(type.GetTypeInfo().GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>());
- }
- return attributeList;
- }
- /// <summary>
- /// Tries to gets an of attribute defined for a class member and it's declaring type including inherited attributes.
- /// Returns default value if it's not declared at all.
- /// </summary>
- /// <typeparam name="TAttribute">Type of the attribute</typeparam>
- /// <param name="memberInfo">MemberInfo</param>
- /// <param name="defaultValue">Default value (null as default)</param>
- /// <param name="inherit">Inherit attribute from base classes</param>
- public static TAttribute GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
- where TAttribute : class
- {
- return memberInfo.GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
- ?? memberInfo.ReflectedType?.GetTypeInfo().GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
- ?? defaultValue;
- }
- /// <summary>
- /// Tries to gets an of attribute defined for a class member and it's declaring type including inherited attributes.
- /// Returns default value if it's not declared at all.
- /// </summary>
- /// <typeparam name="TAttribute">Type of the attribute</typeparam>
- /// <param name="memberInfo">MemberInfo</param>
- /// <param name="defaultValue">Default value (null as default)</param>
- /// <param name="inherit">Inherit attribute from base classes</param>
- public static TAttribute GetSingleAttributeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
- where TAttribute : Attribute
- {
- //Get attribute on the member
- if (memberInfo.IsDefined(typeof(TAttribute), inherit))
- {
- return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
- }
- return defaultValue;
- }
- /// <summary>
- /// Gets a property by it's full path from given object
- /// </summary>
- /// <param name="obj">Object to get value from</param>
- /// <param name="objectType">Type of given object</param>
- /// <param name="propertyPath">Full path of property</param>
- /// <returns></returns>
- internal static object GetPropertyByPath(object obj, Type objectType, string propertyPath)
- {
- var property = obj;
- var currentType = objectType;
- var objectPath = currentType.FullName;
- var absolutePropertyPath = propertyPath;
- if (absolutePropertyPath.StartsWith(objectPath))
- {
- absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
- }
- foreach (var propertyName in absolutePropertyPath.Split('.'))
- {
- property = currentType.GetProperty(propertyName);
- currentType = ((PropertyInfo) property).PropertyType;
- }
- return property;
- }
- /// <summary>
- /// Gets value of a property by it's full path from given object
- /// </summary>
- /// <param name="obj">Object to get value from</param>
- /// <param name="objectType">Type of given object</param>
- /// <param name="propertyPath">Full path of property</param>
- /// <returns></returns>
- internal static object GetValueByPath(object obj, Type objectType, string propertyPath)
- {
- var value = obj;
- var currentType = objectType;
- var objectPath = currentType.FullName;
- var absolutePropertyPath = propertyPath;
- if (absolutePropertyPath.StartsWith(objectPath))
- {
- absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
- }
- foreach (var propertyName in absolutePropertyPath.Split('.'))
- {
- var property = currentType.GetProperty(propertyName);
- value = property.GetValue(value, null);
- currentType = property.PropertyType;
- }
- return value;
- }
-
- /// <summary>
- /// Sets value of a property by it's full path on given object
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="objectType"></param>
- /// <param name="propertyPath"></param>
- /// <param name="value"></param>
- internal static void SetValueByPath(object obj, Type objectType, string propertyPath, object value)
- {
- var currentType = objectType;
- PropertyInfo property;
- var objectPath = currentType.FullName;
- var absolutePropertyPath = propertyPath;
- if (absolutePropertyPath.StartsWith(objectPath))
- {
- absolutePropertyPath = absolutePropertyPath.Replace(objectPath + ".", "");
- }
- var properties = absolutePropertyPath.Split('.');
- if (properties.Length == 1)
- {
- property = objectType.GetProperty(properties.First());
- property.SetValue(obj, value);
- return;
- }
- for (int i = 0; i < properties.Length - 1; i++)
- {
- property = currentType.GetProperty(properties[i]);
- obj = property.GetValue(obj, null);
- currentType = property.PropertyType;
- }
- property = currentType.GetProperty(properties.Last());
- property.SetValue(obj, value);
- }
- internal static bool IsPropertyGetterSetterMethod(MethodInfo method, Type type)
- {
- if (!method.IsSpecialName)
- {
- return false;
- }
- if (method.Name.Length < 5)
- {
- return false;
- }
- return type.GetProperty(method.Name.Substring(4), BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic) != null;
- }
- }
- }
|