PageRenderTime 135ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/TP/External/Oleg_ivo.Tools/Utils/Reflection.cs

http://iist.googlecode.com/
C# | 370 lines | 217 code | 37 blank | 116 comment | 45 complexity | dfa138b3a90649089d44f6984c105d52 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5. using System.Windows.Forms;
  6. namespace Oleg_ivo.Tools.Utils
  7. {
  8. ///<summary>
  9. ///
  10. ///</summary>
  11. public abstract class Reflection
  12. {
  13. #region Assemblies
  14. /// <summary>
  15. /// ?????????? ????????? ??????
  16. /// </summary>
  17. public static void LoadAssemblies()
  18. {
  19. string[] assemblyStrings = new string[]{/*"Transfers.dll"*/};
  20. foreach (string assemblyString in assemblyStrings)
  21. Assembly.LoadFrom(string.Format("{0}\\{1}", Application.StartupPath, assemblyString));
  22. }
  23. ///<summary>
  24. /// ???????? ??? ??????????? ??????
  25. ///</summary>
  26. ///<param name="onlyMine">?????? ???</param>
  27. ///<param name="domain">?????</param>
  28. ///<returns></returns>
  29. public static Assembly[] GetAssemblies(bool onlyMine, AppDomain domain)
  30. {
  31. List<Assembly> assembliesList = new List<Assembly>(GetAssembliesFromAppDomain(domain));
  32. //if (_customAssemblies.Count > 0)
  33. //{
  34. // assembliesList.AddRange(_customAssemblies);
  35. //}
  36. if (onlyMine)
  37. foreach (Assembly assembly in assembliesList.ToArray())
  38. if (assembly.FullName == null || !assembly.FullName.Contains("Oleg_ivo"))
  39. assembliesList.Remove(assembly);
  40. return assembliesList.ToArray();
  41. }
  42. /// <summary>
  43. /// ???????? ?????? ?? ??????
  44. /// </summary>
  45. /// <param name="domain">???? null, ????? ???? ????? ???????? ??????????</param>
  46. /// <returns></returns>
  47. private static Assembly[] GetAssembliesFromAppDomain(AppDomain domain)
  48. {
  49. if (domain == null) domain = AppDomain.CurrentDomain;
  50. return domain.GetAssemblies();
  51. }
  52. #endregion
  53. #region Types
  54. ///<summary>
  55. /// ???????? ??? ???? ?? ?????? ???????? ??????????
  56. ///</summary>
  57. ///<returns></returns>
  58. public static List<Type> GetTypesFromDomain()
  59. {
  60. return GetTypesFromDomain(null);
  61. }
  62. ///<summary>
  63. /// ???????? ??? ???? ?? ??????
  64. ///</summary>
  65. ///<param name="domain"></param>
  66. ///<returns></returns>
  67. public static List<Type> GetTypesFromDomain(AppDomain domain)
  68. {
  69. List<Type> types = new List<Type>();
  70. foreach (Assembly assembly in GetAssembliesFromAppDomain(domain))
  71. foreach (Type type in assembly.GetTypes())
  72. types.Add(type);
  73. return types;
  74. }
  75. /// <summary>
  76. /// ???????? ???
  77. /// </summary>
  78. /// <param name="fullName"></param>
  79. /// <returns></returns>
  80. public static Type GetTypeFromAssemblies(string fullName)
  81. {
  82. foreach (Type type in GetTypesFromDomain())
  83. if (type.FullName == fullName)
  84. return type;
  85. return null;
  86. }
  87. #endregion
  88. #region Members
  89. /// <summary>
  90. /// ???????? ??? ???????????? ??? ????
  91. /// </summary>
  92. /// <param name="type"></param>
  93. /// <returns></returns>
  94. public static ConstructorInfo[] GetConstructorsForType(Type type)
  95. {
  96. ConstructorInfo[] constructorInfos = type.GetConstructors();
  97. return constructorInfos;
  98. }
  99. /// <summary>
  100. /// ???????? ???????????? ??????????? ??? ???? (???? ?? ????)
  101. /// </summary>
  102. /// <param name="type"></param>
  103. /// <returns></returns>
  104. public static ConstructorInfo GetTheOneConstructorForType(Type type)
  105. {
  106. ConstructorInfo[] constructorInfos = GetConstructorsForType(type);
  107. if (constructorInfos != null && constructorInfos.Length == 1)
  108. return constructorInfos[0];
  109. return null;
  110. }
  111. /// <summary>
  112. /// ????? ????? ?? ????? ??? ?????????? ????
  113. /// </summary>
  114. /// <param name="type"></param>
  115. /// <param name="methodName"></param>
  116. /// <returns></returns>
  117. public static MethodInfo FindMethodInfo(Type type, string methodName)
  118. {
  119. MethodInfo methodInfo = null;
  120. if (type != null)
  121. {
  122. MethodInfo[] methodInfos = type.GetMethods();
  123. foreach (MethodInfo info in methodInfos)
  124. {
  125. if (string.Compare(info.Name, methodName, true) == 0)
  126. {
  127. methodInfo = info;
  128. break;
  129. }
  130. }
  131. }
  132. return methodInfo;
  133. }
  134. #endregion
  135. #region Attributes & Metadata Descriptions
  136. /// <summary>
  137. ///
  138. /// </summary>
  139. /// <param name="customAttributeProvider"></param>
  140. /// <param name="attributeType"></param>
  141. /// <returns></returns>
  142. public static Attribute GetAttribute(ICustomAttributeProvider customAttributeProvider, Type attributeType)
  143. {
  144. return GetAttribute(customAttributeProvider, attributeType, true);
  145. }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. /// <param name="customAttributeProvider"></param>
  150. /// <param name="attributeType"></param>
  151. /// <param name="throwOnlyOne">???????? ??????????, ???? ??????? ?????? ???? ?????? ????</param>
  152. /// <returns></returns>
  153. public static Attribute GetAttribute(ICustomAttributeProvider customAttributeProvider, Type attributeType, bool throwOnlyOne)
  154. {
  155. return GetAttribute(customAttributeProvider, attributeType, throwOnlyOne, true);
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. /// <param name="customAttributeProvider"></param>
  161. /// <param name="attributeType"></param>
  162. /// <param name="throwOnlyOne">???????? ??????????, ???? ??????? ?????? ???? ?????? ????</param>
  163. /// <param name="inherit"></param>
  164. /// <returns></returns>
  165. public static Attribute GetAttribute(ICustomAttributeProvider customAttributeProvider, Type attributeType, bool throwOnlyOne, bool inherit)
  166. {
  167. Attribute[] attributes = null;
  168. if (customAttributeProvider != null)
  169. attributes = (Attribute[])customAttributeProvider.GetCustomAttributes(attributeType, inherit);
  170. if (attributes != null)
  171. if (attributes.Length == 1)
  172. {
  173. return attributes[0];
  174. }
  175. else if (attributes.Length > 1 && throwOnlyOne)
  176. {
  177. throw new Exception(string.Format("????????? ????????? '{0}'. ?????? ???? ?????? ????!", GetTypeDescription(attributeType)));
  178. }
  179. return null;
  180. }
  181. /// <summary>
  182. /// ?????????? ???????? ?? ?????????? ???????
  183. /// </summary>
  184. /// <param name="customAttributeProvider"></param>
  185. /// <param name="throwOnlyOne">?????????? ???????????? ??? ?????????? ????????? > 1</param>
  186. /// <returns></returns>
  187. private static string GetDescription(ICustomAttributeProvider customAttributeProvider, bool throwOnlyOne)
  188. {
  189. DescriptionAttribute descriptionAttribute = GetAttribute(customAttributeProvider, typeof(DescriptionAttribute), throwOnlyOne) as DescriptionAttribute;
  190. if (descriptionAttribute != null)
  191. return descriptionAttribute.Description;
  192. return "";
  193. }
  194. /// <summary>
  195. /// ???????? ???? (???? ??? ??????, ?????????? ??? ????)
  196. /// </summary>
  197. /// <param name="type"></param>
  198. /// <returns></returns>
  199. public static string GetTypeDescription(Type type)
  200. {
  201. string description = null;
  202. if (type != null)
  203. {
  204. description = GetDescription(type, false);
  205. if (description == "")
  206. description = type.Name;
  207. }
  208. return description;
  209. }
  210. /// <summary>
  211. /// ?????????? ???????? ?? ?????????? ???????
  212. /// </summary>
  213. /// <param name="customAttributeProvider">??????</param>
  214. /// <returns></returns>
  215. public static string GetDescription(ICustomAttributeProvider customAttributeProvider)
  216. {
  217. return GetDescription(customAttributeProvider, true);
  218. }
  219. /// <summary>
  220. /// ?????????? ??????? ??? ???????? ????????????
  221. /// </summary>
  222. /// <param name="enumType"></param>
  223. /// <param name="itemName"></param>
  224. /// <param name="customAttributeType"></param>
  225. /// <returns></returns>
  226. public static Attribute GetEnumAttributes(Type enumType, string itemName, Type customAttributeType)
  227. {
  228. if(enumType!=null && customAttributeType!=null)
  229. {
  230. FieldInfo fieldInfo = enumType.GetField(itemName);
  231. if (fieldInfo != null)
  232. return GetAttribute(fieldInfo, customAttributeType, true);
  233. }
  234. return null;
  235. }
  236. /// <summary>
  237. /// ?????????? ??????? ???????? ???????????? (???????????? ??????? <see cref="DescriptionAttribute"/>)
  238. /// </summary>
  239. /// <param name="enumType"></param>
  240. /// <returns></returns>
  241. public static Dictionary<int, string> GetEnumMemberDictionary(Type enumType)
  242. {
  243. Dictionary<int, string> dictionary = new Dictionary<int, string>();
  244. if (enumType != null)
  245. {
  246. foreach (string name in Enum.GetNames(enumType))
  247. {
  248. FieldInfo fieldInfo = enumType.GetField(name);
  249. if (fieldInfo!=null)
  250. {
  251. string description = GetDescription(fieldInfo);
  252. int value = (int) Enum.Parse(enumType, name);
  253. dictionary.Add(value, description);
  254. }
  255. }
  256. }
  257. return dictionary;
  258. }
  259. /// <summary>
  260. /// ???????? ???????? ??? ???????? ????????????
  261. /// </summary>
  262. /// <param name="enumMember">??????? ????????????</param>
  263. /// <returns></returns>
  264. public static string GetDescription(Enum enumMember)
  265. {
  266. string description = null;
  267. Type enumType = enumMember.GetType();
  268. if (enumType!=null)
  269. {
  270. Dictionary<int, string> enumMemberDictionary = GetEnumMemberDictionary(enumType);
  271. int value = (int) Enum.Parse(enumType, enumMember.ToString());
  272. description = enumMemberDictionary[value];
  273. }
  274. return description;
  275. }
  276. #endregion
  277. ///<summary>
  278. ///
  279. ///</summary>
  280. ///<param name="memberInfo"></param>
  281. public delegate bool MemberInfoFilter(MemberInfo memberInfo);
  282. ///<summary>
  283. ///
  284. ///</summary>
  285. ///<param name="type"></param>
  286. ///<param name="memberInfoFilter"></param>
  287. ///<param name="memberType"></param>
  288. ///<returns></returns>
  289. public static MemberInfo[] GetMembers(Type type, MemberTypes memberType, MemberInfoFilter memberInfoFilter)
  290. {
  291. List<MemberInfo> memberInfos = new List<MemberInfo>();
  292. MemberInfo[] members = GetMembers(type, memberType);
  293. if(members!=null)
  294. {
  295. memberInfos.AddRange(members);
  296. if (memberInfoFilter != null)
  297. foreach (MemberInfo memberInfo in memberInfos.ToArray())
  298. if (!memberInfoFilter(memberInfo))
  299. memberInfos.Remove(memberInfo);
  300. }
  301. return memberInfos.ToArray();
  302. }
  303. private static MemberInfo[] GetMembers(Type type, MemberTypes memberType)
  304. {
  305. MemberInfo[] infos = null;
  306. if (type!=null)
  307. {
  308. switch (memberType)
  309. {
  310. case MemberTypes.Constructor:
  311. infos = type.GetConstructors();
  312. break;
  313. case MemberTypes.Event:
  314. infos = type.GetEvents();
  315. break;
  316. case MemberTypes.Field:
  317. infos = type.GetFields();
  318. break;
  319. case MemberTypes.Method:
  320. infos = type.GetMethods();
  321. break;
  322. case MemberTypes.Property:
  323. infos = type.GetProperties();
  324. break;
  325. case MemberTypes.All:
  326. infos = type.GetMembers();
  327. break;
  328. }
  329. }
  330. return infos;
  331. }
  332. }
  333. }