/// <summary> /// 通过附加特性获取所有的附加类型 /// </summary> /// <param name="inputType">输入的类型</param> private void AutoGetAttributeTypes(Type inputType) { try { Attribute oneAttribute = inputType.GetCustomAttribute(typeof(ClassSerializeTypeAttribute)); if (null == oneAttribute) { return; } ClassSerializeTypeAttribute useClassSeriializeAttribute = oneAttribute as ClassSerializeTypeAttribute; if (null == useClassSeriializeAttribute) { return; } Type[] useTypes = useClassSeriializeAttribute.ThisTypes; if (null == useTypes) { return; } string tempString; foreach (var oneType in useTypes) { if (null == oneType) { continue; } //调整目前缓存 AssemblyUtility.GetAllInnerTypes(oneType, ref m_lstType, ref m_dicType); } } catch (Exception) { ; } }
/// <summary> /// 微软架构对接接口负责Type的查找 /// </summary> /// <param name="assemblyName">程序集名</param> /// <param name="typeName">类型名</param> /// <returns>找到的type</returns> public override Type BindToType(string assemblyName, string typeName) { string tempString = assemblyName + typeName; Type returnValue = null; Assembly useAssembly = null; //若已被缓存 if (m_dicType.TryGetValue(tempString, out returnValue)) { return(returnValue); } //寻找输入的程序集 foreach (var oneAssembly in m_lstAllAssembly) { if (oneAssembly.GetName().FullName.Equals(assemblyName)) { useAssembly = oneAssembly; break; } } //若找到首先尝试在程序集中选 if (null != useAssembly) { returnValue = AssemblyUtility.TryGetType(typeName, useAssembly); } //若没找到在全程序集中寻找 if (null == returnValue) { foreach (var oneAssembly in m_lstAllAssembly) { returnValue = AssemblyUtility.TryGetType(typeName, oneAssembly); if (null != returnValue) { break; } } } //若仍没找到则在涉及类中寻找 if (null == returnValue) { foreach (var oneType in m_lstType) { if (oneType.FullName.Equals(typeName)) { returnValue = oneType; } } } //泛型制备 if (null == returnValue) { returnValue = GetGenericType(assemblyName, typeName); } if (null != returnValue) { //加入缓存 m_dicType.Add(tempString, returnValue); } return(returnValue); }
/// <summary> /// 获得所有程序集中符合特定标签的类 /// </summary> /// <returns>找到的类</returns> private List <Type> GetAllTypes() { List <Type> lstReutrnValue = new List <Type>(); List <Assembly> lstAllAssembly = new List <Assembly>(); //获得所有程序集 lstAllAssembly = AssemblyUtility.GetAllAssembly(); //获得程序集内所有的类型 foreach (var tempAssembly in lstAllAssembly) { //防止反射异常 try { //获得所有被标签定义的类 foreach (var oneType in tempAssembly.GetTypes()) { if (null != oneType && typeof(ITransformerTag).IsAssignableFrom(oneType)) { lstReutrnValue.Add(oneType); } } } catch (Exception) { ; } } return(lstReutrnValue); }
/// <summary> /// 获得自身Type列表 /// </summary> /// <param name="input"></param> internal UseBinder(Type input) { if (null != input) { if (!m_hashSetType.Contains(input)) { //根据特性放入依赖类 AutoGetAttributeTypes(input); //根据特性设置转换器 AutoSetTransformer(input); //hash表存入 m_hashSetType.Add(input); } //加载输入所有涉及的Type AssemblyUtility.GetAllInnerTypes(input, ref m_lstType, ref m_dicType); } }
/// <summary> /// 静态构造,加载程序集 /// </summary> static UseBinder() { //静态加载所有程序集 m_lstAllAssembly = AssemblyUtility.GetAllAssembly(); }