反射
1.通过反射创建对象 方法1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static IMessageReceiver CreateInstance(WeixinMessage weixinMessage) { IMessageReceiver result = null; MessageTypeAttribute messageTypeAttribute = null; EventTypeAttribute eventTypeAttribute = null; string weixinMessageTypeEnumTitle = EnumHelper.GetEnumTitle(weixinMessage.Type); foreach (Type type in MessageReceiverTypeList) { messageTypeAttribute = AssemblyHelper.GetTypeAttribute<MessageTypeAttribute>(type); if (messageTypeAttribute!=null&&messageTypeAttribute.Name == weixinMessageTypeEnumTitle) { if (messageTypeAttribute.Name == "Event") { string eventType = weixinMessage.Body.Event.Value.ToLower(); eventTypeAttribute = AssemblyHelper.GetTypeAttribute<EventTypeAttribute>(type); if(eventTypeAttribute.Name==eventType) { result = (IMessageReceiver)Activator.CreateInstance(type, true);//根据类型创建实例 break; } } else { result = (IMessageReceiver)Activator.CreateInstance(type, true);//根据类型创建实例 break; } } } if (result != null) { result.Receive(weixinMessage); return result; } else { throw new Exception("not support this message type"); } }
2.通过反射创建对象 方法2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static MessageBehavior Analysis(IMessageReceiver messageReceiver) { MessageBehaviorAttribute messageBehaviorAttribute = null; var messageReceiverType = messageReceiver.GetType(); var attrs = AssemblyHelper.GetAllTypeAttributes(messageReceiver.GetType()); foreach(var attr in attrs) { if (attr.GetType() == typeof(MessageBehaviorAttribute)) { messageBehaviorAttribute = (MessageBehaviorAttribute)attr; break; } } Type messageBehaviorType = MessageBehaviorTypeList.Where(u => u.Name == messageBehaviorAttribute.Name).FirstOrDefault(); if(messageBehaviorType!=null) { object[] parameters = new object[1]; parameters[0] = messageReceiver; Assembly assembly = Assembly.GetExecutingAssembly(); MessageBehavior behavior = (MessageBehavior)assembly.CreateInstance(messageBehaviorType.FullName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null);// return behavior; } return null; }
3.得到入口程序集,兼容Web和Winform
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static Assembly GetEntryAssembly() { var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) return entryAssembly; if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.ApplicationInstance == null) return Assembly.GetExecutingAssembly(); var type = System.Web.HttpContext.Current.ApplicationInstance.GetType(); while (type != null && type.Namespace == "ASP") { type = type.BaseType; } return type == null ? null : type.Assembly; }
4.得到当前应用程序的根目录
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static string GetBaseDirectory() { var baseDirectory = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; if (AppDomain.CurrentDomain.SetupInformation.PrivateBinPath == null) baseDirectory = AppDomain.CurrentDomain.BaseDirectory; return baseDirectory; }
5.扫描程序集找到继承了某基类的所有子类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static List<Type> FindTypeByInheritType(Type inheritType, string searchpattern = "*.dll") { var result = new List<Type>(); Type attr = inheritType; string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { if (type.BaseType == inheritType) { result.Add(type); } } } return result; }
6.扫描程序集找到某接口的所有子类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static List<Type> FindAllTypeByInterface<T>(string searchpattern = "*.dll") where T : class { var result = new List<Type>(); var interfaceType = typeof(T); string domain = GetBaseDirectory(); string[] dllFiles = Directory.GetFiles(domain, searchpattern, SearchOption.TopDirectoryOnly); foreach (string dllFileName in dllFiles) { foreach (Type type in Assembly.LoadFrom(dllFileName).GetLoadableTypes()) { if (type.GetInterface(interfaceType.Name) != null && !type.IsInterface && !type.IsAbstract) { result.Add(type); } } } return result; }