using System; using System.ComponentModel; using System.Drawing; using System.IO; using System.Reflection; using System.Resources; using System.Text; namespace Eas.Client { /// /// 反射操作辅助类,如获取或设置字段、属性的值等反射信息。 /// public sealed class Reflection { private Reflection() { } #region 属性字段设置 /// /// 绑定标识 /// public static BindingFlags bf = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; /// /// 执行方法 /// /// 对象实例 /// 方法名称 /// 参数 /// public static object InvokeMethod(object obj, string methodName, object[] args) { object objReturn = null; Type type = obj.GetType(); objReturn = type.InvokeMember(methodName, bf | BindingFlags.InvokeMethod, null, obj, args); return objReturn; } /// /// 设置对象实例的字段值 /// /// 对象实例 /// 字段名称 /// 字段值 public static void SetField(object obj, string name, object value) { FieldInfo fi = obj.GetType().GetField(name, bf); fi.SetValue(obj, value); } /// /// 获取对象实例的字段值 /// /// 对象实例 /// 字段名称 /// public static object GetField(object obj, string name) { FieldInfo fi = obj.GetType().GetField(name, bf); return fi.GetValue(obj); } /// /// 获取对象实例的字段集合 /// /// 对象实例 /// public static FieldInfo[] GetFields(object obj) { FieldInfo[] fieldInfos = obj.GetType().GetFields(bf); return fieldInfos; } /// /// 设置对象实例的属性值 /// /// 对象实例 /// 属性名称 /// 属性值 public static void SetProperty(object obj, string name, object value) { PropertyInfo fieldInfo = obj.GetType().GetProperty(name, bf); value = Convert.ChangeType(value, fieldInfo.PropertyType); fieldInfo.SetValue(obj, value, null); } /// /// 获取对象实例的属性值 /// /// 对象实例 /// 属性名称 /// public static object GetProperty(object obj, string name) { PropertyInfo fieldInfo = obj.GetType().GetProperty(name, bf); return fieldInfo.GetValue(obj, null); } /// /// 获取对象实例的属性列表 /// /// 对象实例 /// public static PropertyInfo[] GetProperties(object obj) { PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bf); return propertyInfos; } /// /// 把对象的属性和值,输出一个键值的字符串,如A=1&B=test /// /// 实体对象 /// 是否包含空白属性的键值 /// public static string ToNameValuePairs(object obj, bool includeEmptyProperties = true) { string result = ""; foreach (PropertyInfo p in obj.GetType().GetProperties()) { var objVal = p.GetValue(obj, null); var value = objVal != null ? objVal.ToString() : null; if (string.IsNullOrEmpty(value)) { if (includeEmptyProperties) { if (!string.IsNullOrEmpty(result)) { result += "&"; } result += string.Format("{0}={1}", p.Name, value); } } else { if (!string.IsNullOrEmpty(result)) { result += "&"; } result += string.Format("{0}={1}", p.Name, value); } } return result; } #endregion 属性字段设置 #region 获取Description /// /// 获取枚举字段的Description属性值 /// /// The value. /// return description or value.ToString() public static string GetDescription(Enum value) { return GetDescription(value, null); } /// /// Get The Enum Field Description using Description Attribute and /// objects to format the Description. /// /// Enum For Which description is required. /// An Object array containing zero or more objects to format. /// return null if DescriptionAttribute is not found or return type description public static string GetDescription(Enum value, params object[] args) { if (value == null) { throw new ArgumentNullException("value"); } string text1; FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); text1 = (attributes.Length > 0) ? attributes[0].Description : value.ToString(); if ((args != null) && (args.Length > 0)) { return string.Format(null, text1, args); } return text1; } /// /// 获取字段的Description属性值 /// /// Specified Member for which Info is Required /// return null if DescriptionAttribute is not found or return type description public static string GetDescription(MemberInfo member) { return GetDescription(member, null); } /// /// Get The Type Description using Description Attribute and /// objects to format the Description. /// /// Specified Member for which Info is Required /// An Object array containing zero or more objects to format. /// return if DescriptionAttribute is /// not found or return type description public static string GetDescription(MemberInfo member, params object[] args) { string text1; if (member == null) { throw new ArgumentNullException("member"); } if (member.IsDefined(typeof(DescriptionAttribute), false)) { DescriptionAttribute[] attributes = (DescriptionAttribute[])member.GetCustomAttributes(typeof(DescriptionAttribute), false); text1 = attributes[0].Description; } else { return String.Empty; } if ((args != null) && (args.Length > 0)) { return String.Format(null, text1, args); } return text1; } #endregion 获取Description #region 获取Attribute信息 /// /// 获取指定对象实例的attributes内容 /// /// The attribute Type for which the custom attributes are to be returned. /// the assembly in which the specified attribute is defined /// Attribute as Object or null if not found. public static object GetAttribute(Type attributeType, Assembly assembly) { if (attributeType == null) { throw new ArgumentNullException("attributeType"); } if (assembly == null) { throw new ArgumentNullException("assembly"); } if (assembly.IsDefined(attributeType, false)) { object[] attributes = assembly.GetCustomAttributes(attributeType, false); return attributes[0]; } return null; } /// /// 获取指定对象实例的attributes内容 /// /// The attribute Type for which the custom attributes are to be returned. /// the type on which the specified attribute is defined /// Attribute as Object or null if not found. public static object GetAttribute(Type attributeType, MemberInfo type) { return GetAttribute(attributeType, type, false); } /// /// Gets the specified object attributes for type as specified by type with option to serach parent /// /// The attribute Type for which the custom attributes are to be returned. /// the type on which the specified attribute is defined /// if set to [search parent]. /// /// Attribute as Object or null if not found. /// public static object GetAttribute(Type attributeType, MemberInfo type, bool searchParent) { if (attributeType == null) { return null; } if (type == null) { return null; } if (!(attributeType.IsSubclassOf(typeof(Attribute)))) { return null; } if (type.IsDefined(attributeType, searchParent)) { object[] attributes = type.GetCustomAttributes(attributeType, searchParent); if (attributes.Length > 0) { return attributes[0]; } } return null; } /// /// Gets the collection of all specified object attributes for type as specified by type /// /// The attribute Type for which the custom attributes are to be returned. /// the type on which the specified attribute is defined /// Attribute as Object or null if not found. public static object[] GetAttributes(Type attributeType, MemberInfo type) { return GetAttributes(attributeType, type, false); } /// /// Gets the collection of all specified object attributes for type as specified by type with option to serach parent /// /// The attribute Type for which the custom attributes are to be returned. /// the type on which the specified attribute is defined /// The attribute Type for which the custom attribute is to be returned. /// /// Attribute as Object or null if not found. /// public static object[] GetAttributes(Type attributeType, MemberInfo type, bool searchParent) { if (type == null) { return null; } if (attributeType == null) { return null; } if (!(attributeType.IsSubclassOf(typeof(Attribute)))) { return null; } if (type.IsDefined(attributeType, false)) { return type.GetCustomAttributes(attributeType, searchParent); } return null; } #endregion 获取Attribute信息 #region 资源获取 /// /// 根据资源名称获取图片资源流 /// /// /// public static Stream GetImageResource(string ResourceName) { Assembly asm = Assembly.GetExecutingAssembly(); return asm.GetManifestResourceStream(ResourceName); } /// /// 获取程序集资源的位图资源 /// /// 程序集中的某一对象类型 /// 资源的根名称。例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。 /// 资源项名称 public static Bitmap LoadBitmap(Type assemblyType, string resourceHolder, string imageName) { Assembly thisAssembly = Assembly.GetAssembly(assemblyType); ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly); return (Bitmap)rm.GetObject(imageName); } /// /// 获取程序集资源的文本资源 /// /// 程序集中的某一对象类型 /// 资源项名称 /// 资源的根名称。例如,名为“MyResource.en-US.resources”的资源文件的根名称为“MyResource”。 public static string GetStringRes(Type assemblyType, string resName, string resourceHolder) { Assembly thisAssembly = Assembly.GetAssembly(assemblyType); ResourceManager rm = new ResourceManager(resourceHolder, thisAssembly); return rm.GetString(resName); } /// /// 获取程序集嵌入资源的文本形式 /// /// 程序集中的某一对象类型 /// 字符集编码 /// 嵌入资源相对路径 /// 如没找到该资源则返回空字符 public static string GetManifestString(Type assemblyType, string charset, string ResName) { Assembly asm = Assembly.GetAssembly(assemblyType); Stream st = asm.GetManifestResourceStream(string.Concat(assemblyType.Namespace, ".", ResName.Replace("/", "."))); if (st == null) { return ""; } int iLen = (int)st.Length; byte[] bytes = new byte[iLen]; st.Read(bytes, 0, iLen); return (bytes != null) ? Encoding.GetEncoding(charset).GetString(bytes) : ""; } #endregion 资源获取 #region 创建对应实例 /// /// 创建对应实例 /// /// 类型 /// 对应实例 public static object CreateInstance(string type) { Type tmp = null; Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); for (int i = 0; i < assemblies.Length; i++) { tmp = assemblies[i].GetType(type); if (tmp != null) { return assemblies[i].CreateInstance(type); } } return null; //return Assembly.GetExecutingAssembly().CreateInstance(type); } /// /// 创建对应实例 /// /// 类型 /// 对应实例 public static object CreateInstance(Type type) { return CreateInstance(type.FullName); } #endregion 创建对应实例 } }