public ILMethodInfo clone() { ILMethodInfo re = new ILMethodInfo(); re.name = name; re.returnType = returnType; re.visitType = visitType; re.isAbstract = isAbstract; re.needBaseCall = needBaseCall; re.args = args; re._key = _key; return(re); }
private void doAddMethod(ILMethodInfo method) { SSet <string> allowSet = _clsAllowMethods.get(iCls.clsName); //不在允许列表 if (allowSet != null && !allowSet.contains(method.name)) { return; } SSet <string> ignoreSet = _clsIgnoreMethods.get(iCls.clsName); //在允屏蔽列表 if (ignoreSet != null && ignoreSet.contains(method.name)) { return; } iCls.toAddMethod(method); }
public void init() { if (_inited) { return; } _inited = true; iCls = new ILClassInfo(clsName); Type baseType = clsType.BaseType; if (baseType != null) { baseTypeInfo = getTypeInfo(baseType.Name); if (baseTypeInfo != null) { // //类型居然不匹配 // if(bInfo.clsType!=baseType) // { // Ctrl.throwError("类型居然不匹配",baseType.Name); // return; // } if (baseTypeInfo.needHotfixChildren) { //从基类传递 hasHotfixMark = baseTypeInfo.hasHotfixMark; needFactory = baseTypeInfo.needFactory; } //添加基类进来 foreach (ILMethodInfo ilMethodInfo in baseTypeInfo.iCls.methods) { if (ilMethodInfo.isAbstract) { doAddMethod(ilMethodInfo.clone()); } else { doAddMethod(ilMethodInfo); } } } } //接口和枚举除外 if (!clsType.IsInterface && !clsType.IsEnum) { //是否虚类 iCls.isAbstract = clsType.IsAbstract; Attribute customAttribute = Attribute.GetCustomAttribute(clsType, typeof(Hotfix), false); if (customAttribute != null) { Hotfix hotfixAttribute = (Hotfix)customAttribute; hasHotfixMark = true; needFactory = hotfixAttribute.needFactory; needHotfixChildren = hotfixAttribute.needChildren; } MethodInfo[] methodInfos = clsType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (MethodInfo methodInfo in methodInfos) { //自己声明的 if (!methodInfo.IsPrivate && !methodInfo.IsStatic && !methodInfo.IsFinal && (methodInfo.IsVirtual || methodInfo.IsAbstract)) { switch (methodInfo.Name) { case "Equals": case "GetHashCode": case "ToString": case "Finalize": { } break; default: { ILMethodInfo iMethod = new ILMethodInfo(); iMethod.name = methodInfo.Name; iMethod.visitType = methodInfo.IsPublic ? VisitType.Public : VisitType.Protected; iMethod.isAbstract = methodInfo.IsAbstract; iMethod.needBaseCall = !methodInfo.IsAbstract; //虚函数不需要 iMethod.returnType = getTypeName(methodInfo.ReturnType); foreach (ParameterInfo parameterInfo in methodInfo.GetParameters()) { MethodArgInfo iArg = new MethodArgInfo(); iArg.name = parameterInfo.Name; iArg.type = getTypeName(parameterInfo.ParameterType); iArg.isIn = parameterInfo.IsIn; iArg.isOut = parameterInfo.IsOut; // iArg.isRef=parameterInfo.ParameterType.Name.EndsWith("&"); iArg.isRef = parameterInfo.IsRetval; // if(parameterInfo.IsRetval) // { // Ctrl.print("A1",methodInfo.Name,parameterInfo.Name); // } // iArg.autoLength=parameterInfo.IsOptional; iArg.defaultValue = parameterInfo.DefaultValue != null?parameterInfo.DefaultValue.ToString() : ""; iMethod.args.add(iArg); } //TODO:还是采用clone的方式,来对needBaseCall赋值 if (methodInfo.DeclaringType == clsType) { doAddMethod(iMethod); } else { if (methodInfo.IsAbstract) { iCls.methodKeys.get(iMethod.getKey()).needBaseCall = false; } } break; } } } } } }
/** 执行一个方法 */ private static void doOneMethod(ILMethodInfo method, int index, StringBuilder sb1, StringBuilder sb2, IntSet marks) { int aSize; if ((aSize = method.args.size()) > 0) { if (!marks.contains(aSize)) { marks.add(aSize); sb1.Append("private object[] _p" + aSize + "=new object[" + aSize + "];"); endLine(sb1); endLine(sb1); } } sb2.Append("IMethod _m" + index + ";"); endLine(sb2); sb2.Append("bool _g" + index + ";"); endLine(sb2); if (method.needBaseCall) { sb2.Append("bool _b" + index + ";"); endLine(sb2); } switch (method.visitType) { case VisitType.Private: { //private不写 } break; case VisitType.Public: { sb2.Append("public "); sb2.Append("override "); } break; case VisitType.Protected: { sb2.Append("protected "); sb2.Append("override "); } break; } bool needReturn = !string.IsNullOrEmpty(method.returnType); if (!needReturn) { sb2.Append("void"); } else { sb2.Append(method.returnType); } sb2.Append(" " + method.name + "("); MethodArgInfo arg; for (int i = 0; i < method.args.size(); i++) { arg = method.args[i]; if (i > 0) { sb2.Append(","); } if (arg.isIn) { sb2.Append("in "); } else if (arg.isOut) { sb2.Append("out "); } else if (arg.isRef) { sb2.Append("ref "); } sb2.Append(arg.type); sb2.Append(" "); sb2.Append(arg.name); } sb2.Append(")"); endLine(sb2); sb2.Append("{"); endLine(sb2, 1); sb2.Append("if(!_g" + index + ")"); endLine(sb2, 1); sb2.Append("{"); endLine(sb2, 2); sb2.Append("_m" + index + "=instance.Type.GetMethod(\"" + method.name + "\"," + aSize + ");"); endLine(sb2, 2); sb2.Append("_g" + index + "=true;"); endLine(sb2, 1); sb2.Append("}"); endLine(sb2, 1); endLine(sb2, 1); sb2.Append("if(_m" + index + "!=null"); if (method.needBaseCall) { sb2.Append(" && !_b" + index); } sb2.Append(")"); endLine(sb2, 1); sb2.Append("{"); endLine(sb2, 2); if (method.needBaseCall) { sb2.Append("_b" + index + "=true;"); endLine(sb2, 2); } if (aSize > 0) { for (int i = 0; i < aSize; i++) { sb2.Append("_p" + aSize + "[" + i + "]=" + method.args.get(i).name + ";"); endLine(sb2, 2); } } if (needReturn) { sb2.Append(method.returnType + " re=(" + method.returnType + ")"); } sb2.Append("appdomain.Invoke(_m" + index + ",instance,"); if (aSize > 0) { sb2.Append("_p" + aSize); } else { sb2.Append("null"); } sb2.Append(");"); endLine(sb2, 2); if (aSize > 0) { for (int i = 0; i < aSize; i++) { sb2.Append("_p" + aSize + "[" + i + "]=null;"); endLine(sb2, 2); } } if (method.needBaseCall) { sb2.Append("_b" + index + "=false;"); endLine(sb2, 2); } if (needReturn) { sb2.Append("return re;"); endLine(sb2, 2); } endLine(sb2, 1); sb2.Append("}"); if (method.needBaseCall || needReturn) { endLine(sb2, 1); sb2.Append("else"); endLine(sb2, 1); sb2.Append("{"); endLine(sb2, 2); if (needReturn) { sb2.Append("return "); } sb2.Append("base." + method.name + "("); for (int i = 0; i < aSize; i++) { arg = method.args.get(i); if (i > 0) { sb2.Append(","); } if (arg.isIn) { sb2.Append("in "); } else if (arg.isOut) { sb2.Append("out "); } else if (arg.isRef) { sb2.Append("ref "); } sb2.Append(arg.name); } sb2.Append(");"); endLine(sb2, 1); sb2.Append("}"); } endLine(sb2); sb2.Append("}"); endLine(sb2); endLine(sb2); }