#region License /* Copyright (c) 2010-2014 Danko Kozar Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #endregion License using System; using System.Collections.Generic; using System.Reflection; namespace eDriven.Core.Reflection { /// /// Reflection utility /// public static class CoreReflector { #if DEBUG // ReSharper disable UnassignedField.Global /// /// Debug mode /// public static bool DebugMode = true; // ReSharper restore UnassignedField.Global #endif private static FieldInfo[] _fieldInfos; private static PropertyInfo[] _propertyInfos; private static readonly object[] Index = { }; /// /// Checks if the target contains member with the specified name /// /// Target object /// Field or property name /// public static bool HasMember(object target, string memberName) { Type targetType = target.GetType(); var propertyInfo = targetType.GetProperty(memberName); if (null != propertyInfo) { return true; } var fieldInfo = targetType.GetField(memberName); if (null != fieldInfo) { return true; } return false; } /// /// Gets the property value
/// Uses internal caching ///
/// Target object /// Property name /// public static object GetValue(object target, string property) { Type type = target.GetType(); MemberWrapper memberWrapper = GlobalMemberCache.Instance.Get(type, property); if (null == memberWrapper) { memberWrapper = new MemberWrapper(type, property); GlobalMemberCache.Instance.Put(type, property, memberWrapper); } return memberWrapper.GetValue(target); } /// /// Sets the property value
/// Uses internal caching ///
/// Target object /// Property name /// Value public static void SetValue(object target, string property, object value) { //Debug.Log(string.Format(@"SetValue {0}, ""{1}"", ""{2}""", target, property, value)); Type type = target.GetType(); MemberWrapper memberWrapper = GlobalMemberCache.Instance.Get(type, property); if (null == memberWrapper) { memberWrapper = new MemberWrapper(type, property); GlobalMemberCache.Instance.Put(type, property, memberWrapper); } memberWrapper.SetValue(target, value); } /// /// Proxies an item /// /// /// /// public static MemberProxy GetProxy(object item, string variable) { return new MemberProxy(item, variable); } /// /// Gets all field and propertiy names for a given type /// /// /// public static List GetFieldAndPropertyNames(Type type) { // TODO: we might cache this search List names = new List(); _fieldInfos = type.GetFields(); foreach (FieldInfo info in _fieldInfos) { names.Add(info.Name); } _propertyInfos = type.GetProperties(); foreach (PropertyInfo info in _propertyInfos) { names.Add(info.Name); } names.Sort(); return names; } /// /// Gets all members for a type /// /// /// public static List GetMembers(Type type) { return new List(type.GetMembers()); } /// /// Gets a member value /// /// /// /// public static object GetMemberValue(MemberInfo memberInfo, object obj) { object value = null; FieldInfo fi = memberInfo as FieldInfo; if (null != fi) { value = fi.GetValue(obj); //Debug.Log(string.Format(" -> Field: {0}", memberInfo.Name)); } else { PropertyInfo pi = memberInfo as PropertyInfo; if (null != pi) { value = pi.GetValue(obj, Index); //Debug.Log(string.Format(" -> Property: {0}", memberInfo.Name)); } } return value; } /// /// Sets a member value /// /// /// /// public static void SetMemberValue(MemberInfo memberInfo, object obj, object value) { FieldInfo fi = memberInfo as FieldInfo; if (null != fi) { fi.SetValue(obj, value); //Debug.Log(string.Format(" -> Field: {0}", memberInfo.Name)); } else { PropertyInfo pi = memberInfo as PropertyInfo; if (null != pi) { pi.SetValue(obj, value, Index); //Debug.Log(string.Format(" -> Property: {0}", memberInfo.Name)); } } } #region Class attributes /// /// Gets all the attributes of the specified type /// /// /// /// public static List GetClassAttributes(Type type, bool inherit = true) { List outputList = new List(); var attributes = type.GetCustomAttributes(typeof(T), inherit); if (attributes.Length > 0) { foreach (var attribute in attributes) { outputList.Add((T)attribute); } } return outputList; } /// /// Gets all the attributes of the specified type /// /// /// /// public static bool HasClassAttributes(Type type, bool inherit = true) { var attributes = type.GetCustomAttributes(typeof(T), inherit); return attributes.Length > 0; } #endregion #region Member attributes /// /// Gets all the attributes of the specified type /// /// /// /// public static bool HasMemberAttributes(MemberInfo memberInfo, bool inherit = true) { var attributes = memberInfo.GetCustomAttributes(typeof(T), inherit); return attributes.Length > 0; } /// /// Gets all the member attributes of the specified type /// /// /// /// /// public static List GetMemberAttributes(MemberInfo memberInfo, bool inherit = true) { List outputList = new List(); var attributes = memberInfo.GetCustomAttributes(typeof(T), inherit); if (attributes.Length > 0) { foreach (var attribute in attributes) { outputList.Add((T)attribute); } } return outputList; } #endregion #region Method attributes /// /// Gets all the attributes of the specified type /// /// /// /// public static bool HasMethodAttributes(MethodInfo methodInfo, bool inherit = true) { var attributes = methodInfo.GetCustomAttributes(typeof(T), inherit); return attributes.Length > 0; } /// /// Gets all the method attributes of the specified type /// /// /// /// /// public static List GetMethodAttributes(MethodInfo methodInfo, bool inherit = true) { List outputList = new List(); var attributes = methodInfo.GetCustomAttributes(typeof(T), inherit); if (attributes.Length > 0) { foreach (var attribute in attributes) { outputList.Add((T)attribute); } } return outputList; } #endregion } }