From 7ebafcd2e31b1bc5afdbe397c219988ce6b1e738 Mon Sep 17 00:00:00 2001 From: liiir1985 Date: Fri, 1 May 2020 17:05:15 +0800 Subject: [PATCH] Finished Cross binding adapter code generator --- ILRuntime/ILRuntime.csproj | 1 + .../Enviorment/CrossBindingCodeGenerator.cs | 516 ++++++++++++++++++ .../Enviorment/CrossBindingMethodInfo.cs | 10 +- ILRuntimeTest/Adapters/TestClass2Adaptor.cs | 162 +++--- ILRuntimeTest/Adapters/helper.cs | 4 +- ILRuntimeTest/TestMainForm.Designer.cs | 33 +- ILRuntimeTest/TestMainForm.cs | 8 + ILRuntimeTest/TestMainForm.resx | 6 +- TestCases/InheritanceTest.cs | 13 +- 9 files changed, 657 insertions(+), 96 deletions(-) create mode 100644 ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs diff --git a/ILRuntime/ILRuntime.csproj b/ILRuntime/ILRuntime.csproj index 0f9ffcff..9c3d5797 100644 --- a/ILRuntime/ILRuntime.csproj +++ b/ILRuntime/ILRuntime.csproj @@ -96,6 +96,7 @@ + diff --git a/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs b/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs new file mode 100644 index 00000000..15f6bad7 --- /dev/null +++ b/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs @@ -0,0 +1,516 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ILRuntime.CLR.Method; +using ILRuntime.CLR.TypeSystem; +using ILRuntime.Runtime.Intepreter; +using System.Reflection; +using System.Data.SqlTypes; + +namespace ILRuntime.Runtime.Enviorment +{ + public class CrossBindingCodeGenerator + { + public static string GenerateCrossBindingAdapterCode(Type baseType, string nameSpace) + { + StringBuilder sb = new StringBuilder(); + MethodInfo[] methods = baseType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); + List virtMethods = new List(); + foreach(var i in methods) + { + if (i.IsVirtual || i.IsAbstract || baseType.IsInterface) + virtMethods.Add(i); + } + string clsName, realClsName; + bool isByRef; + baseType.GetClassName(out clsName, out realClsName, out isByRef, true); + sb.Append(@"using System; +using ILRuntime.CLR.Method; +using ILRuntime.Runtime.Enviorment; +using ILRuntime.Runtime.Intepreter; + +namespace "); + sb.AppendLine(nameSpace); + sb.Append(@"{ + public class "); + sb.Append(clsName); + sb.AppendLine(@"Adapter : CrossBindingAdaptor + {"); + GenerateCrossBindingMethodInfo(sb, virtMethods); + + sb.Append(@" public override Type BaseCLRType + { + get + { + return typeof("); + sb.Append(realClsName); + sb.AppendLine(@"); + } + } + + public override Type AdaptorType + { + get + { + return typeof(Adapter); + } + } + + public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) + { + return new Adapter(appdomain, instance); + } +"); + + sb.AppendLine(string.Format(" public class Adapter : {0}, CrossBindingAdaptorType", realClsName)); + sb.AppendLine(@" { + ILTypeInstance instance; + ILRuntime.Runtime.Enviorment.AppDomain appdomain; + + public Adapter() + { + + } + + public Adapter(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) + { + this.appdomain = appdomain; + this.instance = instance; + } + + public ILTypeInstance ILInstance { get { return instance; } } +"); + GenerateCrossBindingMethodBody(sb, virtMethods); + sb.Append(@" public override string ToString() + { + IMethod m = appdomain.ObjectType.GetMethod("); sb.AppendLine("\"ToString\", 0);"); + sb.AppendLine(@" m = instance.Type.GetVirtualMethod(m); + if (m == null || m is ILMethod) + { + return instance.ToString(); + } + else + return instance.Type.FullName; + } + } + } +}"); + return sb.ToString(); + } + + static void GenerateCrossBindingMethodBody(StringBuilder sb, List virtMethods) + { + int index = 0; + foreach (var i in virtMethods) + { + if (ShouldSkip(i)) + continue; + var param = i.GetParameters(); + string modifier = i.IsFamily ? "protected" : "public"; + string overrideStr = i.DeclaringType.IsInterface ? "" : "override "; + string clsName, realClsName; + string returnString = ""; + bool isByRef; + if (i.ReturnType != typeof(void)) + { + i.ReturnType.GetClassName(out clsName, out realClsName, out isByRef, true); + returnString = "return "; + } + else + realClsName = "void"; + + sb.Append(string.Format(" {0} {3}{1} {2}(", modifier, realClsName, i.Name, overrideStr)); + GetParameterDefinition(sb, param, true); + sb.AppendLine(@") + {"); + if (!i.IsAbstract) + { + sb.AppendLine(string.Format(" if (m{0}_{1}.CheckShouldInvokeBase(this.instance))", i.Name, index)); + sb.AppendLine(string.Format(" {2}base.{0}({1});", i.Name, GetParameterName(param, true), returnString)); + sb.AppendLine(" else"); + sb.AppendLine(string.Format(" {3}m{0}_{1}.Invoke(this.instance{2});", i.Name, index, GetParameterName(param, false), returnString)); + } + else + { + sb.AppendLine(string.Format(" {3}m{0}_{1}.Invoke(this.instance{2});", i.Name, index, GetParameterName(param, false), returnString)); + } + sb.AppendLine(" }"); + sb.AppendLine(); + index++; + } + } + + static void GenerateCrossBindingMethodInfo(StringBuilder sb, List virtMethods) + { + int index = 0; + foreach (var i in virtMethods) + { + if (ShouldSkip(i)) + continue; + var param = i.GetParameters(); + if (NeedGenerateCrossBindingMethodClass(param)) + { + GenerateCrossBindingMethodClass(sb, i.Name, index, param, i.ReturnType); + sb.AppendLine(string.Format(" static {0}_{1}Info m{0}_{1} = new {0}_{1}Info();", i.Name, index)); + } + else + { + if (i.ReturnType != typeof(void)) + { + sb.AppendLine(string.Format(" static CrossBindingFunctionInfo<{0}> m{1}_{2} = new CrossBindingFunctionInfo<{0}>(\"{1}\");", GetParametersString(param, i.ReturnType), i.Name, index)); + } + else + { + if (param.Length > 0) + sb.AppendLine(string.Format(" static CrossBindingMethodInfo<{0}> m{1}_{2} = new CrossBindingMethodInfo<{0}>(\"{1}\");", GetParametersString(param, i.ReturnType), i.Name, index)); + else + sb.AppendLine(string.Format(" static CrossBindingMethodInfo m{0}_{1} = new CrossBindingMethodInfo(\"{0}\");", i.Name, index)); + } + } + index++; + } + } + + static bool ShouldSkip(MethodInfo info) + { + if (info.Name == "ToString" || info.Name == "GetHashCode" || info.Name == "Finalize") + return info.GetParameters().Length == 0; + if (info.Name == "Equals" && info.GetParameters().Length == 1 && info.GetParameters()[0].ParameterType == typeof(object)) + return true; + return false; + } + static string GetParametersString(ParameterInfo[] param, Type returnType) + { + StringBuilder sb = new StringBuilder(); + bool first = true; + foreach(var i in param) + { + if (!first) + sb.Append(", "); + else + first = false; + string clsName, realClsName; + bool isByRef; + i.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true); + sb.Append(realClsName); + } + if(returnType != typeof(void)) + { + if (!first) + sb.Append(", "); + string clsName, realClsName; + bool isByRef; + returnType.GetClassName(out clsName, out realClsName, out isByRef, true); + sb.Append(realClsName); + } + return sb.ToString(); + } + + static string GetParametersTypeString(ParameterInfo[] param, Type returnType) + { + StringBuilder sb = new StringBuilder(); + bool first = true; + foreach (var i in param) + { + if (!first) + sb.Append(", "); + else + first = false; + string clsName, realClsName; + bool isByRef; + sb.Append("typeof("); + i.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true); + sb.Append(realClsName); + sb.Append(")"); + if (isByRef) + sb.Append(".MakeByRefType()"); + } + if (returnType != typeof(void)) + { + if (!first) + sb.Append(", "); + string clsName, realClsName; + bool isByRef; + returnType.GetClassName(out clsName, out realClsName, out isByRef, true); + sb.Append(realClsName); + } + return sb.ToString(); + } + + static bool NeedGenerateCrossBindingMethodClass(ParameterInfo[] param) + { + if (param.Length > 5) + return true; + foreach(var i in param) + { + if (i.IsOut || i.ParameterType.IsByRef) + return true; + } + return false; + } + + static string GetParameterName(ParameterInfo[] param, bool first) + { + StringBuilder sb = new StringBuilder(); + + foreach (var p in param) + { + if (!first) + sb.Append(", "); + else + first = false; + if (p.IsOut) + sb.Append("out "); + else if (p.ParameterType.IsByRef) + sb.Append("ref "); + sb.Append(p.Name); + } + return sb.ToString(); + } + + static void GetParameterDefinition(StringBuilder sb, ParameterInfo[] param, bool first) + { + string clsName, realClsName; + bool isByRef; + + foreach (var p in param) + { + if (!first) + sb.Append(", "); + else + first = false; + p.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true); + if (p.IsOut) + sb.Append("out "); + else if (isByRef) + sb.Append("ref "); + sb.Append(realClsName); + sb.Append(" "); + sb.Append(p.Name); + } + } + + static void GenerateCrossBindingMethodClass(StringBuilder sb, string funcName, int index, ParameterInfo[] param, Type returnType) + { + sb.AppendLine(string.Format(" class {0}_{1}Info : CrossBindingMethodInfo", funcName, index)); + sb.Append(@" { + static Type[] pTypes = new Type[] {"); + sb.Append(GetParametersTypeString(param, returnType)); + sb.AppendLine("};"); + sb.AppendLine(); + sb.AppendLine(string.Format(" public {0}_{1}Info()", funcName, index)); + sb.AppendLine(string.Format(" : base(\"{0}\")", funcName)); + sb.Append(@" { + + } + + protected override Type ReturnType { get { return "); + + string clsName, realClsName; + bool isByRef; + returnType.GetClassName(out clsName, out realClsName, out isByRef, true); + string rtRealName = realClsName; + bool hasReturn = returnType != typeof(void); + + if (!hasReturn) + sb.Append("null"); + else + { + sb.AppendFormat("typeof({0})", realClsName); + } + sb.AppendLine(@"; } } + + protected override Type[] Parameters { get { return pTypes; } }"); + sb.AppendFormat(" public {0} Invoke(ILTypeInstance instance", !hasReturn ? "void" : realClsName); + GetParameterDefinition(sb, param, false); + sb.AppendLine(@") + { + EnsureMethod(instance); + if (method != null) + { + invoking = true;"); + + if (hasReturn) + sb.AppendLine(string.Format(" {0} __res = default({0});", rtRealName)); + sb.AppendLine(@" try + { + using (var ctx = domain.BeginInvoke(method)) + {"); + Dictionary refIndex = new Dictionary(); + int idx = 0; + foreach (var p in param) + { + if (p.ParameterType.IsByRef) + { + sb.AppendLine(GetPushString(p.ParameterType, p.Name)); + refIndex[p] = idx++; + } + } + sb.AppendLine(" ctx.PushObject(instance);"); + foreach (var p in param) + { + if (p.ParameterType.IsByRef) + { + sb.AppendLine(string.Format(" ctx.PushReference({0});", refIndex[p])); + } + else + { + sb.AppendLine(GetPushString(p.ParameterType, p.Name)); + } + } + sb.AppendLine(" ctx.Invoke();"); + if (hasReturn) + sb.AppendLine(GetReadString(returnType, rtRealName, "", "__res")); + foreach (var p in param) + { + if (p.ParameterType.IsByRef) + { + p.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true); + + sb.AppendLine(GetReadString(p.ParameterType, realClsName, refIndex[p].ToString(), p.Name)); + } + } + sb.AppendLine(" }"); + sb.AppendLine(@" } + finally + { + invoking = false; + }"); + if (hasReturn) + sb.AppendLine(" return __res;"); + sb.AppendLine(" }"); + if (hasReturn) + sb.AppendLine(@" else + return default(TResult);"); + sb.AppendLine(@" } + + public override void Invoke(ILTypeInstance instance) + { + throw new NotSupportedException(); + } + }"); + } + static string GetPushString(Type type, string argName) + { + if (type.IsPrimitive) + { + if (type == typeof(int)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(long)) + { + return string.Format(" ctx.PushLong({0});", argName); + } + else if (type == typeof(short)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(bool)) + { + return string.Format(" ctx.PushBool({0});", argName); + } + else if (type == typeof(ushort)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(float)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(double)) + { + return string.Format(" ctx.PushDouble({0});", argName); + } + else if (type == typeof(byte)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(sbyte)) + { + return string.Format(" ctx.PushInteger({0});", argName); + } + else if (type == typeof(uint)) + { + return string.Format(" ctx.PushInteger((int){0});", argName); + } + else if (type == typeof(char)) + { + return string.Format(" ctx.PushInteger((int){0});", argName); + } + else if (type == typeof(ulong)) + { + return string.Format(" ctx.PushLong((long){0});", argName); + } + else + throw new NotImplementedException(); + } + else + { + return string.Format(" ctx.PushObject({0});", argName); + } + } + + static string GetReadString(Type type, string realClsName, string argName, string valName) + { + if (type.IsPrimitive) + { + if (type == typeof(int)) + { + return string.Format(" {1} = ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(long)) + { + return string.Format(" {1} = ctx.ReadLong({0});", argName, valName); + } + else if (type == typeof(short)) + { + return string.Format(" {1} = (short)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(bool)) + { + return string.Format(" {1} = ctx.ReadBool({0});", argName, valName); + } + else if (type == typeof(ushort)) + { + return string.Format(" {1} = (ushort)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(float)) + { + return string.Format(" {1} = ctx.ReadFloat({0});", argName, valName); + } + else if (type == typeof(double)) + { + return string.Format(" {1} = ctx.ReadDouble({0});", argName, valName); + } + else if (type == typeof(byte)) + { + return string.Format(" {1} = (byte)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(sbyte)) + { + return string.Format(" {1} = (sbyte)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(uint)) + { + return string.Format(" {1} = (uint)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(char)) + { + return string.Format(" {1} = (char)ctx.ReadInteger({0});", argName, valName); + } + else if (type == typeof(ulong)) + { + return string.Format(" {1} = (ulong)ctx.ReadLong({0});", argName, valName); + } + else + throw new NotImplementedException(); + } + else + { + return string.Format(" {2} = ctx.ReadObject<{1}>({0});", argName, realClsName, valName); + } + } + } +} diff --git a/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs b/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs index 29914829..445350e8 100644 --- a/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs +++ b/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs @@ -711,9 +711,15 @@ protected void EnsureMethod(ILTypeInstance ins) if (Parameters != null) { param = new List(); - foreach(var i in Parameters) + foreach (var i in Parameters) { - param.Add(domain.GetType(i)); + if (i.IsByRef) + { + var type = domain.GetType(i.GetElementType()); + param.Add(type.MakeByRefType()); + } + else + param.Add(domain.GetType(i)); } } if (ReturnType != null) diff --git a/ILRuntimeTest/Adapters/TestClass2Adaptor.cs b/ILRuntimeTest/Adapters/TestClass2Adaptor.cs index e48c1b19..a85d8874 100644 --- a/ILRuntimeTest/Adapters/TestClass2Adaptor.cs +++ b/ILRuntimeTest/Adapters/TestClass2Adaptor.cs @@ -3,19 +3,62 @@ using ILRuntime.Runtime.Enviorment; using ILRuntime.Runtime.Intepreter; -namespace ILRuntimeTest.TestFramework -{ - public class TestClass2Adaptor : CrossBindingAdaptor +namespace ILRuntimeTest +{ + public class TestClass2Adapter : CrossBindingAdaptor { - static CrossBindingMethodInfo mVMethod1 = new CrossBindingMethodInfo("VMethod1"); - static CrossBindingFunctionInfo mVMethod2 = new CrossBindingFunctionInfo("VMethod2"); - static CrossBindingMethodInfo mAMethod1 = new CrossBindingMethodInfo("AbMethod1"); - static CrossBindingFunctionInfo mAMethod2 = new CrossBindingFunctionInfo("AbMethod2"); + static CrossBindingMethodInfo mVMethod1_0 = new CrossBindingMethodInfo("VMethod1"); + static CrossBindingFunctionInfo mVMethod2_1 = new CrossBindingFunctionInfo("VMethod2"); + class VMethod3_2Info : CrossBindingMethodInfo + { + static Type[] pTypes = new Type[] { typeof(System.Int32).MakeByRefType() }; + + public VMethod3_2Info() + : base("VMethod3") + { + + } + + protected override Type ReturnType { get { return null; } } + + protected override Type[] Parameters { get { return pTypes; } } + public void Invoke(ILTypeInstance instance, ref System.Int32 arg) + { + EnsureMethod(instance); + if (method != null) + { + invoking = true; + try + { + using (var ctx = domain.BeginInvoke(method)) + { + ctx.PushObject(arg); + ctx.PushObject(instance); + ctx.PushReference(0); + ctx.Invoke(); + arg = ctx.ReadObject(0); + } + } + finally + { + invoking = false; + } + } + } + + public override void Invoke(ILTypeInstance instance) + { + throw new NotSupportedException(); + } + } + static VMethod3_2Info mVMethod3_2 = new VMethod3_2Info(); + static CrossBindingMethodInfo mAbMethod1_3 = new CrossBindingMethodInfo("AbMethod1"); + static CrossBindingFunctionInfo mAbMethod2_4 = new CrossBindingFunctionInfo("AbMethod2"); public override Type BaseCLRType { get { - return typeof(TestClass2); + return typeof(ILRuntimeTest.TestFramework.TestClass2); } } @@ -23,26 +66,26 @@ public override Type AdaptorType { get { - return typeof(Adaptor); + return typeof(Adapter); } } public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { - return new Adaptor(appdomain, instance); + return new Adapter(appdomain, instance); } - internal class Adaptor : TestClass2, CrossBindingAdaptorType + public class Adapter : ILRuntimeTest.TestFramework.TestClass2, CrossBindingAdaptorType { ILTypeInstance instance; ILRuntime.Runtime.Enviorment.AppDomain appdomain; - public Adaptor() + public Adapter() { } - public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) + public Adapter(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { this.appdomain = appdomain; this.instance = instance; @@ -50,57 +93,40 @@ public Adaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance public ILTypeInstance ILInstance { get { return instance; } } - public override void VMethod1() { - if (mVMethod1.CheckShouldInvokeBase(instance)) + if (mVMethod1_0.CheckShouldInvokeBase(this.instance)) base.VMethod1(); else - mVMethod1.Invoke(instance); + mVMethod1_0.Invoke(this.instance); } - public override Boolean VMethod2() + public override System.Boolean VMethod2() { - if (mVMethod2.CheckShouldInvokeBase(instance)) + if (mVMethod2_1.CheckShouldInvokeBase(this.instance)) return base.VMethod2(); else - return mVMethod2.Invoke(instance); + return mVMethod2_1.Invoke(this.instance); } - IMethod mVMethod3; - bool invoking; - public override void VMethod3(ref int arg) + + public override void VMethod3(ref System.Int32 arg) { - if(mVMethod3 == null) - { - mVMethod3 = instance.Type.GetMethod("VMethod3"); - } - if (mVMethod3 != null && !invoking) - { - invoking = true; - using(var ctx = appdomain.BeginInvoke(mVMethod3)) - { - ctx.PushInteger(arg);//byref - ctx.PushObject(instance); - ctx.PushReference(0); - ctx.Invoke(); - arg = ctx.ReadInteger(0); - } - invoking = false; - } - else + if (mVMethod3_2.CheckShouldInvokeBase(this.instance)) base.VMethod3(ref arg); + else + mVMethod3_2.Invoke(this.instance, ref arg); } protected override void AbMethod1() { - mAMethod1.Invoke(instance); + mAbMethod1_3.Invoke(this.instance); } - public override Single AbMethod2(Int32 arg1) + public override System.Single AbMethod2(System.Int32 arg1) { - return mAMethod2.Invoke(instance, arg1); + return mAbMethod2_4.Invoke(this.instance, arg1); } - + public override string ToString() { IMethod m = appdomain.ObjectType.GetMethod("ToString", 0); @@ -115,13 +141,14 @@ public override string ToString() } } - public class IDisposableClassInheritanceAdaptor : CrossBindingAdaptor + public class IDisposableAdapter : CrossBindingAdaptor { + static CrossBindingMethodInfo mDispose_0 = new CrossBindingMethodInfo("Dispose"); public override Type BaseCLRType { get { - return typeof(IDisposable); + return typeof(System.IDisposable); } } @@ -129,64 +156,49 @@ public override Type AdaptorType { get { - return typeof(IDisposableAdaptor); + return typeof(Adapter); } } public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { - return new IDisposableAdaptor(appdomain, instance); + return new Adapter(appdomain, instance); } - public class IDisposableAdaptor : IDisposable, CrossBindingAdaptorType + public class Adapter : System.IDisposable, CrossBindingAdaptorType { - private ILTypeInstance instance; - private ILRuntime.Runtime.Enviorment.AppDomain appDomain; - - private IMethod iDisposable; - private readonly object[] param0 = new object[0]; + ILTypeInstance instance; + ILRuntime.Runtime.Enviorment.AppDomain appdomain; - public IDisposableAdaptor() + public Adapter() { + } - public IDisposableAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appDomain, ILTypeInstance instance) + public Adapter(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) { - this.appDomain = appDomain; + this.appdomain = appdomain; this.instance = instance; } - public ILTypeInstance ILInstance - { - get - { - return instance; - } - } + public ILTypeInstance ILInstance { get { return instance; } } public void Dispose() { - if (this.iDisposable == null) - { - this.iDisposable = instance.Type.GetMethod("Dispose"); - } - this.appDomain.Invoke(this.iDisposable, instance, this.param0); + mDispose_0.Invoke(this.instance); } public override string ToString() { - IMethod m = this.appDomain.ObjectType.GetMethod("ToString", 0); + IMethod m = appdomain.ObjectType.GetMethod("ToString", 0); m = instance.Type.GetVirtualMethod(m); if (m == null || m is ILMethod) { return instance.ToString(); } - - return instance.Type.FullName; + else + return instance.Type.FullName; } - - } } - } \ No newline at end of file diff --git a/ILRuntimeTest/Adapters/helper.cs b/ILRuntimeTest/Adapters/helper.cs index 928f7692..ba36d9b8 100644 --- a/ILRuntimeTest/Adapters/helper.cs +++ b/ILRuntimeTest/Adapters/helper.cs @@ -21,10 +21,10 @@ public static void Init(ILRuntime.Runtime.Enviorment.AppDomain app) app.RegisterCrossBindingAdaptor(new ClassInheritanceTestAdaptor()); app.RegisterCrossBindingAdaptor(new InterfaceTestAdaptor()); - app.RegisterCrossBindingAdaptor(new TestClass2Adaptor()); + app.RegisterCrossBindingAdaptor(new TestClass2Adapter()); app.RegisterCrossBindingAdaptor(new TestClass3Adaptor()); app.RegisterCrossBindingAdaptor(new TestClass4Adaptor()); - app.RegisterCrossBindingAdaptor(new IDisposableClassInheritanceAdaptor()); + app.RegisterCrossBindingAdaptor(new IDisposableAdapter()); app.RegisterCrossBindingAdaptor(new ClassInheritanceTest2Adaptor()); app.RegisterCrossBindingAdaptor(new IAsyncStateMachineClassInheritanceAdaptor()); diff --git a/ILRuntimeTest/TestMainForm.Designer.cs b/ILRuntimeTest/TestMainForm.Designer.cs index e5f05615..5af01a9e 100644 --- a/ILRuntimeTest/TestMainForm.Designer.cs +++ b/ILRuntimeTest/TestMainForm.Designer.cs @@ -37,6 +37,7 @@ private void InitializeComponent() this.btnRunSelect = new System.Windows.Forms.Button(); this.btnGenerateBinding = new System.Windows.Forms.Button(); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); + this.btnTestCrossBind = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -50,7 +51,7 @@ private void InitializeComponent() // btnRun // this.btnRun.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnRun.Location = new System.Drawing.Point(514, 380); + this.btnRun.Location = new System.Drawing.Point(574, 380); this.btnRun.Margin = new System.Windows.Forms.Padding(2); this.btnRun.Name = "btnRun"; this.btnRun.Size = new System.Drawing.Size(62, 29); @@ -62,9 +63,10 @@ private void InitializeComponent() // listView1 // this.listView1.Dock = System.Windows.Forms.DockStyle.Fill; + this.listView1.HideSelection = false; this.listView1.Location = new System.Drawing.Point(0, 0); this.listView1.Name = "listView1"; - this.listView1.Size = new System.Drawing.Size(664, 157); + this.listView1.Size = new System.Drawing.Size(809, 157); this.listView1.TabIndex = 1; this.listView1.UseCompatibleStateImageBehavior = false; // @@ -77,13 +79,13 @@ private void InitializeComponent() this.tbLog.Multiline = true; this.tbLog.Name = "tbLog"; this.tbLog.ScrollBars = System.Windows.Forms.ScrollBars.Both; - this.tbLog.Size = new System.Drawing.Size(664, 192); + this.tbLog.Size = new System.Drawing.Size(809, 192); this.tbLog.TabIndex = 2; // // btnLoad // this.btnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnLoad.Location = new System.Drawing.Point(332, 380); + this.btnLoad.Location = new System.Drawing.Point(392, 380); this.btnLoad.Name = "btnLoad"; this.btnLoad.Size = new System.Drawing.Size(100, 29); this.btnLoad.TabIndex = 3; @@ -97,13 +99,13 @@ private void InitializeComponent() | System.Windows.Forms.AnchorStyles.Right))); this.txtPath.Location = new System.Drawing.Point(12, 385); this.txtPath.Name = "txtPath"; - this.txtPath.Size = new System.Drawing.Size(314, 21); + this.txtPath.Size = new System.Drawing.Size(374, 21); this.txtPath.TabIndex = 4; // // btnRunSelect // this.btnRunSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnRunSelect.Location = new System.Drawing.Point(438, 380); + this.btnRunSelect.Location = new System.Drawing.Point(498, 379); this.btnRunSelect.Name = "btnRunSelect"; this.btnRunSelect.Size = new System.Drawing.Size(71, 29); this.btnRunSelect.TabIndex = 5; @@ -114,7 +116,7 @@ private void InitializeComponent() // btnGenerateBinding // this.btnGenerateBinding.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.btnGenerateBinding.Location = new System.Drawing.Point(581, 380); + this.btnGenerateBinding.Location = new System.Drawing.Point(641, 380); this.btnGenerateBinding.Name = "btnGenerateBinding"; this.btnGenerateBinding.Size = new System.Drawing.Size(84, 29); this.btnGenerateBinding.TabIndex = 6; @@ -138,15 +140,27 @@ private void InitializeComponent() // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.tbLog); - this.splitContainer1.Size = new System.Drawing.Size(664, 355); + this.splitContainer1.Size = new System.Drawing.Size(809, 355); this.splitContainer1.SplitterDistance = 157; this.splitContainer1.TabIndex = 7; // + // btnTestCrossBind + // + this.btnTestCrossBind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnTestCrossBind.Location = new System.Drawing.Point(731, 380); + this.btnTestCrossBind.Name = "btnTestCrossBind"; + this.btnTestCrossBind.Size = new System.Drawing.Size(98, 29); + this.btnTestCrossBind.TabIndex = 8; + this.btnTestCrossBind.Text = "CrossBind Gen"; + this.btnTestCrossBind.UseVisualStyleBackColor = true; + this.btnTestCrossBind.Click += new System.EventHandler(this.button1_Click); + // // TestMainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(688, 420); + this.ClientSize = new System.Drawing.Size(833, 420); + this.Controls.Add(this.btnTestCrossBind); this.Controls.Add(this.btnGenerateBinding); this.Controls.Add(this.btnRunSelect); this.Controls.Add(this.txtPath); @@ -178,5 +192,6 @@ private void InitializeComponent() private System.Windows.Forms.Button btnRunSelect; private System.Windows.Forms.Button btnGenerateBinding; private System.Windows.Forms.SplitContainer splitContainer1; + private System.Windows.Forms.Button btnTestCrossBind; } } \ No newline at end of file diff --git a/ILRuntimeTest/TestMainForm.cs b/ILRuntimeTest/TestMainForm.cs index a973b7c6..d3ed6268 100644 --- a/ILRuntimeTest/TestMainForm.cs +++ b/ILRuntimeTest/TestMainForm.cs @@ -245,6 +245,14 @@ private void UpdateBtnState() btnRunSelect.Enabled = _selectItemArgs != null; } + private void button1_Click(object sender, EventArgs e) + { + var msg = ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(TestClass2), "ILRuntimeTest"); + MessageBox.Show(msg); + msg = ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(IDisposable), "ILRuntimeTest"); + MessageBox.Show(msg); + } + private void btnGenerateBinding_Click(object sender, EventArgs e) { /*List types = new List(); diff --git a/ILRuntimeTest/TestMainForm.resx b/ILRuntimeTest/TestMainForm.resx index 20883cb4..121fdbeb 100644 --- a/ILRuntimeTest/TestMainForm.resx +++ b/ILRuntimeTest/TestMainForm.resx @@ -112,12 +112,12 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17 \ No newline at end of file diff --git a/TestCases/InheritanceTest.cs b/TestCases/InheritanceTest.cs index 9ff46546..b7e4e453 100644 --- a/TestCases/InheritanceTest.cs +++ b/TestCases/InheritanceTest.cs @@ -141,13 +141,16 @@ public static void InheritanceTest07() public static void InheritanceTest08() { - AABase obj = new AABase(); - obj.R = 1; - obj.R2 = 2; + using (AABase obj = new AABase()) + { + obj.R = 1; + obj.R2 = 2; + + var m = typeof(InheritanceTest).GetMethod("InheritanceTest08_Sub"); - var m = typeof(InheritanceTest).GetMethod("InheritanceTest08_Sub"); + m.Invoke(null, new object[] { obj }); - m.Invoke(null, new object[] { obj }); + } } static void InheritanceTest08_Sub(AABase obj)