public static void BuildProperties(TextFile tfStatic, TextFile tfInst, Type type, List <MemberInfoEx> properties, int slot) { for (int i = 0; i < properties.Count; i++) { MemberInfoEx infoEx = properties[i]; if (infoEx.Ignored) { continue; } PropertyInfo property = infoEx.member as PropertyInfo; ParameterInfo[] ps = property.GetIndexParameters(); string indexerParamA = string.Empty; string indexerParamB = string.Empty; string indexerParamC = string.Empty; for (int j = 0; j < ps.Length; j++) { indexerParamA += "ind" + j.ToString(); indexerParamB += "ind" + j.ToString() + ", "; if (j < ps.Length - 1) { indexerParamA += ", "; } indexerParamC += ", ind" + j.ToString(); } MethodInfo[] accessors = property.GetAccessors(); bool isStatic = accessors[0].IsStatic; // 特殊情况,当[]时,property.Name=Item string mName = Member_AddSuffix(property.Name, infoEx.GetOverloadIndex()); TextFile tf = isStatic ? tfStatic : tfInst; tf.Add("get{0}: function ({1}) {{ return CS.Call({2}, {3}, {4}, {5}{6}{7}); }},", mName, indexerParamA, (int)JSVCall.Oper.GET_PROPERTY, slot, i, (isStatic ? "true" : "false"), (isStatic ? "" : ", this"), indexerParamC); tf.Add("set{0}: function ({1}v) {{ return CS.Call({2}, {3}, {4}, {5}{6}{7}, v); }},", mName, indexerParamB, (int)JSVCall.Oper.SET_PROPERTY, slot, i, (isStatic ? "true" : "false"), (isStatic ? "" : ", this"), indexerParamC); } }
// can handle all methods public static void BuildMethods(TextFile tfStatic, TextFile tfInst, Type type, List <MemberInfoEx> methods, int slot) { for (int i = 0; i < methods.Count; i++) { MemberInfoEx infoEx = methods[i]; if (infoEx.Ignored) { continue; } MethodInfo method = infoEx.member as MethodInfo; StringBuilder sbFormalParam = new StringBuilder(); StringBuilder sbActualParam = new StringBuilder(); ParameterInfo[] paramS = method.GetParameters(); TextFile tfInitT = new TextFile(); int TCount = 0; // add T to formal param if (method.IsGenericMethodDefinition) { Type[] GAs = method.GetGenericArguments(); for (int j = 0; j < GAs.Length; j++) { sbFormalParam.AppendFormat("{0}", GAs[j].Name); if (j < GAs.Length - 1 || paramS.Length > 0) { sbFormalParam.Append(", "); } tfInitT.Add("var ${0} = Bridge.Reflection.getTypeFullName({0});", GAs[j].Name); sbActualParam.AppendFormat(", ${0}", GAs[j].Name); } } int L = paramS.Length; for (int j = 0; j < L; j++) { sbFormalParam.AppendFormat("a{0}/* {1} */{2}", j, paramS[j].ParameterType.Name, (j == L - 1 ? "" : ", ")); // 特殊处理 // 配合 UnityEngineManual.GameObject_AddComponent__Type if (j == 0 && type == typeof(GameObject) && method.Name == "AddComponent" && paramS.Length == 1 && paramS[j].ParameterType == typeof(Type)) { sbActualParam.AppendFormat(", Bridge.Reflection.getTypeFullName(a{0})", j); } else { sbActualParam.AppendFormat(", a{0}", j); } } //int TCount = method.GetGenericArguments().Length; string methodName = method.Name; // if (methodName == "ToString") { methodName = "toString"; } string mName = Member_AddSuffix(methodName, infoEx.GetOverloadIndex(), TCount); TextFile tf = method.IsStatic ? tfStatic : tfInst; string strReturn = string.Format("return CS.Call({0}, {1}, {2}, {3}{4});", (int)JSVCall.Oper.METHOD, slot, i, (method.IsStatic ? "true" : "false"), (method.IsStatic ? "" : ", this") + sbActualParam.ToString()); if (tfInitT.Ch.Count > 0) { tf.Add("{0}: function ({1}) {{", mName, sbFormalParam.ToString()) .In() .Add(tfInitT.Ch) .Add(strReturn) .Out() .Add("},"); } else { tf.Add("{0}: function ({1}) {{ {2} }},", mName, sbFormalParam.ToString(), strReturn); } } }
public static void BuildConstructors(TextFile tfInst, Type type, List <MemberInfoEx> constructors, int slot) { var argActual = new args(); var argFormal = new args(); for (int i = 0; i < constructors.Count; i++) { MemberInfoEx infoEx = constructors[i]; if (infoEx.Ignored) { continue; } ConstructorInfo con = infoEx.member as ConstructorInfo; TextFile tf = new TextFile(); ParameterInfo[] ps = con == null ? new ParameterInfo[0] : con.GetParameters(); argActual.Clear().Add( (int)JSVCall.Oper.CONSTRUCTOR, // OP slot, i, // NOTICE "true", // IsStatics "this" ); argFormal.Clear(); // add T to formal param Type[] GAs = null; if (type.IsGenericTypeDefinition) { GAs = type.GetGenericArguments(); for (int j = 0; j < GAs.Length; j++) { //argFormal.Add("t" + j + ""); argActual.AddFormat("${0}", GAs[j].Name); } } //StringBuilder sbFormalParam = new StringBuilder(); //StringBuilder sbActualParam = new StringBuilder(); for (int j = 0; j < ps.Length; j++) { argFormal.Add("a" + j.ToString()); argActual.Add("a" + j.ToString()); } string mName = Ctor_Name(infoEx.GetOverloadIndex()); // 特殊处理 - MonoBehaviour 不需要构造函数内容 if (type == typeof(MonoBehaviour)) { tf.Add("{0}: function ({1}) {{}},", mName, argFormal); } // 再次特殊处理 else if (type == typeof(WaitForSeconds)) { tf.Add("{0}: function ({1}) {{", mName, argFormal) .In() .Add("this.$totalTime = a0;") .Add("this.$elapsedTime = 0;") .Add("this.$finished = false;") .Out().Add("},"); } else { TextFile tfFun = tf.Add("{0}: function ({1}) {{", mName, argFormal) .In(); if (type.IsGenericTypeDefinition) { tfFun.Add("var $GAs = Bridge.Reflection.getGenericArguments(Bridge.getType(this));"); for (int j = 0; j < GAs.Length; j++) { tfFun.Add("var ${0} = Bridge.Reflection.getTypeFullName($GAs[{1}]);", GAs[j].Name, j); } } tfFun.Add("CS.Call({0});", argActual) .Out().Add("},"); } tfInst.Add(tf.Ch); } }
public static void BuildProperties(TextFile tfStatic, TextFile tfInst, TextFile tfAlias, Type[] declInterfs, Type type, List <MemberInfoEx> properties, int slot) { for (int i = 0; i < properties.Count; i++) { MemberInfoEx infoEx = properties[i]; if (infoEx.Ignored) { continue; } PropertyInfo property = infoEx.member as PropertyInfo; ParameterInfo[] ps = property.GetIndexParameters(); string indexerParamA = string.Empty; string indexerParamB = string.Empty; string indexerParamC = string.Empty; for (int j = 0; j < ps.Length; j++) { indexerParamA += "ind" + j.ToString(); indexerParamB += "ind" + j.ToString() + ", "; if (j < ps.Length - 1) { indexerParamA += ", "; } indexerParamC += ", ind" + j.ToString(); } MethodInfo[] accessors = property.GetAccessors(); bool isStatic = accessors[0].IsStatic; // 特殊情况,当[]时,property.Name=Item string mName = Pro_AddSuffix(property.Name, infoEx.GetOverloadIndex()); if (tfAlias != null) { Type iType; if (property.CanRead && shouldAddAlias(type, accessors[0], declInterfs, out iType)) { tfAlias.Add("\"get{0}\", \"{1}\",", mName, getAliasName(iType, "get" + mName)); } if (property.CanWrite && accessors.Length > 1 && shouldAddAlias(type, accessors[1], declInterfs, out iType)) { tfAlias.Add("\"set{0}\", \"{1}\",", mName, getAliasName(iType, "set" + mName)); } } TextFile tf = isStatic ? tfStatic : tfInst; if (property.CanRead) { tf.Add("get{0}: function ({1}) {{ return CS.Call({2}, {3}, {4}, {5}{6}{7}); }},", mName, indexerParamA, (int)JSVCall.Oper.GET_PROPERTY, slot, i, (isStatic ? "true" : "false"), (isStatic ? "" : ", this"), indexerParamC); } if (property.CanWrite) { tf.Add("set{0}: function ({1}v) {{ return CS.Call({2}, {3}, {4}, {5}{6}{7}, v); }},", mName, indexerParamB, (int)JSVCall.Oper.SET_PROPERTY, slot, i, (isStatic ? "true" : "false"), (isStatic ? "" : ", this"), indexerParamC); } } }