public void LoadAllMap(string key, List <Assembly> MappingAssemblies = null, LoadMapperMode loadMode = LoadMapperMode.FluentMapper) { //从缓存读取 ConcurrentDictionary <Type, IClassMapper> _Maps = ClassMapperCache.Get(key); if (_Maps != null) { SetAllMap(_Maps); return; } this.MappingAssemblies = MappingAssemblies; var entityTypes = GetAllMapType(MappingAssemblies, loadMode); foreach (var entityType in entityTypes) { GetMap(entityType); } //设置缓存 ClassMapperCache.Set(key, GetAllMap()); return; }
public List <Type> GetAllMapType(List <Assembly> MappingAssemblies = null, LoadMapperMode loadMode = LoadMapperMode.FluentMapper) { Func <Assembly, List <Type> > getTypeAll = a => { Type[] types = a.GetTypes(); return((from type in types where type != null && (type.GetInterface(typeof(IClassMapper <>).FullName) != null || type.GetCustomClassAttributes <TableAttribute>(false).Count > 0 ) select type).ToList()); }; //从fluent mapper方式获取 Func <Assembly, List <Type> > getType = a => { Type[] types = a.GetTypes(); return((from type in types let interfaceType = type.GetInterface(typeof(IClassMapper <>).FullName) where interfaceType != null //&& //interfaceType.GetGenericArguments()[0] == entityType select interfaceType.GetGenericArguments()[0]).ToList()); }; //从table attribute方式获取 Func <Assembly, List <Type> > getTypeFromAttr = a => { Type[] types = a.GetTypes(); return((from type in types let interfaceType = type.GetCustomClassAttributes <TableAttribute>(false) where interfaceType != null && interfaceType.Count > 0 && interfaceType[0].IgnoredMigrate == false //&& //interfaceType.GetGenericArguments()[0] == entityType select type).ToList()); }; //Func<Assembly, Type> getType = a => //{ // Type[] types = a.GetTypes(); // return (from type in types // let interfaceType = type.GetInterface(typeof(IClassMapper<>).FullName) // where // interfaceType != null && // interfaceType.GetGenericArguments()[0] == entityType // select type).FirstOrDefault(); //}; List <Type> result = new List <Type>(); List <Type> temp = null; if (MappingAssemblies != null && MappingAssemblies.Count > 0) { foreach (var mappingAssembly in MappingAssemblies) { try { if (loadMode == LoadMapperMode.FluentMapper) //fluent { temp = getType(mappingAssembly); if (temp != null && temp.Count > 0) { result.AddRange(temp); } } else if (loadMode == LoadMapperMode.AttributeMapper)//attribute { temp = getTypeFromAttr(mappingAssembly); if (temp != null && temp.Count > 0) { result.AddRange(temp); } } else if (loadMode == LoadMapperMode.SqlMapper)//sql map { } else { temp = getTypeAll(mappingAssembly); //获取所有方式 if (temp != null && temp.Count > 0) { result.AddRange(temp); } } } catch { continue; } } } else { var ass = AssemblyHelper.GetAllAssembly("*.dll"); ass.AddRange(AssemblyHelper.GetAllAssembly("*.exe")); ass.Remove(typeof(Database).Assembly); MappingAssemblies = ass; foreach (var mappingAssembly in ass) { try { if (loadMode == LoadMapperMode.FluentMapper) //fluent { temp = getType(mappingAssembly); if (temp != null && temp.Count > 0) { result.AddRange(temp); } } else if (loadMode == LoadMapperMode.AttributeMapper)//attribute { temp = getTypeFromAttr(mappingAssembly); if (temp != null && temp.Count > 0) { result.AddRange(temp); } } else if (loadMode == LoadMapperMode.SqlMapper)//sql map { } else { temp = getTypeAll(mappingAssembly); //获取所有方式 if (temp != null && temp.Count > 0) { result.AddRange(temp); } } } catch { continue; } } } result = result.Distinct().ToList(); return(result); }