/// <summary> /// 是否已经手工绑定。 /// </summary> /// <param name="type"></param> /// <returns></returns> public static bool IsSkipCreator(Type type) { if (type == null) { return(true); } if (Equals(type, typeof(Type))) { return(true); } if (type.IsArray) { return(IsSkipCreator(type.GetElementType())); } ASBinCode.RunTimeDataType rttype = MethodNativeCodeCreator.GetAS3Runtimetype(type); if (rttype < ASBinCode.RunTimeDataType._OBJECT) { return(true); } string fn = type.FullName; if (fn == "System.ValueType") { return(true); } if (SkipCreateTypes.Contains(fn)) { return(true); } return(false); }
public string GetAS3TypeString(Type type, Dictionary <Type, string> typeimports, Type checktype, System.Reflection.MethodBase checkmethod, System.Reflection.ParameterInfo checkparameter) { if (type.IsByRef) { if (!typeimports.ContainsValue("import as3runtime.RefOutStore;")) { typeimports.Add(typeof(ASRuntime.nativefuncs.linksystem.RefOutStore), "import as3runtime.RefOutStore;"); } return(GetAS3TypeString(type.GetElementType(), typeimports, checktype, checkmethod, checkparameter)); } if (checkparameter != null) { if (checkmethod.DeclaringType == typeof(System.Collections.IList) || isMapToInterface(checkmethod, typeof(System.Collections.IList)) != null ) { if (isMapToInterface(checkmethod, typeof(System.Collections.IList)) != null) { checkmethod = isMapToInterface(checkmethod, typeof(System.Collections.IList)); } if (checkmethod.Name == "set_Item" && checkparameter.Name == "value") { return("*"); } else if (checkmethod.Name == "Add") { return("*"); } else if (checkmethod.Name == "Contains") { return("*"); } else if (checkmethod.Name == "IndexOf") { return("*"); } else if (checkmethod.Name == "Insert" && checkparameter.Name == "value") { return("*"); } else if (checkmethod.Name == "Remove") { return("*"); } } if (checkmethod.DeclaringType == typeof(System.Collections.IDictionary) || isMapToInterface(checkmethod, typeof(System.Collections.IDictionary)) != null ) { if (isMapToInterface(checkmethod, typeof(System.Collections.IDictionary)) != null) { checkmethod = isMapToInterface(checkmethod, typeof(System.Collections.IDictionary)); } if (checkmethod.Name == "get_Item" && checkparameter.Name == "key") { return("Object"); } else if (checkmethod.Name == "set_Item" && checkparameter.Name == "value") { return("*"); } else if (checkmethod.Name == "set_Item" && checkparameter.Name == "key") { return("Object"); } else if (checkmethod.Name == "Add" && checkparameter.Name == "key") { return("Object"); } else if (checkmethod.Name == "Add" && checkparameter.Name == "value") { return("*"); } else if (checkmethod.Name == "Contains") { return("Object"); } else if (checkmethod.Name == "Remove") { return("Object"); } } } if (checktype == typeof(System.Collections.IEnumerator)) { if (checkmethod.Name == "System.Collections.IEnumerator.get_Current" || checkmethod.Name == "get_Current" ) { return("*"); } } else if (checktype == typeof(System.Collections.IList)) { if (checkmethod.Name == "get_Item" || checkmethod.Name == "System.Collections.IList.get_Item" ) { return("*"); } } else if (checktype == typeof(System.Collections.IDictionary)) { if (checkmethod.Name == "get_Item" || checkmethod.Name == "System.Collections.IDictionary.get_Item" ) { return("*"); } } else if (checktype == typeof(System.Collections.IDictionaryEnumerator)) { if (checkmethod.Name == "get_Key" || isMapToInterface(checkmethod, typeof(System.Collections.IDictionaryEnumerator)) != null ) { return("Object"); } else if (checkmethod.Name == "get_Value" || isMapToInterface(checkmethod, typeof(System.Collections.IDictionaryEnumerator)) != null ) { return("*"); } } if (type.Equals(typeof(ASBinCode.RunTimeValueBase))) { return("*"); } ASBinCode.RunTimeDataType rttype = MethodNativeCodeCreator.GetAS3Runtimetype(type); if (rttype == ASBinCode.RunTimeDataType.fun_void) { return("void"); } else if (rttype == ASBinCode.RunTimeDataType.rt_number) { return("Number"); } else if (rttype == ASBinCode.RunTimeDataType.rt_int) { return("int"); } else if (rttype == ASBinCode.RunTimeDataType.rt_uint) { return("uint"); } else if (rttype == ASBinCode.RunTimeDataType.rt_boolean) { return("Boolean"); } else if (rttype == ASBinCode.RunTimeDataType.rt_string) { return("String"); } do { if (type == typeof(Type)) { break; } if (type == typeof(Array) || type.IsArray) { if (!typeimports.ContainsValue("import system._Array_;")) { typeimports.Add(typeof(Array), "import system._Array_;"); } } else if (this.type.Namespace != type.Namespace) { if (!typeimports.ContainsKey(type)) { typeimports.Add(type, "import " + type.Namespace.ToLower() + "." + GetAS3ClassOrInterfaceName(type) + ";"); } } } while (false); string name = GetAS3ClassOrInterfaceName(type); return(name); }
/// <summary> /// 返回如果在成员中涉及这种类型是否要跳过 /// </summary> /// <param name="type"></param> /// <returns></returns> public static bool IsSkipType(Type type, bool includeref = false) { if (type == null) { return(true); } var c = CustomAttributeData.GetCustomAttributes(type); foreach (CustomAttributeData item in c) { if (item.Constructor.DeclaringType == typeof(System.ObsoleteAttribute)) { return(true); } } //object[] objs = type.GetCustomAttributes(typeof(System.ObsoleteAttribute), false); //if (objs.Length > 0) //{ // return true; //} if (Equals(type, typeof(Type))) //Type会转换为Class { return(false); } if (type.IsCOMObject) { return(true); } if (Equals(type, typeof(Enum))) { return(true); } if (type.IsEnum) { foreach (var item in NotCreateNameSpace) { if (type.Namespace == item) { return(true); } if (type.Namespace.StartsWith(item + ".")) { return(true); } } if (NotCreateTypes.Contains(type.FullName)) { return(true); } return(false); } if (type.IsByRef) { if (!includeref) { return(true); } else if (type.HasElementType) { return(IsSkipType(type.GetElementType())); } } if (type.IsNotPublic) { return(true); } if (type.IsNested) { if (IsSkipType(type.DeclaringType)) { return(true); } } if (type.IsGenericTypeDefinition) { return(true); } if (type.IsGenericType && type.IsInterface) { return(true); } if (IsDelegate(type)) { //var m = GetDelegateMethodInfo(type); //var n=GetSharpTypeName(type); //var k = NativeCodeCreatorBase.GetTypeFullName(type); var m = GetDelegateMethodInfo(type); //***如果该委托包含要跳过的类型,则跳过**** if (m.IsGenericMethod) { return(true); } if (m.ReturnType.IsGenericTypeDefinition) { return(true); } var rt = MethodNativeCodeCreator.GetAS3Runtimetype(m.ReturnType); if (rt > ASBinCode.RunTimeDataType.unknown) { if (IsSkipType(m.ReturnType)) { return(true); } } var paras = m.GetParameters(); foreach (var p in paras) { if (p.IsOut) { return(true); } if (p.ParameterType.IsByRef) { return(true); } //if (p.ParameterType.IsGenericType) if (p.ParameterType.IsGenericTypeDefinition) { return(true); } if (p.IsOptional) { if (p.RawDefaultValue != null) { var rrt = MethodNativeCodeCreator.GetAS3Runtimetype(p.ParameterType); if (rrt > ASBinCode.RunTimeDataType.unknown) { return(true); } } } if (IsSkipType(p.ParameterType)) { return(true); } } return(false); } //if (type.IsGenericType) //{ // return true; //} if (type.IsArray) { return(IsSkipType(type.GetElementType())); } foreach (var item in NotCreateNameSpace) { if (type.Namespace == item) { return(true); } if (type.Namespace.StartsWith(item + ".")) { return(true); } } if (NotCreateTypes.Contains(type.FullName)) { return(true); } if (Equals(type, typeof(TypedReference))) { return(true); } if (type.BaseType != null) { return(IsSkipType(type.BaseType)); } return(false); }
public InterfaceCreator(Type interfacetype, string as3apidocpath, string csharpnativecodepath, Dictionary <TypeKey, CreatorBase> typeCreators, string linkcodenamespace ) : base(interfacetype, as3apidocpath, csharpnativecodepath, linkcodenamespace) { if (!interfacetype.IsInterface) { throw new ArgumentException("类型不是接口"); } if (interfacetype.IsGenericType) { throw new ArgumentException("不支持泛型接口"); } if (IsSkipCreator(interfacetype) ) { throw new ArgumentException("接口已配置为跳过"); } if (IsSkipType(interfacetype)) { throw new ArgumentException("接口已配置为不生成"); } name = GetAS3ClassOrInterfaceName(interfacetype); var exts = GetExtendInterfaces(interfacetype); foreach (var item in exts) { if (!typeCreators.ContainsKey(new TypeKey(item)) && !IsSkipCreator(item) && !IsSkipType(item)) { typeCreators.Add(new TypeKey(item), null); typeCreators[new TypeKey(item)] = new InterfaceCreator(item, as3apidocpath, csharpnativecodepath, typeCreators, linkcodenamespace); } } if (exts.Count > 1) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(interfacetype + "检查到接口多继承,这里使用第一个"); Console.ResetColor(); super = exts[0]; } else if (exts.Count == 1) { super = exts[0]; } //****分析引用到的类型**** var methods = interfacetype.GetMethods(); foreach (var item in methods) { if (IsObsolete(item, type)) { continue; } if (item.IsGenericMethod) { continue; } if (item.ReturnType.IsGenericTypeDefinition) { continue; } if (IsSkipMember(item)) { continue; } var rt = MethodNativeCodeCreator.GetAS3Runtimetype(item.ReturnType); if (rt > ASBinCode.RunTimeDataType.unknown) { if (IsSkipType(item.ReturnType)) { continue; } MakeCreator(item.ReturnType, typeCreators); } bool hasoptional = false; bool parapass = true; var paras = item.GetParameters(); foreach (var p in paras) { if (p.IsOut) { parapass = false; break; } if (p.ParameterType.IsByRef) { parapass = false; break; } if (p.ParameterType.IsGenericTypeDefinition) { parapass = false; break; } if (p.IsOptional) { hasoptional = true; if (p.RawDefaultValue != null) { var rrt = MethodNativeCodeCreator.GetAS3Runtimetype(p.ParameterType); if (rrt > ASBinCode.RunTimeDataType.unknown) { parapass = false; break; } } } else { if (hasoptional) { parapass = false; break; } } if (IsSkipType(p.ParameterType, true)) { parapass = false; break; } var pt = MethodNativeCodeCreator.GetAS3Runtimetype(p.ParameterType); if (pt > ASBinCode.RunTimeDataType.unknown) { var mt = p.ParameterType; if (p.ParameterType.IsByRef) { mt = p.ParameterType.GetElementType(); } MakeCreator(mt, typeCreators); } } if (parapass) { methodlist.Add(item); } } }
public override string Create() { Dictionary <Type, string> typeimports = new Dictionary <Type, string>(); StringBuilder nativefunc = new StringBuilder(); //GenNativeFuncImport(nativefunc); GenNativeFuncNameSpaceAndClass(nativefunc); BeginRegFunction(nativefunc); List <string> regfunctions = new List <string>(); List <string> nativefuncClasses = new List <string>(); StringBuilder as3api = new StringBuilder(); GenAS3FileHead(as3api); as3api.AppendLine("@imports"); as3api.AppendLine(); as3api.AppendFormat("\t[link_system_interface({0})]", GetCreatorNativeFuncName(type)); as3api.AppendLine(); as3api.AppendFormat("\tpublic interface {0}", name); regfunctions.Add( "\t\t\tbin.regNativeFunction(LinkSystem_Buildin.getCreator(\"" + GetCreatorNativeFuncName(type) + "\", default(" + NativeCodeCreatorBase.GetTypeFullName(type) + ")));"); if (super != null) { as3api.AppendFormat(" extends {0}", GetAS3TypeString(super, typeimports, null, null, null)); } as3api.AppendLine(); as3api.AppendLine("\t{"); bool existsindexgetter = false; bool existsindexsetter = false; foreach (var method in methodlist) { //if (method.IsGenericMethod) //{ // Console.WriteLine("跳过泛型方法"+method.ToString()); // continue; //} //if (IsSkipType(method.ReturnType)) //{ // Console.WriteLine(method.ToString() + "返回类型被配置为需要跳过"); // continue; //} var paras = method.GetParameters(); //bool parachecked = true; //foreach (var para in paras) //{ // if (para.IsOut) // { // Console.WriteLine(method.ToString()+"参数"+ para.Position+" " +para+ "为out,跳过"); // parachecked=false; // break; // } // if (para.ParameterType.IsByRef) // { // Console.WriteLine(method.ToString() + "参数" + para.Position + " " + para + "为byref,跳过"); // parachecked = false; // break; // } // if (para.ParameterType.IsGenericType) // { // Console.WriteLine(method.ToString() + "参数" + para.Position + " " + para + "为泛型,跳过"); // parachecked = false; // break; // } // if (para.IsOptional) // { // if (para.RawDefaultValue != null) // { // var rt = MethodNativeCodeCreator.GetAS3Runtimetype(para.ParameterType); // if (rt > ASBinCode.RunTimeDataType.unknown) // { // Console.WriteLine(method.ToString() + "参数" + para.Position + " " + para + "为可选,并且默认值不是基本类型"); // parachecked = false; // break; // } // } // } // if (IsSkipType(para.ParameterType)) // { // Console.WriteLine(method.ToString() + "参数" + para.Position + " " + para + "被配置为需要跳过"); // parachecked = false; // break; // } //} //if (!parachecked) //{ // continue; //} if (isMethodSkip(method)) { Console.WriteLine(method.ToString() + "有参数或返回值被配置为需要跳过"); continue; } string returntype = GetAS3TypeString(method.ReturnType, typeimports, type, method, null); string nativefunName = GetMethodNativeFunctionName(method, type, null, null); System.Reflection.PropertyInfo pinfo; if (MethodNativeCodeCreator.CheckIsIndexerGetter(method, type, out pinfo) && !existsindexgetter) { existsindexgetter = true; //****索引器**** as3api.Append("\t\t"); as3api.AppendLine("[get_this_item];"); as3api.Append("\t\t"); as3api.AppendFormat("[native,{0}];", nativefunName); as3api.AppendLine(); as3api.Append("\t\t"); var name = GetMethodName(method.Name, method, type, null, null); as3api.Append("function " + name); } else if (MethodNativeCodeCreator.CheckIsGetter(method, type, out pinfo)) { as3api.Append("\t\t"); as3api.AppendFormat("[native,{0}]", nativefunName); as3api.AppendLine(); as3api.Append("\t\t"); as3api.Append("function get "); as3api.Append(GetMethodName(pinfo.Name, method, type, null, null)); } else if (MethodNativeCodeCreator.CheckIsIndexerSetter(method, type, out pinfo) && !existsindexsetter) { existsindexsetter = true; //****索引器**** as3api.Append("\t\t"); as3api.AppendLine("[set_this_item];"); as3api.Append("\t\t"); as3api.AppendFormat("[native,{0}];", nativefunName); as3api.AppendLine(); as3api.Append("\t\t"); var name = GetMethodName(method.Name, method, type, null, null); as3api.Append("function " + name); var temp = paras[0]; paras[0] = paras[1]; paras[1] = temp; } else if (MethodNativeCodeCreator.CheckIsSetter(method, type, out pinfo)) { as3api.Append("\t\t"); as3api.AppendFormat("[native,{0}]", nativefunName); as3api.AppendLine(); as3api.Append("\t\t"); as3api.Append("function set "); as3api.Append(GetMethodName(pinfo.Name, method, type, null, null)); } else { as3api.Append("\t\t"); as3api.AppendFormat("[native,{0}]", nativefunName); as3api.AppendLine(); as3api.Append("\t\t"); as3api.Append("function "); as3api.Append(GetMethodName(method.Name, method, type, null, null)); } as3api.Append("("); for (int i = 0; i < paras.Length; i++) { var para = paras[i]; as3api.Append(para.Name); if (as3keywords.ContainsKey(para.Name)) { as3api.Append("_"); } as3api.Append(":"); as3api.Append(GetAS3TypeString(para.ParameterType, typeimports, null, method, para)); if (para.IsOptional) { as3api.Append("="); if (para.RawDefaultValue != null) { var rt = MethodNativeCodeCreator.GetAS3Runtimetype(para.ParameterType); if (rt == ASBinCode.RunTimeDataType.rt_string) { string str = para.RawDefaultValue.ToString(); str = str.Replace("\\", "\\\\"); str = str.Replace("\"", "\\\""); as3api.Append("\"" + str + "\""); } else { as3api.Append(para.RawDefaultValue.ToString()); } } else { as3api.Append("null"); } } if (i < paras.Length - 1) { as3api.Append(","); } } as3api.Append(")"); as3api.Append(":"); as3api.Append(returntype); as3api.AppendLine(";"); as3api.AppendLine(); //***编写方法的本地代码*** //****检测是否可以用反射。*** bool checkreflection = true; if (method.ReturnType.IsByRef) { checkreflection = false; } var param = method.GetParameters(); foreach (var item in param) { if (item.ParameterType.IsByRef) { checkreflection = false; } if (item.ParameterType.Equals(typeof(Type))) { checkreflection = false; } //if (item.ParameterType.IsValueType) // checkreflection = false; } if (method.ReturnType.Equals(typeof(Type))) { checkreflection = false; } if (checkreflection && false) { bool needswap = false; System.Reflection.PropertyInfo propertyInfo; if (MethodNativeCodeCreator.CheckIsIndexerSetter((System.Reflection.MethodInfo)method, method.DeclaringType, out propertyInfo) && param.Length == 2) { needswap = true; } string returntypestr; if (method.ReturnType.IsValueType && !method.ReturnType.Equals(typeof(void))) { returntypestr = NativeCodeCreatorBase.GetTypeFullName(method.ReturnType); } else { returntypestr = "object"; } string regrefl = string.Format("\t\t\tbin.regNativeFunction(new ReflectionInvokeNativeFunction<" + returntypestr + ">(" + (method.IsStatic ? "true" : "false") + "," + (needswap ? "true" : "false") + "," + param.Length + ",\"{0}\",\"{1}\",", nativefunName, method.Name); regrefl += GetReflectionInvokeNativeFunctionTypeCodeString(method.ReturnType) + " ,\n\t\t\t\t"; regrefl += "new int[]"; regrefl += "{"; if (needswap) { regrefl += GetReflectionInvokeNativeFunctionTypeCodeString(param[1].ParameterType); regrefl += ",\n\t\t\t\t"; regrefl += GetReflectionInvokeNativeFunctionTypeCodeString(param[0].ParameterType); } else { for (int i = 0; i < param.Length; i++) { regrefl += GetReflectionInvokeNativeFunctionTypeCodeString(param[i].ParameterType); if (i < param.Length - 1) { regrefl += ",\n\t\t\t\t"; } } } regrefl += "}))"; regrefl += ";"; regfunctions.Add(regrefl); } else { regfunctions.Add(string.Format("\t\t\tbin.regNativeFunction(\"{0}\",\"{1}\");", LinkCodeNampScapePart + nativefunName, nativefunName)); MethodNativeCodeCreator mc = new MethodNativeCodeCreator(nativefunName, method, type); nativefuncClasses.Add(mc.GetCode()); } } as3api.AppendLine("\t}"); EndAS3File(as3api); string imports = string.Empty; foreach (var item in typeimports.Values) { imports += "\t" + item + "\n"; } as3api.Replace("@imports", imports); for (int i = 0; i < regfunctions.Count; i++) { nativefunc.AppendLine(regfunctions[i]); } EndRegFunction(nativefunc); for (int i = 0; i < nativefuncClasses.Count; i++) { nativefunc.AppendLine(nativefuncClasses[i]); } EndNativeFuncClass(nativefunc); //Console.WriteLine(as3api.ToString()); //Console.WriteLine(nativefunc.ToString()); string as3file = as3apidocpath + "/" + GetPackageName(type).Replace(".", "/") + "/" + name + ".as"; string nativefunfile = csharpnativecodepath + "/" + GetNativeFunctionClassName(type) + ".cs"; System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(as3file)); System.IO.File.WriteAllText(as3file, as3api.ToString()); System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(nativefunfile)); StringBuilder usingcode = new StringBuilder(); GenNativeFuncImport(usingcode); System.IO.File.WriteAllText(nativefunfile, usingcode.ToString() + nativefunc.ToString()); return(nativefunc.ToString()); }
public static bool isMethodSkip(System.Reflection.MethodInfo method) { var item = method; if (IsObsolete(item, method.DeclaringType)) { return(true); } if (item.IsGenericMethod) { return(true); } if (item.ReturnType.IsGenericTypeDefinition) { return(true); } if (IsSkipMember(item)) { return(true); } var rt = MethodNativeCodeCreator.GetAS3Runtimetype(item.ReturnType); if (rt > ASBinCode.RunTimeDataType.unknown) { if (IsSkipType(method.ReturnType)) { return(true); } } bool hasoptional = false; var paras = item.GetParameters(); foreach (var p in paras) { if (p.IsOut) { return(true); } if (p.ParameterType.IsByRef) { return(true); } if (p.ParameterType.IsGenericTypeDefinition) { return(true); } if (p.IsOptional) { hasoptional = true; if (p.RawDefaultValue != null) { var rrt = MethodNativeCodeCreator.GetAS3Runtimetype(p.ParameterType); if (rrt > ASBinCode.RunTimeDataType.unknown) { return(true); } } } else { if (hasoptional) { return(true); } } if (IsSkipType(p.ParameterType, true)) { return(true); } } return(false); //if (IsObsolete(method,method.DeclaringType)) //{ // return true; //} //if (method.IsGenericMethod) //{ // return true; //} //if (IsSkipType(method.ReturnType)) //{ // return true; //} //var paras = method.GetParameters(); //foreach (var para in paras) //{ // if (para.IsOut) // { // return true; // } // if (para.ParameterType.IsByRef) // { // return true; // } // if (para.ParameterType.IsGenericType) // { // return true; // } // if (para.IsOptional) // { // if (para.RawDefaultValue != null) // { // var rt = MethodNativeCodeCreator.GetAS3Runtimetype(para.ParameterType); // if (rt > ASBinCode.RunTimeDataType.unknown) // { // return true; // } // } // } // if (IsSkipType(para.ParameterType)) // { // return true; // } //} //return false; }