void GenerateClassSkip(Assembly assembly) { // 所有public的类 var types = assembly.GetExportedTypes().ToList(); types.RemoveAll(v => _skipClass.Contains(v)); // 所有需要生成的类 var gens = _methods.Values.GroupBy(v => v.method.ReflectedType.IsGenericType && !v.method.ReflectedType.IsGenericTypeDefinition ? v.method.ReflectedType.GetGenericTypeDefinition() : v.method.ReflectedType).Select(v => v.Key).Concat(_includeClass).GroupBy(v => v).ToDictionary(v => v.Key); var impls = CustomSettings.luaImplementList.ToDictionary(v => v); // 找到不需要生成的类 types.RemoveAll(v => impls.ContainsKey(v) || gens.ContainsKey(v) || !(v.IsClass || IsStruct(v)) || ToLuaExport.IsDelegateType(v)); foreach (var namespacePair in types.GroupBy(t => t.Namespace)) { var ns = namespacePair.Key; sb.AppendFormat("\t\t<namespace name=\"{0}\">\n", ns); foreach (var classTypePair in namespacePair) { var classType = classTypePair; GenerateBanedClass(classType); } sb.AppendFormat("\t\t</namespace>\n"); } }
static HashSet <Type> GetCustomTypeDelegates() { BindType[] list = CustomSettings.customTypeList; HashSet <Type> set = new HashSet <Type>(); BindingFlags binding = BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.Instance; for (int i = 0; i < list.Length; i++) { Type type = list[i].type; FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.SetField | binding); PropertyInfo[] props = type.GetProperties(BindingFlags.GetProperty | BindingFlags.SetProperty | binding); MethodInfo[] methods = null; if (type.IsInterface) { methods = type.GetMethods(); } else { methods = type.GetMethods(BindingFlags.Instance | binding); } for (int j = 0; j < fields.Length; j++) { Type t = fields[j].FieldType; if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } for (int j = 0; j < props.Length; j++) { Type t = props[j].PropertyType; if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } for (int j = 0; j < methods.Length; j++) { MethodInfo m = methods[j]; if (m.IsGenericMethod) { continue; } ParameterInfo[] pifs = m.GetParameters(); for (int k = 0; k < pifs.Length; k++) { Type t = pifs[k].ParameterType; if (t.IsByRef) { t = t.GetElementType(); } if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } } } return(set); }
/// <summary> /// 以 HashSet<Type> 形式获取 CustomSettings.customTypeList 中所有委托类型元素(字段、属性、方法参数)的类型 /// </summary> static HashSet <Type> GetCustomTypeDelegates() { // 以数组形式获取 CustomSettings.customTypeList 的所有元素 BindType[] list = CustomSettings.customTypeList; HashSet <Type> set = new HashSet <Type>(); // 指定 BindingFlags 为公共、静态、忽略大小写、包括实例 BindingFlags binding = BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.Instance; for (int i = 0; i < list.Length; i++) { Type type = list[i].type; // 根据 BindingFlags 指定规则获取字段 FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.SetField | binding); // 根据 BindingFlags 指定规则获取属性信息 PropertyInfo[] props = type.GetProperties(BindingFlags.GetProperty | BindingFlags.SetProperty | binding); MethodInfo[] methods = null; // 为 MethodInfo[] methods 获取 MethodInfo if (type.IsInterface) { methods = type.GetMethods(); } else { methods = type.GetMethods(BindingFlags.Instance | binding); } // 为 HashSet<Type> set 添加委托类型字段的类型 for (int j = 0; j < fields.Length; j++) { Type t = fields[j].FieldType; if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } // 为 HashSet<Type> set 添加委托类型属性的类型 for (int j = 0; j < props.Length; j++) { Type t = props[j].PropertyType; if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } for (int j = 0; j < methods.Length; j++) { MethodInfo m = methods[j]; // 如果是泛型方法直接跳过 if (m.IsGenericMethod) { continue; } // 获取当前元素的参数信息 ParameterInfo[] pifs = m.GetParameters(); // 为 HashSet<Type> set 添加委托类型参数的类型 for (int k = 0; k < pifs.Length; k++) { Type t = pifs[k].ParameterType; if (t.IsByRef) { t = t.GetElementType(); } if (ToLuaExport.IsDelegateType(t)) { set.Add(t); } } } } return(set); }