//目的,想自己生成实体类,不走dc的生成机制,所以研究一下此方法。
public void GenerateEntities(string generatedAssemblyFile) { lock (this.lockObjectForDC) {
//此数组为要生成代码的接口类型 Type[] entityInterfaces = this.entitiesToGenerateInfo.GetEntityInterfaces(); if (!this.IsAlreadyBuild && (entityInterfaces.Length > 0)) {
//生成过了,且有实体接口,有文件的情况下,直接加载 if (File.Exists(generatedAssemblyFile)) { this.generatedAssembly = Assembly.LoadFrom(generatedAssemblyFile); } else {
//没有文件,马上生成一个。 DCBuilder builder = new DCBuilder(this.typesInfo); builder.Setup(this.entitiesToGenerateInfo, this.customLogics, this.existingImplementorsInfo); this.generatedAssembly = builder.GetAssembly(generatedAssemblyFile); } this.ProcessGeneratedAssembly(this.generatedAssembly);
//遍历所有的实体类,即接口定义 Dictionary<Type, object> dictionary = new Dictionary<Type, object>(); foreach (Type type in entityInterfaces) {
//没处理过的 if (!dictionary.ContainsKey(type)) { dictionary.Add(type, null);
//标记为处理过。
//处理“基”接口 foreach (Type type2 in type.GetInterfaces()) {
//已经注册过的实体类中包含这个接,且,没处理过此接口时,把基接口也加进来 if (this.registeredEntityTypes.Contains(type2) && !dictionary.ContainsKey(type2)) { dictionary.Add(type2, null); } } } }
foreach (Type type3 in dictionary.Keys) {
//上面收集到了所有需要用到的接口,刷新类型信息。使用实际类型查找接口 this.typesInfo.RefreshInfo(type3); Type entityTypeByInterface = this.GetEntityTypeByInterface(type3); if (entityTypeByInterface != null) { this.typesInfo.RefreshInfo(entityTypeByInterface); foreach (TypeInfo info in this.typesInfo.FindTypeInfo(entityTypeByInterface).RequiredTypes) { info.Refresh(true); } } Type dataTypeByInterface = this.GetDataTypeByInterface(type3); if (dataTypeByInterface != null) { this.typesInfo.RefreshInfo(dataTypeByInterface); } }
foreach (Type type6 in this.generatedAssembly.GetExportedTypes()) { if (this.TypeIsKnown(type6)) { TypeInfo info2 = this.typesInfo.FindTypeInfo(type6); if ((info2 != null) && (info2.Source != this)) { info2.Source = this; this.typesInfo.RefreshInfo(type6);
//类typeinfo附source的值,即TypeInfoSource } } } } } }