Просмотр исходного кода

更新了ilruntime版本,删除了不需要的插件

tanghai 8 лет назад
Родитель
Сommit
b0cbf36a4e
100 измененных файлов с 3342 добавлено и 1127 удалено
  1. 3 3
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/CLRMethod.cs
  2. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/CLRMethod.cs.meta
  3. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ExceptionHandler.cs.meta
  4. 10 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ILMethod.cs
  5. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ILMethod.cs.meta
  6. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Method/IMethod.cs.meta
  7. 147 7
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/CLRType.cs
  8. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/CLRType.cs.meta
  9. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILGenericParameterType.cs.meta
  10. 36 11
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILType.cs
  11. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILType.cs.meta
  12. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/IType.cs.meta
  13. 83 6
      Unity/Assets/ILRuntime/ILRuntime/CLR/Utils/Extensions.cs
  14. 1 1
      Unity/Assets/ILRuntime/ILRuntime/CLR/Utils/Extensions.cs.meta
  15. 18 0
      Unity/Assets/ILRuntime/ILRuntime/Other/ByReferenceKeyComparer.cs
  16. 2 2
      Unity/Assets/ILRuntime/ILRuntime/Other/ByReferenceKeyComparer.cs.meta
  17. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Other/DelegateExportAttribute.cs.meta
  18. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Other/NeedAdaptorAttribute.cs.meta
  19. 132 0
      Unity/Assets/ILRuntime/ILRuntime/Other/ThreadSafeDictionary.cs
  20. 2 2
      Unity/Assets/ILRuntime/ILRuntime/Other/ThreadSafeDictionary.cs.meta
  21. 999 0
      Unity/Assets/ILRuntime/ILRuntime/Other/UncheckedList.cs
  22. 2 2
      Unity/Assets/ILRuntime/ILRuntime/Other/UncheckedList.cs.meta
  23. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/Extensions.cs.meta
  24. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeConstructorInfo.cs.meta
  25. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeFieldInfo.cs.meta
  26. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeMethodInfo.cs.meta
  27. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeParameterInfo.cs.meta
  28. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimePropertyInfo.cs.meta
  29. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeType.cs.meta
  30. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeWrapperType.cs.meta
  31. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Adaptors/CLRCrossBindingAdaptors.cs.meta
  32. 305 848
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs
  33. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs.meta
  34. 335 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingGeneratorExtensions.cs
  35. 2 2
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingGeneratorExtensions.cs.meta
  36. 183 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/CommonBindingGenerator.cs
  37. 12 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/CommonBindingGenerator.cs.meta
  38. 180 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ConstructorBindingGenerator.cs
  39. 12 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ConstructorBindingGenerator.cs.meta
  40. 114 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/FieldBindingGenerator.cs
  41. 12 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/FieldBindingGenerator.cs.meta
  42. 320 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/MethodBindingGenerator.cs
  43. 12 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/MethodBindingGenerator.cs.meta
  44. 46 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ValueTypeBindingGenerator.cs
  45. 12 0
      Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ValueTypeBindingGenerator.cs.meta
  46. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakPointContext.cs
  47. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakPointContext.cs.meta
  48. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakpointInfo.cs.meta
  49. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugMessageType.cs.meta
  50. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugService.cs
  51. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugService.cs.meta
  52. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugSocket.cs.meta
  53. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebuggerServer/DebuggerServer.cs.meta
  54. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSBindBreakpoint.cs.meta
  55. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSDeleteBreakpoint.cs.meta
  56. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSExecute.cs.meta
  57. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSResolveVariable.cs.meta
  58. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSStep.cs.meta
  59. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCAttachResult.cs.meta
  60. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCBindBreakpointResult.cs.meta
  61. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCBreakpointHit.cs.meta
  62. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCModuleLoaded.cs.meta
  63. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCResolveVariableResult.cs.meta
  64. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCStepComplete.cs.meta
  65. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCThreadStarted.cs.meta
  66. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/StackFrameInfo.cs.meta
  67. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/StepTypes.cs.meta
  68. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/VariableInfo.cs.meta
  69. 63 20
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/AppDomain.cs
  70. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/AppDomain.cs.meta
  71. 13 13
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CLRRedirections.cs
  72. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CLRRedirections.cs.meta
  73. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CrossBindingAdaptor.cs
  74. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CrossBindingAdaptor.cs.meta
  75. 6 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/DelegateManager.cs
  76. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/DelegateManager.cs.meta
  77. 0 28
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/ILContext.cs
  78. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Extensions.cs.meta
  79. 18 12
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs
  80. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs.meta
  81. 159 72
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs
  82. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs.meta
  83. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILRuntimeException.cs.meta
  84. 11 16
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs
  85. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs.meta
  86. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/OpCodes/OpCode.cs.meta
  87. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/OpCodes/OpCodeEnum.cs.meta
  88. 20 7
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/RuntimeStack.cs
  89. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/RuntimeStack.cs.meta
  90. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackFrame.cs.meta
  91. 6 7
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackObject.cs
  92. 1 1
      Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackObject.cs.meta
  93. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Code.cs.meta
  94. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/CodeReader.cs.meta
  95. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/CodeWriter.cs.meta
  96. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Document.cs.meta
  97. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/ExceptionHandler.cs.meta
  98. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/ILProcessor.cs.meta
  99. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Instruction.cs.meta
  100. 1 1
      Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/MethodBody.cs.meta

+ 3 - 3
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/CLRMethod.cs

@@ -207,7 +207,7 @@ namespace ILRuntime.CLR.Method
             return (StackObject*)((long)a - sizeof(StackObject) * b);
         }
 
-        public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, List<object> mStack, bool isNewObj = false)
+        public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, IList<object> mStack, bool isNewObj = false)
         {
             if (parameters == null)
             {
@@ -276,7 +276,7 @@ namespace ILRuntime.CLR.Method
             }
         }
 
-        unsafe void FixReference(int paramCount, StackObject* esp, object[] param, List<object> mStack)
+        unsafe void FixReference(int paramCount, StackObject* esp, object[] param, IList<object> mStack)
         {
             for (int i = paramCount; i >= 1; i--)
             {
@@ -323,7 +323,7 @@ namespace ILRuntime.CLR.Method
                             }
                             else
                             {
-                                ((CLRType)t).GetField(p->ValueLow).SetValue(null, val);
+                                ((CLRType)t).SetStaticFieldValue(p->ValueLow, val);
                             }
                         }
                         break;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/CLRMethod.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: e3a93984f12c9da43b98d43930dd8c49
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ExceptionHandler.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 99ef6f3b1f4f7894ebd5e1878c08fcb5
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 10 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ILMethod.cs

@@ -389,6 +389,15 @@ namespace ILRuntime.CLR.Method
                             else
                                 code.TokenInteger = token.GetHashCode();
                         }
+                        else
+                        {
+                            //Cannot find method or the method is dummy
+                            MethodReference _ref = (MethodReference)token;
+                            int paramCnt = _ref.HasParameters ? _ref.Parameters.Count : 0;
+                            if (_ref.HasThis)
+                                paramCnt++;
+                            code.TokenLong = paramCnt;
+                        }
                     }
                     break;
                 case OpCodeEnum.Constrained:
@@ -408,7 +417,7 @@ namespace ILRuntime.CLR.Method
                 case OpCodeEnum.Ldfld:
                 case OpCodeEnum.Ldflda:
                     {
-                        code.TokenInteger = appdomain.GetFieldIndex(token, declaringType, this);
+                        code.TokenLong = appdomain.GetStaticFieldIndex(token, declaringType, this);
                     }
                     break;
 

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/ILMethod.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 4d61dbf18ea786940ad2dda2466cde64
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Method/IMethod.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 3ee73b411c3d8c34cbcc5578441c854a
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 147 - 7
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/CLRType.cs

@@ -7,6 +7,8 @@ using System.Reflection;
 using Mono.Cecil;
 using ILRuntime.CLR.Method;
 using ILRuntime.Reflection;
+using ILRuntime.Runtime.Enviorment;
+
 namespace ILRuntime.CLR.TypeSystem
 {
     public class CLRType : IType
@@ -19,13 +21,17 @@ namespace ILRuntime.CLR.TypeSystem
         List<CLRType> genericInstances;
         Dictionary<string, int> fieldMapping;
         Dictionary<int, FieldInfo> fieldInfoCache;
+        Dictionary<int, CLRFieldGetterDelegate> fieldGetterCache;
+        Dictionary<int, CLRFieldSetterDelegate> fieldSetterCache;
+        CLRMemberwiseCloneDelegate memberwiseCloneDelegate;
+        CLRCreateDefaultInstanceDelegate createDefaultInstanceDelegate;
+        CLRCreateArrayInstanceDelegate createArrayInstanceDelegate;
         Dictionary<int, int> fieldTokenMapping;
         IType byRefType, arrayType, elementType;
         IType[] interfaces;
         bool isDelegate;
         IType baseType;
         bool isBaseTypeInitialized = false, interfaceInitialized = false;
-        MethodInfo memberwiseClone;
         ILRuntimeWrapperType wraperType;
 
         int hashCode = -1;
@@ -167,16 +173,26 @@ namespace ILRuntime.CLR.TypeSystem
             }
         }
 
-        public new MethodInfo MemberwiseClone
+        public object PerformMemberwiseClone(object target)
         {
-            get
+            if (memberwiseCloneDelegate == null)
             {
-                if (clrType.IsValueType && memberwiseClone == null)
+                if (!AppDomain.MemberwiseCloneMap.TryGetValue(this.clrType, out memberwiseCloneDelegate))
                 {
-                    memberwiseClone = clrType.GetMethod("MemberwiseClone", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
+                    var memberwiseClone = clrType.GetMethod("MemberwiseClone", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
+
+                    if (memberwiseClone != null)
+                    {
+                        memberwiseCloneDelegate = (ref object t) => memberwiseClone.Invoke(t, null);
+                    }
+                    else
+                    {
+                        throw new InvalidOperationException("Memberwise clone method not found for " + clrType.FullName);
+                    }
                 }
-                return memberwiseClone;
             }
+
+            return memberwiseCloneDelegate(ref target);
         }
 
         void InitializeBaseType()
@@ -203,6 +219,89 @@ namespace ILRuntime.CLR.TypeSystem
             }
         }
 
+        public object GetFieldValue(int hash, object target)
+        {
+            if (fieldMapping == null)
+                InitializeFields();
+
+            var getter = GetFieldGetter(hash);
+            if (getter != null)
+            {
+                return getter(ref target);
+            }
+
+            var fieldinfo = GetField(hash);
+            if (fieldinfo != null)
+            {
+                return fieldinfo.GetValue(target);
+            }
+
+            return null;
+        }
+
+        public void SetStaticFieldValue(int hash, object value)
+        {
+            if (fieldMapping == null)
+                InitializeFields();
+
+            var setter = GetFieldSetter(hash);
+            object target = null;
+            if (setter != null)
+            {
+                setter(ref target, value);
+                return;
+            }
+
+            var fieldInfo = GetField(hash);
+            if (fieldInfo != null)
+            {
+                fieldInfo.SetValue(null, value);
+            }
+        }
+
+        public unsafe void SetFieldValue(int hash, ref object target, object value)
+        {
+            if (fieldMapping == null)
+                InitializeFields();
+
+            var setter = GetFieldSetter(hash);
+            if (setter != null)
+            {
+                setter(ref target, value);
+                return;
+            }
+
+            var fieldInfo = GetField(hash);
+            if (fieldInfo != null)
+            {
+                fieldInfo.SetValue(target, value);
+            }
+        }
+
+        private CLRFieldGetterDelegate GetFieldGetter(int hash)
+        {
+            var dic = fieldGetterCache;
+            CLRFieldGetterDelegate res;
+            if (dic != null && dic.TryGetValue(hash, out res))
+                return res;
+            else if (BaseType != null)
+                return ((CLRType)BaseType).GetFieldGetter(hash);
+            else
+                return null;
+        }
+
+        private CLRFieldSetterDelegate GetFieldSetter(int hash)
+        {
+            var dic = fieldSetterCache;
+            CLRFieldSetterDelegate res;
+            if (dic != null && dic.TryGetValue(hash, out res))
+                return res;
+            else if (BaseType != null)
+                return ((CLRType)BaseType).GetFieldSetter(hash);
+            else
+                return null;
+        }
+
         public FieldInfo GetField(int hash)
         {
             var dic = Fields;
@@ -269,12 +368,27 @@ namespace ILRuntime.CLR.TypeSystem
             var fields = clrType.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
             foreach (var i in fields)
             {
+                int hashCode = i.GetHashCode();
+
                 if (i.IsPublic || i.IsFamily)
                 {
-                    int hashCode = i.GetHashCode();
                     fieldMapping[i.Name] = hashCode;
                     fieldInfoCache[hashCode] = i;
                 }
+
+                CLRFieldGetterDelegate getter;
+                if (AppDomain.FieldGetterMap.TryGetValue(i, out getter))
+                {
+                    if (fieldGetterCache == null) fieldGetterCache = new Dictionary<int, CLRFieldGetterDelegate>();
+                    fieldGetterCache[hashCode] = getter;
+                }
+
+                CLRFieldSetterDelegate setter;
+                if (AppDomain.FieldSetterMap.TryGetValue(i, out setter))
+                {
+                    if (fieldSetterCache == null) fieldSetterCache = new Dictionary<int, CLRFieldSetterDelegate>();
+                    fieldSetterCache[hashCode] = setter;
+                }
             }
         }
         public int GetFieldIndex(object token)
@@ -489,6 +603,32 @@ namespace ILRuntime.CLR.TypeSystem
             return res;
         }
 
+        public object CreateDefaultInstance()
+        {
+            if (createDefaultInstanceDelegate == null)
+            {
+                if (!AppDomain.CreateDefaultInstanceMap.TryGetValue(clrType, out createDefaultInstanceDelegate))
+                {
+                    createDefaultInstanceDelegate = () => Activator.CreateInstance(TypeForCLR);
+                }
+            }
+
+            return createDefaultInstanceDelegate();
+        }
+
+        public object CreateArrayInstance(int size)
+        {
+            if (createArrayInstanceDelegate == null)
+            {
+                if (!AppDomain.CreateArrayInstanceMap.TryGetValue(clrType, out createArrayInstanceDelegate))
+                {
+                    createArrayInstanceDelegate = s => Array.CreateInstance(TypeForCLR, s);
+                }
+            }
+
+            return createArrayInstanceDelegate(size);
+        }
+
         public IType MakeByRefType()
         {
             if (byRefType == null)

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/CLRType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 77e1f51e465e9e24ba3a42bb88b7ad34
-timeCreated: 1496471235
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILGenericParameterType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ea169e3908d84da4088fc6eae41d3c41
-timeCreated: 1496471235
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 36 - 11
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILType.cs

@@ -255,11 +255,16 @@ namespace ILRuntime.CLR.TypeSystem
             get; private set;
         }
 
+        private bool? isValueType;
+
         public bool IsValueType
         {
             get
             {
-                return definition.IsValueType;
+                if (isValueType == null)
+                    isValueType = definition.IsValueType;
+
+                return isValueType.Value;
             }
         }
 
@@ -444,7 +449,9 @@ namespace ILRuntime.CLR.TypeSystem
                         }
                     }
                     if (baseType == null)
+                    {
                         throw new TypeLoadException("Cannot find Adaptor for:" + definition.BaseType.FullName);
+                    }
                 }
                 else
                 {
@@ -549,14 +556,18 @@ namespace ILRuntime.CLR.TypeSystem
         public IMethod GetVirtualMethod(IMethod method)
         {
             IType[] genericArguments = null;
-            if (method is ILMethod)
-            {
-                genericArguments = ((ILMethod)method).GenericArugmentsArray;
-            }
-            else
+            if (method.IsGenericInstance)
             {
-                genericArguments = ((CLRMethod)method).GenericArguments;
+                if (method is ILMethod)
+                {
+                    genericArguments = ((ILMethod)method).GenericArugmentsArray;
+                }
+                else
+                {
+                    genericArguments = ((CLRMethod)method).GenericArguments;
+                }
             }
+
             var m = GetMethod(method.Name, method.Parameters, genericArguments, method.ReturnType);
             if (m == null)
             {
@@ -581,8 +592,9 @@ namespace ILRuntime.CLR.TypeSystem
             IMethod genericMethod = null;
             if (methods.TryGetValue(name, out lst))
             {
-                foreach (var i in lst)
+                for (var idx = 0; idx < lst.Count; idx++)
                 {
+                    var i = lst[idx];
                     int pCnt = param != null ? param.Count : 0;
                     if (i.ParameterCount == pCnt)
                     {
@@ -684,6 +696,13 @@ namespace ILRuntime.CLR.TypeSystem
             return constructors;
         }
 
+        public IMethod GetStaticConstroctor()
+        {
+            if (constructors == null)
+                InitializeMethods();
+            return staticConstructor;
+        }
+
         public IMethod GetConstructor(int paramCnt)
         {
             if (constructors == null)
@@ -857,9 +876,15 @@ namespace ILRuntime.CLR.TypeSystem
             {
                 return true;
             }
-            else if (BaseType != null)
+
+            if (BaseType != null)
+            {
                 res = BaseType.CanAssignTo(type);
-            if (!res && Implements != null)
+
+                if (res) return true;
+            }
+
+            if (Implements != null)
             {
                 for (int i = 0; i < interfaces.Length; i++)
                 {
@@ -869,7 +894,7 @@ namespace ILRuntime.CLR.TypeSystem
                         return true;
                 }
             }
-            return false;
+            return res;
         }
 
         public ILTypeInstance Instantiate(bool callDefaultConstructor = true)

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/ILType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: a657d8c53554fdb498c23061939379d5
-timeCreated: 1496471235
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/TypeSystem/IType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 2d59f9c8b6bc1b542a95502fa20ddea8
-timeCreated: 1496471235
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 83 - 6
Unity/Assets/ILRuntime/ILRuntime/CLR/Utils/Extensions.cs

@@ -5,6 +5,7 @@ using System.Text;
 
 using ILRuntime.CLR.TypeSystem;
 using ILRuntime.CLR.Method;
+using ILRuntime.Other;
 using Mono.Cecil;
 using ILRuntime.Runtime.Intepreter;
 namespace ILRuntime.CLR.Utils
@@ -106,11 +107,84 @@ namespace ILRuntime.CLR.Utils
             return sb.ToString();
         }
 
+        [Flags]
+        public enum TypeFlags
+        {
+            Default = 0,
+            IsPrimitive = 0x1,
+            IsByRef = 0x2,
+            IsEnum = 0x4,
+            IsDelegate = 0x8,
+            IsValueType = 0x10,
+        }
+
+        private static readonly Dictionary<Type, TypeFlags> typeFlags = new Dictionary<Type, TypeFlags>(new ByReferenceKeyComparer<Type>());
+
+        public static bool FastIsEnum(this Type pt)
+        {
+            return (pt.GetTypeFlags() & TypeFlags.IsEnum) != 0;
+        }
+
+        public static bool FastIsByRef(this Type pt)
+        {
+            return (pt.GetTypeFlags() & TypeFlags.IsByRef) != 0;
+        }
+
+        public static bool FastIsPrimitive(this Type pt)
+        {
+            return (pt.GetTypeFlags() & TypeFlags.IsPrimitive) != 0;
+        }
+
+        public static bool FastIsValueType(this Type pt)
+        {
+            return (pt.GetTypeFlags() & TypeFlags.IsValueType) != 0;
+        }
+
+        public static TypeFlags GetTypeFlags(this Type pt)
+        {
+            var result = TypeFlags.Default;
+
+            if (!typeFlags.TryGetValue(pt, out result))
+            {
+                if (pt.IsPrimitive)
+                {
+                    result |= TypeFlags.IsPrimitive;
+                }
+
+                if (pt == typeof(Delegate) || pt.IsSubclassOf(typeof(Delegate)))
+                {
+                    result |= TypeFlags.IsDelegate;
+                }
+
+                if (pt.IsByRef)
+                {
+                    result |= TypeFlags.IsByRef;
+                }
+
+                if (pt.IsEnum)
+                {
+                    result |= TypeFlags.IsEnum;
+                }
+
+                if (pt.IsValueType)
+                {
+                    result |= TypeFlags.IsValueType;
+                }
+
+                typeFlags[pt] = result;
+            }
+
+            return result;
+        }
+
         public static object CheckCLRTypes(this Type pt, object obj)
         {
             if (obj == null)
                 return null;
-            if (pt.IsPrimitive && pt != typeof(int))
+
+            var typeFlags = GetTypeFlags(pt);
+
+            if ((typeFlags & TypeFlags.IsPrimitive) != 0 && pt != typeof(int))
             {
                 if (pt == typeof(bool) && !(obj is bool))
                 {
@@ -137,7 +211,7 @@ namespace ILRuntime.CLR.Utils
             {
                 obj = ((ILRuntime.Reflection.ILRuntimeWrapperType)obj).RealType;
             }
-            else if (pt == typeof(Delegate) || pt.IsSubclassOf(typeof(Delegate)))
+            else if ((typeFlags & TypeFlags.IsDelegate) != 0)
             {
                 if (obj is Delegate)
                     return obj;
@@ -145,20 +219,23 @@ namespace ILRuntime.CLR.Utils
                     return ((IDelegateAdapter)obj).Delegate;
                 return ((IDelegateAdapter)obj).GetConvertor(pt);
             }
-            else if (pt.IsByRef)
+            else if ((typeFlags & TypeFlags.IsByRef) != 0)
             {
                 return CheckCLRTypes(pt.GetElementType(), obj);
             }
-            else if (pt.IsEnum)
+            else if ((typeFlags & TypeFlags.IsEnum) != 0)
             {
                 return Enum.ToObject(pt, obj);
             }
             else if (obj is ILTypeInstance)
             {
-                if (obj is IDelegateAdapter && pt != typeof(ILTypeInstance))
+                var adapter = obj as IDelegateAdapter;
+
+                if (adapter != null && pt != typeof(ILTypeInstance))
                 {
-                    return ((IDelegateAdapter)obj).Delegate;
+                    return adapter.Delegate;
                 }
+
                 if (!(obj is ILEnumTypeInstance))
                 {
                     var ins = (ILTypeInstance)obj;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/CLR/Utils/Extensions.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ed1e47ba193179f4b9f21caedbc1b605
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 18 - 0
Unity/Assets/ILRuntime/ILRuntime/Other/ByReferenceKeyComparer.cs

@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace ILRuntime.Other
+{
+    class ByReferenceKeyComparer<T> : IEqualityComparer<T>
+    {
+        public bool Equals(T x, T y)
+        {
+            return object.ReferenceEquals(x, y);
+        }
+
+        public int GetHashCode(T obj)
+        {
+            return RuntimeHelpers.GetHashCode(obj);
+        }
+    }
+}

+ 2 - 2
Unity/Assets/PSD2UGUI/Editor/Effects/MirrorEditor.cs.meta → Unity/Assets/ILRuntime/ILRuntime/Other/ByReferenceKeyComparer.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 7f38fce6b2a9a7a4cba217bee3a8ec37
-timeCreated: 1486174738
+guid: 85b57f58903421241b65cd2ffbcac7bf
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Other/DelegateExportAttribute.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: eb007a85cca9ce54ea6e3b32ab7ddceb
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Other/NeedAdaptorAttribute.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: fd1e000bd78872247b5ee948842785e9
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 132 - 0
Unity/Assets/ILRuntime/ILRuntime/Other/ThreadSafeDictionary.cs

@@ -0,0 +1,132 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Runtime.Versioning;
+using System.Threading;
+
+namespace ILRuntime.Other
+{
+    /// <summary>
+    /// A thread safe dictionary for internal use
+    /// </summary>
+    /// <typeparam name="K"></typeparam>
+    /// <typeparam name="V"></typeparam>
+    class ThreadSafeDictionary<K, V> : IDictionary<K, V>
+    {
+        Dictionary<K, V> dic = new Dictionary<K, V>();
+
+        public Dictionary<K,V> InnerDictionary { get { return dic; } }
+        public V this[K key]
+        {
+            get
+            {
+                lock(dic)
+                    return dic[key];
+            }
+
+            set
+            {
+               lock(dic)
+                    dic[key] = value;
+            }
+        }
+
+        public int Count
+        {
+            get
+            {
+                lock(dic)
+                    return dic.Count;
+            }
+        }
+
+        public bool IsReadOnly
+        {
+            get
+            {
+                lock(dic)
+                    return IsReadOnly;
+            }
+        }
+
+        public ICollection<K> Keys
+        {
+            get
+            {
+                throw new NotImplementedException();
+            }
+        }
+
+        public ICollection<V> Values
+        {
+            get
+            {
+                throw new NotImplementedException();
+            }
+        }
+
+        public void Add(KeyValuePair<K, V> item)
+        {
+            lock (dic)
+                 dic.Add(item.Key, item.Value);
+        }
+
+        public void Add(K key, V value)
+        {
+            lock(dic)
+                dic.Add(key, value);
+        }
+
+        public void Clear()
+        {
+            lock(dic)
+                dic.Clear();
+        }
+
+        public bool Contains(KeyValuePair<K, V> item)
+        {
+           lock(dic)
+                return dic.ContainsKey(item.Key);
+        }
+
+        public bool ContainsKey(K key)
+        {
+            lock(dic)
+                return dic.ContainsKey(key);
+        }
+
+        public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
+        {
+            throw new NotImplementedException();
+        }
+
+        public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool Remove(KeyValuePair<K, V> item)
+        {
+            throw new NotImplementedException();
+        }
+
+        public bool Remove(K key)
+        {
+            lock(dic)
+                return dic.Remove(key);
+        }
+
+        public bool TryGetValue(K key, out V value)
+        {
+             lock(dic)
+                return dic.TryGetValue(key, out value);
+        }
+
+        IEnumerator IEnumerable.GetEnumerator()
+        {
+            throw new NotImplementedException();
+        }
+    }
+}

+ 2 - 2
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/ILContext.cs.meta → Unity/Assets/ILRuntime/ILRuntime/Other/ThreadSafeDictionary.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 4d1d82ea4e082bc4da5829542d3f98c9
-timeCreated: 1496395687
+guid: e45123ef096e7a446a6ccd805feaf05e
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 999 - 0
Unity/Assets/ILRuntime/ILRuntime/Other/UncheckedList.cs

@@ -0,0 +1,999 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Diagnostics;
+using System.Runtime.Versioning;
+using System.Threading;
+
+namespace ILRuntime.Other
+{
+    /// <summary>
+    /// This is a copy of the latest .NET framework 4.5 List implementation, with all extraneous checking removed.
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    [Serializable]
+    public class UncheckedList<T> : IList<T>, ICollection<T>, IEnumerable<T>, IEnumerable, IList, ICollection
+    {
+
+       private const int _defaultCapacity = 4;
+
+        private T[] _items;
+        private int _size;
+        private int _version;
+        [NonSerialized]
+        private Object _syncRoot;
+
+        private static readonly T[] _emptyArray = new T[0];
+
+        // Constructs a UncheckedList. The list is initially empty and has a capacity
+        // of zero. Upon adding the first element to the list the capacity is
+        // increased to _defaultCapacity, and then increased in multiples of two
+        // as required.
+        public UncheckedList()
+        {
+            _items = _emptyArray;
+        }
+
+        // Constructs a UncheckedList with a given initial capacity. The list is
+        // initially empty, but will have room for the given number of elements
+        // before any reallocations are required.
+        // 
+        public UncheckedList(int capacity)
+        {
+            if (capacity == 0)
+                _items = _emptyArray;
+            else
+                _items = new T[capacity];
+        }
+
+        // Constructs a UncheckedList, copying the contents of the given collection. The
+        // size and capacity of the new list will both be equal to the size of the
+        // given collection.
+        // 
+        public UncheckedList(IEnumerable<T> collection)
+        {
+            ICollection<T> c = collection as ICollection<T>;
+            if (c != null)
+            {
+                int count = c.Count;
+                if (count == 0)
+                {
+                    _items = _emptyArray;
+                }
+                else
+                {
+                    _items = new T[count];
+                    c.CopyTo(_items, 0);
+                    _size = count;
+                }
+            }
+            else
+            {
+                _size = 0;
+                _items = _emptyArray;
+                AddEnumerable(collection);
+            }
+        }
+
+        // Gets and sets the capacity of this list.  The capacity is the size of
+        // the internal array used to hold items.  When set, the internal 
+        // array of the list is reallocated to the given capacity.
+        // 
+        public int Capacity
+        {
+            get
+            {
+                return _items.Length;
+            }
+            set
+            {
+                if (value != _items.Length)
+                {
+                    if (value > 0)
+                    {
+                        T[] newItems = new T[value];
+                        if (_size > 0)
+                        {
+                            Array.Copy(_items, 0, newItems, 0, _size);
+                        }
+                        _items = newItems;
+                    }
+                    else
+                    {
+                        _items = _emptyArray;
+                    }
+                }
+            }
+        }
+
+        // Read-only property describing how many elements are in the UncheckedList.
+        public int Count
+        {
+            get
+            {
+                return _size;
+            }
+        }
+
+        bool System.Collections.IList.IsFixedSize
+        {
+            get { return false; }
+        }
+
+
+        // Is this UncheckedList read-only?
+        bool ICollection<T>.IsReadOnly
+        {
+            get { return false; }
+        }
+
+        bool System.Collections.IList.IsReadOnly
+        {
+            get { return false; }
+        }
+
+        // Is this UncheckedList synchronized (thread-safe)?
+        bool System.Collections.ICollection.IsSynchronized
+        {
+            get { return false; }
+        }
+
+        // Synchronization root for this object.
+        Object System.Collections.ICollection.SyncRoot
+        {
+            get
+            {
+                if (_syncRoot == null)
+                {
+                    System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
+                }
+                return _syncRoot;
+            }
+        }
+        // Sets or Gets the element at the given index.
+        // 
+        public T this[int index]
+        {
+            get
+            {
+                return _items[index];
+            }
+
+            set
+            {
+                _items[index] = value;
+                _version++;
+            }
+        }
+
+        private static bool IsCompatibleObject(object value)
+        {
+            // Non-null values are fine.  Only accept nulls if T is a class or Nullable<U>.
+            // Note that default(T) is not equal to null for value types except when T is Nullable<U>. 
+            return ((value is T) || (value == null && default(T) == null));
+        }
+
+        Object System.Collections.IList.this[int index]
+        {
+            get
+            {
+                return this[index];
+            }
+            set
+            {
+                try
+                {
+                    this[index] = (T)value;
+                }
+                catch (InvalidCastException)
+                {
+                }
+            }
+        }
+
+        // Adds the given object to the end of this list. The size of the list is
+        // increased by one. If required, the capacity of the list is doubled
+        // before adding the new element.
+        public void Add(T item)
+        {
+            var array = _items;
+            var size = _size;
+            _version++;
+            if ((uint)size < (uint)array.Length)
+            {
+                _size = size + 1;
+                array[size] = item;
+            }
+            else
+            {
+                AddWithResize(item);
+            }
+        }
+
+        // Non-inline from UncheckedList.Add to improve its code quality as uncommon path
+        private void AddWithResize(T item)
+        {
+            var size = _size;
+            EnsureCapacity(size + 1);
+            _size = size + 1;
+            _items[size] = item;
+        }
+
+        int System.Collections.IList.Add(Object item)
+        {
+            try
+            {
+                Add((T)item);
+            }
+            catch (InvalidCastException)
+            {
+            }
+
+            return Count - 1;
+        }
+
+
+        // Adds the elements of the given collection to the end of this list. If
+        // required, the capacity of the list is increased to twice the previous
+        // capacity or the new size, whichever is larger.
+        //
+        public void AddRange(IEnumerable<T> collection)
+        {
+            InsertRange(_size, collection);
+        }
+
+        public ReadOnlyCollection<T> AsReadOnly()
+        {
+            return new ReadOnlyCollection<T>(this);
+        }
+
+        // Searches a section of the list for a given element using a binary search
+        // algorithm. Elements of the list are compared to the search value using
+        // the given IComparer interface. If comparer is null, elements of
+        // the list are compared to the search value using the IComparable
+        // interface, which in that case must be implemented by all elements of the
+        // list and the given search value. This method assumes that the given
+        // section of the list is already sorted; if this is not the case, the
+        // result will be incorrect.
+        //
+        // The method returns the index of the given value in the list. If the
+        // list does not contain the given value, the method returns a negative
+        // integer. The bitwise complement operator (~) can be applied to a
+        // negative result to produce the index of the first element (if any) that
+        // is larger than the given search value. This is also the index at which
+        // the search value should be inserted into the list in order for the list
+        // to remain sorted.
+        // 
+        // The method uses the Array.BinarySearch method to perform the
+        // search.
+        // 
+        public int BinarySearch(int index, int count, T item, IComparer<T> comparer)
+        {
+            if (index < 0) return -1;
+            return Array.BinarySearch<T>(_items, index, count, item, comparer);
+        }
+
+        public int BinarySearch(T item)
+        {
+            return BinarySearch(0, Count, item, null);
+        }
+
+        public int BinarySearch(T item, IComparer<T> comparer)
+        {
+            return BinarySearch(0, Count, item, comparer);
+        }
+
+
+        // Clears the contents of UncheckedList.
+        public void Clear()
+        {
+            if (!typeof(T).IsValueType)
+            {
+                int size = _size;
+                _size = 0;
+                _version++;
+                if (size > 0)
+                {
+                    Array.Clear(_items, 0, size); // Clear the elements so that the gc can reclaim the references.
+                }
+            }
+            else
+            {
+                _size = 0;
+                _version++;
+            }
+        }
+
+        // Contains returns true if the specified element is in the UncheckedList.
+        // It does a linear, O(n) search.  Equality is determined by calling
+        // EqualityComparer<T>.Default.Equals().
+
+        public bool Contains(T item)
+        {
+            // PERF: IndexOf calls Array.IndexOf, which internally
+            // calls EqualityComparer<T>.Default.IndexOf, which
+            // is specialized for different types. This
+            // boosts performance since instead of making a
+            // virtual method call each iteration of the loop,
+            // via EqualityComparer<T>.Default.Equals, we
+            // only make one virtual call to EqualityComparer.IndexOf.
+
+            return _size != 0 && IndexOf(item) != -1;
+        }
+
+        bool System.Collections.IList.Contains(Object item)
+        {
+            if (IsCompatibleObject(item))
+            {
+                return Contains((T)item);
+            }
+            return false;
+        }
+
+        public UncheckedList<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
+        {
+            UncheckedList<TOutput> list = new UncheckedList<TOutput>(_size);
+            for (int i = 0; i < _size; i++)
+            {
+                list._items[i] = converter(_items[i]);
+            }
+            list._size = _size;
+            return list;
+        }
+
+        // Copies this UncheckedList into array, which must be of a 
+        // compatible array type.  
+        //
+        public void CopyTo(T[] array)
+        {
+            CopyTo(array, 0);
+        }
+
+        // Copies this UncheckedList into array, which must be of a 
+        // compatible array type.  
+        //
+        void System.Collections.ICollection.CopyTo(Array array, int arrayIndex)
+        {
+            try
+            {
+                // Array.Copy will check for NULL.
+                Array.Copy(_items, 0, array, arrayIndex, _size);
+            }
+            catch (ArrayTypeMismatchException)
+            {
+            }
+        }
+
+        // Copies a section of this list to the given array at the given index.
+        // 
+        // The method uses the Array.Copy method to copy the elements.
+        // 
+        public void CopyTo(int index, T[] array, int arrayIndex, int count)
+        {
+            // Delegate rest of error checking to Array.Copy.
+            Array.Copy(_items, index, array, arrayIndex, count);
+        }
+
+        public void CopyTo(T[] array, int arrayIndex)
+        {
+            // Delegate rest of error checking to Array.Copy.
+            Array.Copy(_items, 0, array, arrayIndex, _size);
+        }
+
+        // Ensures that the capacity of this list is at least the given minimum
+        // value. If the current capacity of the list is less than min, the
+        // capacity is increased to twice the current capacity or to min,
+        // whichever is larger.
+        private void EnsureCapacity(int min)
+        {
+            if (_items.Length < min)
+            {
+                int newCapacity = _items.Length == 0 ? _defaultCapacity : _items.Length * 2;
+                // Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
+                // Note that this check works even when _items.Length overflowed thanks to the (uint) cast
+                if ((uint) newCapacity > Int32.MaxValue) newCapacity = Int32.MaxValue;
+                if (newCapacity < min) newCapacity = min;
+                Capacity = newCapacity;
+            }
+        }
+
+        public bool Exists(Predicate<T> match)
+        {
+            return FindIndex(match) != -1;
+        }
+
+        public T Find(Predicate<T> match)
+        {
+            for (int i = 0; i < _size; i++)
+            {
+                if (match(_items[i]))
+                {
+                    return _items[i];
+                }
+            }
+            return default(T);
+        }
+
+        public UncheckedList<T> FindAll(Predicate<T> match)
+        {
+            UncheckedList<T> list = new UncheckedList<T>();
+            for (int i = 0; i < _size; i++)
+            {
+                if (match(_items[i]))
+                {
+                    list.Add(_items[i]);
+                }
+            }
+            return list;
+        }
+
+        public int FindIndex(Predicate<T> match)
+        {
+            return FindIndex(0, _size, match);
+        }
+
+        public int FindIndex(int startIndex, Predicate<T> match)
+        {
+            return FindIndex(startIndex, _size - startIndex, match);
+        }
+
+        public int FindIndex(int startIndex, int count, Predicate<T> match)
+        {
+            int endIndex = startIndex + count;
+            for (int i = startIndex; i < endIndex; i++)
+            {
+                if (match(_items[i])) return i;
+            }
+            return -1;
+        }
+
+        public T FindLast(Predicate<T> match)
+        {
+            for (int i = _size - 1; i >= 0; i--)
+            {
+                if (match(_items[i]))
+                {
+                    return _items[i];
+                }
+            }
+            return default(T);
+        }
+
+        public int FindLastIndex(Predicate<T> match)
+        {
+            return FindLastIndex(_size - 1, _size, match);
+        }
+
+        public int FindLastIndex(int startIndex, Predicate<T> match)
+        {
+            return FindLastIndex(startIndex, startIndex + 1, match);
+        }
+
+        public int FindLastIndex(int startIndex, int count, Predicate<T> match)
+        {
+            int endIndex = startIndex - count;
+            for (int i = startIndex; i > endIndex; i--)
+            {
+                if (match(_items[i]))
+                {
+                    return i;
+                }
+            }
+            return -1;
+        }
+
+        public void ForEach(Action<T> action)
+        {
+            int version = _version;
+
+            for (int i = 0; i < _size; i++)
+            {
+                if (version != _version)
+                {
+                    break;
+                }
+                action(_items[i]);
+            }
+        }
+
+        // Returns an enumerator for this list with the given
+        // permission for removal of elements. If modifications made to the list 
+        // while an enumeration is in progress, the MoveNext and 
+        // GetObject methods of the enumerator will throw an exception.
+        //
+        public Enumerator GetEnumerator()
+        {
+            return new Enumerator(this);
+        }
+
+        IEnumerator<T> IEnumerable<T>.GetEnumerator()
+        {
+            return new Enumerator(this);
+        }
+
+        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+        {
+            return new Enumerator(this);
+        }
+
+        public UncheckedList<T> GetRange(int index, int count)
+        {
+            UncheckedList<T> list = new UncheckedList<T>(count);
+            Array.Copy(_items, index, list._items, 0, count);
+            list._size = count;
+            return list;
+        }
+
+
+        // Returns the index of the first occurrence of a given value in a range of
+        // this list. The list is searched forwards from beginning to end.
+        // The elements of the list are compared to the given value using the
+        // Object.Equals method.
+        // 
+        // This method uses the Array.IndexOf method to perform the
+        // search.
+        // 
+        public int IndexOf(T item)
+        {
+            return Array.IndexOf(_items, item, 0, _size);
+        }
+
+        int System.Collections.IList.IndexOf(Object item)
+        {
+            if (IsCompatibleObject(item))
+            {
+                return IndexOf((T)item);
+            }
+            return -1;
+        }
+
+        // Returns the index of the first occurrence of a given value in a range of
+        // this list. The list is searched forwards, starting at index
+        // index and ending at count number of elements. The
+        // elements of the list are compared to the given value using the
+        // Object.Equals method.
+        // 
+        // This method uses the Array.IndexOf method to perform the
+        // search.
+        // 
+        public int IndexOf(T item, int index)
+        {
+            return Array.IndexOf(_items, item, index, _size - index);
+        }
+
+        // Returns the index of the first occurrence of a given value in a range of
+        // this list. The list is searched forwards, starting at index
+        // index and upto count number of elements. The
+        // elements of the list are compared to the given value using the
+        // Object.Equals method.
+        // 
+        // This method uses the Array.IndexOf method to perform the
+        // search.
+        // 
+        public int IndexOf(T item, int index, int count)
+        {
+            return Array.IndexOf(_items, item, index, count);
+        }
+
+        // Inserts an element into this list at a given index. The size of the list
+        // is increased by one. If required, the capacity of the list is doubled
+        // before inserting the new element.
+        // 
+        public void Insert(int index, T item)
+        {
+            if (_size == _items.Length) EnsureCapacity(_size + 1);
+            if (index < _size)
+            {
+                Array.Copy(_items, index, _items, index + 1, _size - index);
+            }
+            _items[index] = item;
+            _size++;
+            _version++;
+        }
+
+        void System.Collections.IList.Insert(int index, Object item)
+        {
+            try
+            {
+                Insert(index, (T)item);
+            }
+            catch (InvalidCastException)
+            {
+            }
+        }
+
+        // Inserts the elements of the given collection at a given index. If
+        // required, the capacity of the list is increased to twice the previous
+        // capacity or the new size, whichever is larger.  Ranges may be added
+        // to the end of the list by setting index to the UncheckedList's size.
+        //
+        public void InsertRange(int index, IEnumerable<T> collection)
+        {
+            ICollection<T> c = collection as ICollection<T>;
+            if (c != null)
+            {    // if collection is ICollection<T>
+                int count = c.Count;
+                if (count > 0)
+                {
+                    EnsureCapacity(_size + count);
+                    if (index < _size)
+                    {
+                        Array.Copy(_items, index, _items, index + count, _size - index);
+                    }
+
+                    // If we're inserting a UncheckedList into itself, we want to be able to deal with that.
+                    if (this == c)
+                    {
+                        // Copy first part of _items to insert location
+                        Array.Copy(_items, 0, _items, index, index);
+                        // Copy last part of _items back to inserted location
+                        Array.Copy(_items, index + count, _items, index * 2, _size - index);
+                    }
+                    else
+                    {
+                        c.CopyTo(_items, index);
+                    }
+                    _size += count;
+                }
+            }
+            else if (index < _size)
+            {
+                // We're inserting a lazy enumerable. Call Insert on each of the constituent items.
+                using (IEnumerator<T> en = collection.GetEnumerator())
+                {
+                    while (en.MoveNext())
+                    {
+                        Insert(index++, en.Current);
+                    }
+                }
+            }
+            else
+            {
+                // We're adding a lazy enumerable because the index is at the end of this list.
+                AddEnumerable(collection);
+            }
+            _version++;
+        }
+
+        // Returns the index of the last occurrence of a given value in a range of
+        // this list. The list is searched backwards, starting at the end 
+        // and ending at the first element in the list. The elements of the list 
+        // are compared to the given value using the Object.Equals method.
+        // 
+        // This method uses the Array.LastIndexOf method to perform the
+        // search.
+        // 
+        public int LastIndexOf(T item)
+        {
+            if (_size == 0)
+            {  // Special case for empty list
+                return -1;
+            }
+            else
+            {
+                return LastIndexOf(item, _size - 1, _size);
+            }
+        }
+
+        // Returns the index of the last occurrence of a given value in a range of
+        // this list. The list is searched backwards, starting at index
+        // index and ending at the first element in the list. The 
+        // elements of the list are compared to the given value using the 
+        // Object.Equals method.
+        // 
+        // This method uses the Array.LastIndexOf method to perform the
+        // search.
+        // 
+        public int LastIndexOf(T item, int index)
+        {
+            return LastIndexOf(item, index, index + 1);
+        }
+
+        // Returns the index of the last occurrence of a given value in a range of
+        // this list. The list is searched backwards, starting at index
+        // index and upto count elements. The elements of
+        // the list are compared to the given value using the Object.Equals
+        // method.
+        // 
+        // This method uses the Array.LastIndexOf method to perform the
+        // search.
+        // 
+        public int LastIndexOf(T item, int index, int count)
+        {
+            if (_size == 0)
+            {  // Special case for empty list
+                return -1;
+            }
+
+            return Array.LastIndexOf(_items, item, index, count);
+        }
+
+        // Removes the element at the given index. The size of the list is
+        // decreased by one.
+        // 
+        public bool Remove(T item)
+        {
+            int index = IndexOf(item);
+            if (index >= 0)
+            {
+                RemoveAt(index);
+                return true;
+            }
+
+            return false;
+        }
+
+        void System.Collections.IList.Remove(Object item)
+        {
+            if (IsCompatibleObject(item))
+            {
+                Remove((T)item);
+            }
+        }
+
+        // This method removes all items which matches the predicate.
+        // The complexity is O(n).   
+        public int RemoveAll(Predicate<T> match)
+        {
+            int freeIndex = 0;   // the first free slot in items array
+
+            // Find the first item which needs to be removed.
+            while (freeIndex < _size && !match(_items[freeIndex])) freeIndex++;
+            if (freeIndex >= _size) return 0;
+
+            int current = freeIndex + 1;
+            while (current < _size)
+            {
+                // Find the first item which needs to be kept.
+                while (current < _size && match(_items[current])) current++;
+
+                if (current < _size)
+                {
+                    // copy item to the free slot.
+                    _items[freeIndex++] = _items[current++];
+                }
+            }
+
+            if (!typeof(T).IsValueType)
+            {
+                Array.Clear(_items, freeIndex, _size - freeIndex); // Clear the elements so that the gc can reclaim the references.
+            }
+
+            int result = _size - freeIndex;
+            _size = freeIndex;
+            _version++;
+            return result;
+        }
+
+        // Removes the element at the given index. The size of the list is
+        // decreased by one.
+        // 
+        public void RemoveAt(int index)
+        {
+            _size--;
+            if (index < _size)
+            {
+                Array.Copy(_items, index + 1, _items, index, _size - index);
+            }
+            if (!typeof(T).IsValueType)
+            {
+                _items[_size] = default(T);
+            }
+            _version++;
+        }
+
+        // Removes a range of elements from this list.
+        // 
+        public void RemoveRange(int index, int count)
+        {
+            if (count > 0)
+            {
+                int i = _size;
+                _size -= count;
+                if (index < _size)
+                {
+                    Array.Copy(_items, index + count, _items, index, _size - index);
+                }
+
+                _version++;
+                if (!typeof(T).IsValueType)
+                {
+                    Array.Clear(_items, _size, count);
+                }
+            }
+        }
+
+        // Reverses the elements in this list.
+        public void Reverse()
+        {
+            Reverse(0, Count);
+        }
+
+        // Reverses the elements in a range of this list. Following a call to this
+        // method, an element in the range given by index and count
+        // which was previously located at index i will now be located at
+        // index index + (index + count - i - 1).
+        // 
+        public void Reverse(int index, int count)
+        {
+            if (count > 1)
+            {
+                Array.Reverse(_items, index, count);
+            }
+            _version++;
+        }
+
+        // Sorts the elements in this list.  Uses the default comparer and 
+        // Array.Sort.
+        public void Sort()
+        {
+            Sort(0, Count, null);
+        }
+
+        // Sorts the elements in this list.  Uses Array.Sort with the
+        // provided comparer.
+        public void Sort(IComparer<T> comparer)
+        {
+            Sort(0, Count, comparer);
+        }
+
+        // Sorts the elements in a section of this list. The sort compares the
+        // elements to each other using the given IComparer interface. If
+        // comparer is null, the elements are compared to each other using
+        // the IComparable interface, which in that case must be implemented by all
+        // elements of the list.
+        // 
+        // This method uses the Array.Sort method to sort the elements.
+        // 
+        public void Sort(int index, int count, IComparer<T> comparer)
+        {
+            if (count > 1)
+            {
+                Array.Sort<T>(_items, index, count, comparer);
+            }
+            _version++;
+        }
+
+        public void Sort(Comparison<T> comparison)
+        {
+            throw new NotImplementedException();
+            /*if (_size > 1)
+            {
+                ArraySortHelper<T>.Sort(_items, 0, _size, comparison);
+            }
+            _version++;*/
+        }
+
+        // ToArray returns an array containing the contents of the UncheckedList.
+        // This requires copying the UncheckedList, which is an O(n) operation.
+        public T[] ToArray()
+        {
+            if (_size == 0)
+            {
+                return _emptyArray;
+            }
+
+            T[] array = new T[_size];
+            Array.Copy(_items, 0, array, 0, _size);
+            return array;
+        }
+
+        // Sets the capacity of this list to the size of the list. This method can
+        // be used to minimize a list's memory overhead once it is known that no
+        // new elements will be added to the list. To completely clear a list and
+        // release all memory referenced by the list, execute the following
+        // statements:
+        // 
+        // list.Clear();
+        // list.TrimExcess();
+        // 
+        public void TrimExcess()
+        {
+            int threshold = (int)(((double)_items.Length) * 0.9);
+            if (_size < threshold)
+            {
+                Capacity = _size;
+            }
+        }
+
+        public bool TrueForAll(Predicate<T> match)
+        {
+            for (int i = 0; i < _size; i++)
+            {
+                if (!match(_items[i]))
+                {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        private void AddEnumerable(IEnumerable<T> enumerable)
+        {
+            Debug.Assert(enumerable != null);
+            Debug.Assert(!(enumerable is ICollection<T>), "We should have optimized for this beforehand.");
+
+            using (IEnumerator<T> en = enumerable.GetEnumerator())
+            {
+                _version++; // Even if the enumerable has no items, we can update _version.
+
+                while (en.MoveNext())
+                {
+                    // Capture Current before doing anything else. If this throws
+                    // an exception, we want to make a clean break.
+                    T current = en.Current;
+
+                    if (_size == _items.Length)
+                    {
+                        EnsureCapacity(_size + 1);
+                    }
+
+                    _items[_size++] = current;
+                }
+            }
+        }
+
+        public struct Enumerator : IEnumerator<T>, System.Collections.IEnumerator
+        {
+            private UncheckedList<T> list;
+            private int index;
+            private int version;
+            private T current;
+
+            internal Enumerator(UncheckedList<T> list)
+            {
+                this.list = list;
+                index = 0;
+                version = list._version;
+                current = default(T);
+            }
+
+            public void Dispose()
+            {
+            }
+
+            public bool MoveNext()
+            {
+                UncheckedList<T> localUncheckedList = list;
+
+                if (version == localUncheckedList._version && ((uint)index < (uint)localUncheckedList._size))
+                {
+                    current = localUncheckedList._items[index];
+                    index++;
+                    return true;
+                }
+                return MoveNextRare();
+            }
+
+            private bool MoveNextRare()
+            {
+                index = list._size + 1;
+                current = default(T);
+                return false;
+            }
+
+            public T Current
+            {
+                get
+                {
+                    return current;
+                }
+            }
+
+            Object System.Collections.IEnumerator.Current
+            {
+                get
+                {
+                    return Current;
+                }
+            }
+
+            void System.Collections.IEnumerator.Reset()
+            {
+                index = 0;
+                current = default(T);
+            }
+        }
+    }
+}

+ 2 - 2
Unity/Assets/PSD2UGUI/Effects/Collections/ListPool.cs.meta → Unity/Assets/ILRuntime/ILRuntime/Other/UncheckedList.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: e45925eea4a843246be53091a0caaaea
-timeCreated: 1487670266
+guid: d923aa25f3f74d0489a9082f6a6c9942
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/Extensions.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ccb5490caeef4b7419ab1fc0690f19bd
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeConstructorInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 205a4012c9bebf742b9ebdc7fcb24bff
-timeCreated: 1496395687
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeFieldInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 05340a76bcd08e843b481e980988b984
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeMethodInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: e975e373cbb39824c9dbf99b5b834f76
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeParameterInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: fb4bb43a033c2ee45b6957d7b7ae8b28
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimePropertyInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 15c350fc8adaa6d43b32f054dacecead
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: f3a1f2fbaeb2b8446a4001bf4537351d
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Reflection/ILRuntimeWrapperType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: eaefbb07ad715ce489ab00dd2080222f
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Adaptors/CLRCrossBindingAdaptors.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 9c62df830a323ae4185db20e52d0a559
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 305 - 848
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs

@@ -4,12 +4,13 @@ using System.Reflection;
 using System.Linq;
 using System.Text;
 using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Other;
 
 namespace ILRuntime.Runtime.CLRBinding
 {
     public class BindingCodeGenerator
     {
-        public static void GenerateBindingCode(List<Type> types, string outputPath, HashSet<MethodBase> excludes = null)
+        public static void GenerateBindingCode(List<Type> types, string outputPath, HashSet<MethodBase> excludeMethods = null, HashSet<FieldInfo> excludeFields = null)
         {
             if (!System.IO.Directory.Exists(outputPath))
                 System.IO.Directory.CreateDirectory(outputPath);
@@ -32,6 +33,7 @@ namespace ILRuntime.Runtime.CLRBinding
                     sw.Write(@"using System;
 using System.Collections.Generic;
 using System.Reflection;
+using System.Runtime.InteropServices;
 
 using ILRuntime.CLR.TypeSystem;
 using ILRuntime.CLR.Method;
@@ -50,23 +52,35 @@ namespace ILRuntime.Runtime.Generated
         {
             BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
             MethodBase method;
+            FieldInfo field;
             Type[] args;
             Type type = typeof(");
                     sw.Write(realClsName);
                     sw.WriteLine(");");
                     MethodInfo[] methods = i.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
-                    string registerCode = GenerateRegisterCode(i, methods, excludes);
-                    string commonCode = GenerateCommonCode(i, realClsName);
+                    FieldInfo[] fields = i.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
+                    string registerMethodCode = i.GenerateMethodRegisterCode(methods, excludeMethods);
+                    string registerFieldCode = i.GenerateFieldRegisterCode(fields, excludeFields);
+                    string registerValueTypeCode = i.GenerateValueTypeRegisterCode(realClsName);
+                    string registerMiscCode = i.GenerateMiscRegisterCode(realClsName, true, true);
+                    string commonCode = i.GenerateCommonCode(realClsName);
                     ConstructorInfo[] ctors = i.GetConstructors(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
-                    string ctorRegisterCode = GenerateConstructorRegisterCode(i, ctors, excludes);
-                    string wraperCode = GenerateWraperCode(i, methods, realClsName, excludes);
-                    string ctorWraperCode = GenerateConstructorWraperCode(i, ctors, realClsName, excludes);
-                    sw.WriteLine(registerCode);
+                    string ctorRegisterCode = i.GenerateConstructorRegisterCode(ctors, excludeMethods);
+                    string methodWraperCode = i.GenerateMethodWraperCode(methods, realClsName, excludeMethods);
+                    string fieldWraperCode = i.GenerateFieldWraperCode(fields, realClsName, excludeFields);
+                    string cloneWraperCode = i.GenerateCloneWraperCode(fields, realClsName);
+                    string ctorWraperCode = i.GenerateConstructorWraperCode(ctors, realClsName, excludeMethods);
+                    sw.WriteLine(registerMethodCode);
+                    sw.WriteLine(registerFieldCode);
+                    sw.WriteLine(registerValueTypeCode);
+                    sw.WriteLine(registerMiscCode);
                     sw.WriteLine(ctorRegisterCode);
                     sw.WriteLine("        }");
                     sw.WriteLine();
                     sw.WriteLine(commonCode);
-                    sw.WriteLine(wraperCode);
+                    sw.WriteLine(methodWraperCode);
+                    sw.WriteLine(fieldWraperCode);
+                    sw.WriteLine(cloneWraperCode);
                     sw.WriteLine(ctorWraperCode);
                     sw.WriteLine("    }");
                     sw.WriteLine("}");
@@ -102,898 +116,341 @@ namespace ILRuntime.Runtime.Generated
             }
         }
 
-        static bool ShouldSkipMethod(Type type, MethodBase i)
+        class CLRBindingGenerateInfo
         {
-            if (i.IsPrivate)
-                return true;
-            if (i.IsGenericMethod)
-                return true;
-            //EventHandler is currently not supported
-            if (i.IsSpecialName)
+            public Type Type { get; set; }
+            public HashSet<MethodInfo> Methods { get; set; }
+            public HashSet<FieldInfo> Fields { get; set; }
+            public HashSet<ConstructorInfo> Constructors { get; set; }
+            public bool ArrayNeeded { get; set; }
+            public bool DefaultInstanceNeeded { get; set; }
+            public bool ValueTypeNeeded { get; set; }
+
+            public bool NeedGenerate
             {
-                string[] t = i.Name.Split('_');
-                if (t[0] == "add" || t[0] == "remove")
-                    return true;
-                if (t[0] == "get" || t[0] == "set")
+                get
                 {
-                    var prop = type.GetProperty(t[1]);
-                    if (prop == null)
-                        return true;
-                    if (prop.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
+                    if (Methods.Count == 0 && Constructors.Count == 0 && Fields.Count == 0 && !ArrayNeeded && !DefaultInstanceNeeded && !ValueTypeNeeded)
+                        return false;
+                    else
+                    {
+                        //Making CLRBinding for such types makes no sense
+                        if (Type == typeof(Delegate) || Type == typeof(System.Runtime.CompilerServices.RuntimeHelpers))
+                            return false;
                         return true;
+                    }
                 }
             }
-            if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
-                return true;
-            var param = i.GetParameters();
-            foreach(var j in param)
-            {
-                if (j.ParameterType.IsPointer)
-                    return true;
-            }
-            return false;
         }
 
-        static string GenerateRegisterCode(Type type, MethodInfo[] methods, HashSet<MethodBase> excludes)
+        public static void GenerateBindingCode(ILRuntime.Runtime.Enviorment.AppDomain domain, string outputPath)
         {
-            StringBuilder sb = new StringBuilder();
-            int idx = 0;
-            foreach (var i in methods)
+            if (domain == null)
+                return;
+            if (!System.IO.Directory.Exists(outputPath))
+                System.IO.Directory.CreateDirectory(outputPath);
+            Dictionary<Type, CLRBindingGenerateInfo> infos = new Dictionary<Type, CLRBindingGenerateInfo>(new ByReferenceKeyComparer<Type>());
+            CrawlAppdomain(domain, infos);
+            string[] oldFiles = System.IO.Directory.GetFiles(outputPath, "*.cs");
+            foreach (var i in oldFiles)
             {
-                if (excludes != null && excludes.Contains(i))
-                    continue;
-                if (ShouldSkipMethod(type, i))
-                    continue;
-                bool isProperty = i.IsSpecialName;
-                var param = i.GetParameters();
-                StringBuilder sb2 = new StringBuilder();
-                sb2.Append("{");
-                bool first = true;
-                foreach (var j in param)
-                {
-                    if (first)
-                        first = false;
-                    else
-                        sb2.Append(", ");
-                    sb2.Append("typeof(");
-                    string tmp, clsName;
-                    bool isByRef;
-                    j.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
-                    sb2.Append(clsName);
-                    sb2.Append(")");
-                    if (isByRef)
-                        sb2.Append(".MakeByRefType()");
-                }
-                sb2.Append("}");
-                sb.AppendLine(string.Format("            args = new Type[]{0};", sb2));
-                sb.AppendLine(string.Format("            method = type.GetMethod(\"{0}\", flag, null, args, null);", i.Name));
-                sb.AppendLine(string.Format("            app.RegisterCLRMethodRedirection(method, {0}_{1});", i.Name, idx));
-
-                idx++;
+                System.IO.File.Delete(i);
             }
-            return sb.ToString();
-        }
 
-        static string GenerateConstructorRegisterCode(Type type, ConstructorInfo[] methods, HashSet<MethodBase> excludes)
-        {
-            StringBuilder sb = new StringBuilder();
-            int idx = 0;
-            foreach (var i in methods)
+            HashSet<MethodBase> excludeMethods = null;
+            HashSet<FieldInfo> excludeFields = null;
+            List<string> clsNames = new List<string>();
+            foreach (var info in infos)
             {
-                if (excludes != null && excludes.Contains(i))
+                if (!info.Value.NeedGenerate)
                     continue;
-                if (ShouldSkipMethod(type, i))
+                Type i = info.Value.Type;
+                string clsName, realClsName;
+                bool isByRef;
+                if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
                     continue;
-                var param = i.GetParameters();
-                StringBuilder sb2 = new StringBuilder();
-                sb2.Append("{");
-                bool first = true;
-                foreach (var j in param)
+                i.GetClassName(out clsName, out realClsName, out isByRef);
+                clsNames.Add(clsName);
+                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputPath + "/" + clsName + ".cs", false, Encoding.UTF8))
                 {
-                    if (first)
-                        first = false;
-                    else
-                        sb2.Append(", ");
-                    sb2.Append("typeof(");
-                    string tmp, clsName;
-                    bool isByRef;
-                    j.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
-                    sb2.Append(clsName);
-                    sb2.Append(")");
-                    if (isByRef)
-                        sb2.Append(".MakeByRefType()");
-                }
-                sb2.Append("}");
-                sb.AppendLine(string.Format("            args = new Type[]{0};", sb2));
-                sb.AppendLine("            method = type.GetConstructor(flag, null, args, null);");
-                sb.AppendLine(string.Format("            app.RegisterCLRMethodRedirection(method, Ctor_{0});",idx));
+                    sw.Write(@"using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Runtime.InteropServices;
 
-                idx++;
-            }
-            return sb.ToString();
-        }
+using ILRuntime.CLR.TypeSystem;
+using ILRuntime.CLR.Method;
+using ILRuntime.Runtime.Enviorment;
+using ILRuntime.Runtime.Intepreter;
+using ILRuntime.Runtime.Stack;
+using ILRuntime.Reflection;
+using ILRuntime.CLR.Utils;
 
-        static string GenerateCommonCode(Type type, string typeClsName)
+namespace ILRuntime.Runtime.Generated
+{
+    unsafe class ");
+                    sw.WriteLine(clsName);
+                    sw.Write(@"    {
+        public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
         {
-            if (!type.IsValueType)
-                return "";
-            StringBuilder sb = new StringBuilder();
-            if (type.IsPrimitive)
-            {
-                sb.AppendLine(string.Format("        static {0} GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, List<object> __mStack)", typeClsName));
-                sb.AppendLine("        {");
-                if (type.IsPrimitive || type.IsValueType)
-                    sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
-                sb.AppendLine(string.Format("            {0} instance_of_this_method;", typeClsName));
-                sb.Append(@"            switch(ptr_of_this_method->ObjectType)
-            {
-                case ObjectTypes.FieldReference:
-                    {
-                        var instance_of_fieldReference = __mStack[ptr_of_this_method->Value];
-                        if(instance_of_fieldReference is ILTypeInstance)
-                        {
-                            instance_of_this_method = (");
-                sb.Append(typeClsName);
-                sb.Append(")((ILTypeInstance)instance_of_fieldReference)[ptr_of_this_method->ValueLow];");
-                sb.Append(@"
-                        }
-                        else
-                        {
-                            var t = __domain.GetType(instance_of_fieldReference.GetType()) as CLRType;
-                            instance_of_this_method = (");
-                sb.Append(typeClsName);
-                sb.Append(")t.GetField(ptr_of_this_method->ValueLow).GetValue(instance_of_fieldReference);");
-                sb.Append(@"
-                        }
-                    }
-                    break;
-                case ObjectTypes.StaticFieldReference:
-                    {
-                        var t = __domain.GetType(ptr_of_this_method->Value);
-                        if(t is ILType)
-                        {
-                            instance_of_this_method = (");
-                sb.Append(typeClsName);
-                sb.Append(@")((ILType)t).StaticInstance[ptr_of_this_method->ValueLow];
-                        }
-                        else
-                        {
-                            instance_of_this_method = (");
-                sb.Append(typeClsName);
-                sb.Append(@")((CLRType)t).GetField(ptr_of_this_method->ValueLow).GetValue(null);
-                        }
-                    }
-                    break;
-                case ObjectTypes.ArrayReference:
-                    {
-                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
-                sb.Append(typeClsName);
-                sb.AppendLine(@"[];
-                        instance_of_this_method = instance_of_arrayReference[ptr_of_this_method->ValueLow];                        
-                    }
-                    break;
-                default:");
-                sb.AppendLine(string.Format("                    instance_of_this_method = {0};", GetRetrieveValueCode(type, typeClsName)));
-                sb.AppendLine(@"                    break;
-            }
-            return instance_of_this_method;");
-                sb.AppendLine("        }");
-            }
-            if (!type.IsPrimitive && !type.IsAbstract)
-            {
-                sb.AppendLine(string.Format("        static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, List<object> __mStack, ref {0} instance_of_this_method)", typeClsName));
-                sb.AppendLine("        {");
-                sb.AppendLine(@"            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
-            switch(ptr_of_this_method->ObjectType)
-            {
-                case ObjectTypes.Object:
-                    {
-                        __mStack[ptr_of_this_method->Value] = instance_of_this_method;");
-                sb.Append(@"                    }
-                    break;
-                case ObjectTypes.FieldReference:
-                    {
-                        var ___obj = __mStack[ptr_of_this_method->Value];
-                        if(___obj is ILTypeInstance)
-                        {
-                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = instance_of_this_method");
-                sb.Append(@";
-                        }
-                        else
-                        {
-                            var t = __domain.GetType(___obj.GetType()) as CLRType;
-                            t.GetField(ptr_of_this_method->ValueLow).SetValue(___obj, instance_of_this_method");
-                sb.Append(@");
-                        }
-                    }
-                    break;
-                case ObjectTypes.StaticFieldReference:
-                    {
-                        var t = __domain.GetType(ptr_of_this_method->Value);
-                        if(t is ILType)
-                        {
-                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = instance_of_this_method");
-                sb.Append(@";
-                        }
-                        else
-                        {
-                            ((CLRType)t).GetField(ptr_of_this_method->ValueLow).SetValue(null, instance_of_this_method");
-                sb.Append(@");
-                        }
-                    }
-                    break;
-                 case ObjectTypes.ArrayReference:
+            BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
+            MethodBase method;
+            FieldInfo field;
+            Type[] args;
+            Type type = typeof(");
+                    sw.Write(realClsName);
+                    sw.WriteLine(");");
+                    MethodInfo[] methods = info.Value.Methods.ToArray();
+                    FieldInfo[] fields = info.Value.Fields.ToArray();
+                    string registerMethodCode = i.GenerateMethodRegisterCode(methods, excludeMethods);
+                    string registerFieldCode = fields.Length > 0 ? i.GenerateFieldRegisterCode(fields, excludeFields) : null;
+                    string registerValueTypeCode = info.Value.ValueTypeNeeded ? i.GenerateValueTypeRegisterCode(realClsName) : null;
+                    string registerMiscCode = i.GenerateMiscRegisterCode(realClsName, info.Value.DefaultInstanceNeeded, info.Value.ArrayNeeded);
+                    string commonCode = i.GenerateCommonCode(realClsName);
+                    ConstructorInfo[] ctors = info.Value.Constructors.ToArray();
+                    string ctorRegisterCode = i.GenerateConstructorRegisterCode(ctors, excludeMethods);
+                    string methodWraperCode = i.GenerateMethodWraperCode(methods, realClsName, excludeMethods);
+                    string fieldWraperCode = fields.Length > 0 ? i.GenerateFieldWraperCode(fields, realClsName, excludeFields) : null;
+                    string cloneWraperCode = null;
+                    if (info.Value.ValueTypeNeeded)
                     {
-                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
-                sb.Append(typeClsName);
-                sb.AppendLine(@"[];
-                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = instance_of_this_method;
+                        //Memberwise clone should copy all fields
+                        var fs = i.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);
+                        cloneWraperCode = i.GenerateCloneWraperCode(fs, realClsName);
                     }
-                    break;
-            }");
-                sb.AppendLine(@"        }");
-            }
-            return sb.ToString();
-        }
-
-        static string GenerateConstructorWraperCode(Type type, ConstructorInfo[] methods, string typeClsName, HashSet<MethodBase> excludes)
-        {
-            StringBuilder sb = new StringBuilder();
-
-            int idx = 0;
-            foreach (var i in methods)
-            {
-                if (excludes != null && excludes.Contains(i))
-                    continue;
-                if (ShouldSkipMethod(type, i) || i.IsStatic)
-                    continue;
-                var param = i.GetParameters();
-                int paramCnt = param.Length;
-                sb.AppendLine(string.Format("        static StackObject* Ctor_{0}(ILIntepreter __intp, StackObject* __esp, List<object> __mStack, CLRMethod __method, bool isNewObj)", idx));
-                sb.AppendLine("        {");
-                sb.AppendLine("            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
-                sb.AppendLine("            StackObject* ptr_of_this_method;");
-                sb.AppendLine(string.Format("            StackObject* __ret = ILIntepreter.Minus(__esp, {0});", paramCnt));
-                for (int j = param.Length; j > 0; j--)
-                {
-                    var p = param[j - 1];
-                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
-                    string tmp, clsName;
-                    bool isByRef;
-                    p.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
-                    if (isByRef)
-                        sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
-                    sb.AppendLine(string.Format("            {0} {1} = {2};", clsName, p.Name, GetRetrieveValueCode(p.ParameterType, clsName)));
-                    if (!isByRef && !p.ParameterType.IsPrimitive)
-                        sb.AppendLine("            __intp.Free(ptr_of_this_method);");
+                    string ctorWraperCode = i.GenerateConstructorWraperCode(ctors, realClsName, excludeMethods);
+                    sw.WriteLine(registerMethodCode);
+                    if (fields.Length > 0)
+                        sw.WriteLine(registerFieldCode);
+                    if (info.Value.ValueTypeNeeded)
+                        sw.WriteLine(registerValueTypeCode);
+                    if (!string.IsNullOrEmpty(registerMiscCode))
+                        sw.WriteLine(registerMiscCode);
+                    sw.WriteLine(ctorRegisterCode);
+                    sw.WriteLine("        }");
+                    sw.WriteLine();
+                    sw.WriteLine(commonCode);
+                    sw.WriteLine(methodWraperCode);
+                    if (fields.Length > 0)
+                        sw.WriteLine(fieldWraperCode);
+                    if (info.Value.ValueTypeNeeded)
+                        sw.WriteLine(cloneWraperCode);
+                    sw.WriteLine(ctorWraperCode);
+                    sw.WriteLine("    }");
+                    sw.WriteLine("}");
+                    sw.Flush();
                 }
-                sb.AppendLine();
-                sb.Append("            var result_of_this_method = ");
-                {
-                    string tmp, clsName;
-                    bool isByRef;
-                    type.GetClassName(out tmp, out clsName, out isByRef);
-                    sb.Append(string.Format("new {0}(", clsName));
-                    AppendParameters(param, sb);
-                    sb.AppendLine(");");
+            }
 
-                }
-                sb.AppendLine();
-                if (type.IsValueType)
-                {
-                    sb.AppendLine(@"            if(!isNewObj)
+            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(outputPath + "/CLRBindings.cs", false, Encoding.UTF8))
             {
-                __ret--;
-                WriteBackInstance(__domain, __ret, __mStack, ref result_of_this_method);
-                return __ret;
-            }"); 
-                }
+                sw.WriteLine(@"using System;
+using System.Collections.Generic;
+using System.Reflection;
 
-                //Ref/Out
-                for (int j = param.Length; j > 0; j--)
+namespace ILRuntime.Runtime.Generated
+{
+    class CLRBindings
+    {
+        /// <summary>
+        /// Initialize the CLR binding, please invoke this AFTER CLR Redirection registration
+        /// </summary>
+        public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
+        {");
+                foreach (var i in clsNames)
                 {
-                    var p = param[j - 1];
-                    if (!p.ParameterType.IsByRef)
-                        continue;
-                    string tmp, clsName;
-                    bool isByRef;
-                    p.ParameterType.GetElementType().GetClassName(out tmp, out clsName, out isByRef);
-                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
-                    sb.AppendLine(@"            switch(ptr_of_this_method->ObjectType)
-            {
-                case ObjectTypes.StackObjectReference:
-                    {
-                        var ___dst = *(StackObject**)&ptr_of_this_method->Value;");
-                    GetRefWriteBackValueCode(p.ParameterType.GetElementType(), sb, p.Name);
-                    sb.Append(@"                    }
-                    break;
-                case ObjectTypes.FieldReference:
-                    {
-                        var ___obj = __mStack[ptr_of_this_method->Value];
-                        if(___obj is ILTypeInstance)
-                        {
-                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.Append(@";
-                        }
-                        else
-                        {
-                            var t = __domain.GetType(___obj.GetType()) as CLRType;
-                            t.GetField(ptr_of_this_method->ValueLow).SetValue(___obj, ");
-                    sb.Append(p.Name);
-                    sb.Append(@");
-                        }
-                    }
-                    break;
-                case ObjectTypes.StaticFieldReference:
-                    {
-                        var t = __domain.GetType(ptr_of_this_method->Value);
-                        if(t is ILType)
-                        {
-                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.Append(@";
-                        }
-                        else
-                        {
-                            ((CLRType)t).GetField(ptr_of_this_method->ValueLow).SetValue(null, ");
-                    sb.Append(p.Name);
-                    sb.Append(@");
-                        }
-                    }
-                    break;
-                 case ObjectTypes.ArrayReference:
-                    {
-                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
-                    sb.Append(clsName);
-                    sb.Append(@"[];
-                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.AppendLine(@";
-                    }
-                    break;
-            }");
-                    sb.AppendLine();
+                    sw.Write("            ");
+                    sw.Write(i);
+                    sw.WriteLine(".Register(app);");
                 }
-                sb.AppendLine("            return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);");
 
-                sb.AppendLine("        }");
-                sb.AppendLine();
-                idx++;
+                sw.WriteLine(@"        }
+    }
+}");
             }
-
-            return sb.ToString();
         }
 
-        static string GenerateWraperCode(Type type, MethodInfo[] methods, string typeClsName, HashSet<MethodBase> excludes)
+        static void CrawlAppdomain(ILRuntime.Runtime.Enviorment.AppDomain domain, Dictionary<Type, CLRBindingGenerateInfo> infos)
         {
-            StringBuilder sb = new StringBuilder();
-
-            int idx = 0;
-            foreach (var i in methods)
+            var arr = domain.LoadedTypes.Values.ToArray();
+            //Prewarm
+            foreach (var type in arr)
             {
-                if (excludes != null && excludes.Contains(i))
-                    continue;
-                if (ShouldSkipMethod(type, i))
-                    continue;
-                bool isProperty = i.IsSpecialName;
-                var param = i.GetParameters();
-                int paramCnt = param.Length;
-                if (!i.IsStatic)
-                    paramCnt++;
-                sb.AppendLine(string.Format("        static StackObject* {0}_{1}(ILIntepreter __intp, StackObject* __esp, List<object> __mStack, CLRMethod __method, bool isNewObj)", i.Name, idx));
-                sb.AppendLine("        {");
-                sb.AppendLine("            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
-                sb.AppendLine("            StackObject* ptr_of_this_method;");
-                sb.AppendLine(string.Format("            StackObject* __ret = ILIntepreter.Minus(__esp, {0});", paramCnt));
-                for (int j = param.Length; j > 0; j--)
-                {
-                    var p = param[j - 1];
-                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
-                    string tmp, clsName;
-                    bool isByRef;
-                    p.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
-                    if (isByRef)
-                        sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
-                    sb.AppendLine(string.Format("            {0} {1} = {2};", clsName, p.Name, GetRetrieveValueCode(p.ParameterType, clsName)));
-                    if (!isByRef && !p.ParameterType.IsPrimitive)
-                        sb.AppendLine("            __intp.Free(ptr_of_this_method);");
-                }
-                if (!i.IsStatic)
-                {
-                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", paramCnt));
-                    if (type.IsPrimitive)
-                        sb.AppendLine(string.Format("            {0} instance_of_this_method = GetInstance(__domain, ptr_of_this_method, __mStack);", typeClsName));
-                    else
-                    {
-                        if (type.IsValueType)
-                            sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
-                        sb.AppendLine(string.Format("            {0} instance_of_this_method;", typeClsName));
-                        sb.AppendLine(string.Format("            instance_of_this_method = {0};", GetRetrieveValueCode(type, typeClsName)));
-                        if (!type.IsValueType)
-                            sb.AppendLine("            __intp.Free(ptr_of_this_method);");
-                    }
-                }
-                sb.AppendLine();
-                if (i.ReturnType != typeof(void))
-                {
-                    sb.Append("            var result_of_this_method = ");
-                }
-                else
-                    sb.Append("            ");
-                if (i.IsStatic)
+                if (type is CLR.TypeSystem.ILType)
                 {
-                    if (isProperty)
+                    if (type.HasGenericParameter)
+                        continue;
+                    var methods = type.GetMethods().ToList();
+                    foreach (var i in ((CLR.TypeSystem.ILType)type).GetConstructors())
+                        methods.Add(i);
+                    if (((CLR.TypeSystem.ILType)type).GetStaticConstroctor() != null)
+                        methods.Add(((CLR.TypeSystem.ILType)type).GetStaticConstroctor());
+                    foreach (var j in methods)
                     {
-                        string[] t = i.Name.Split('_');
-                        string propType = t[0];
-
-                        if (propType == "get")
-                        {
-                            bool isIndexer = param.Length > 0;
-                            if (isIndexer)
-                            {
-                                sb.AppendLine(string.Format("{1}[{0}];", param[0].Name, typeClsName));
-                            }
-                            else
-                                sb.AppendLine(string.Format("{1}.{0};", t[1], typeClsName));
-                        }
-                        else if (propType == "set")
-                        {
-                            bool isIndexer = param.Length > 1;
-                            if (isIndexer)
-                            {
-                                sb.AppendLine(string.Format("{2}[{0}] = {1};", param[0].Name, param[1].Name, typeClsName));
-                            }
-                            else
-                                sb.AppendLine(string.Format("{2}.{0} = {1};", t[1], param[0].Name, typeClsName));
-                        }
-                        else if (propType == "op")
+                        CLR.Method.ILMethod method = j as CLR.Method.ILMethod;
+                        if (method != null)
                         {
-                            switch (t[1])
-                            {
-                                case "Equality":
-                                    sb.AppendLine(string.Format("{0} == {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "Inequality":
-                                    sb.AppendLine(string.Format("{0} != {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "Addition":
-                                    sb.AppendLine(string.Format("{0} + {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "Subtraction":
-                                    sb.AppendLine(string.Format("{0} - {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "Multiply":
-                                    sb.AppendLine(string.Format("{0} * {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "Division":
-                                    sb.AppendLine(string.Format("{0} / {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "GreaterThan":
-                                    sb.AppendLine(string.Format("{0} > {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "GreaterThanOrEqual":
-                                    sb.AppendLine(string.Format("{0} >= {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "LessThan":
-                                    sb.AppendLine(string.Format("{0} < {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "LessThanOrEqual":
-                                    sb.AppendLine(string.Format("{0} <= {1};", param[0].Name, param[1].Name));
-                                    break;
-                                case "UnaryNegation":
-                                    sb.AppendLine(string.Format("-{0};", param[0].Name));
-                                    break;
-                                case "Implicit":
-                                case "Explicit":
-                                    {
-                                        string tmp, clsName;
-                                        bool isByRef;
-                                        i.ReturnType.GetClassName(out tmp, out clsName, out isByRef);
-                                        sb.AppendLine(string.Format("({1}){0};", param[0].Name, clsName));
-                                    }
-                                    break;
-                                default:
-                                    throw new NotImplementedException(i.Name);
-                            }
+                            if (method.GenericParameterCount > 0 && !method.IsGenericInstance)
+                                continue;
+                            var body = method.Body;
                         }
-                        else
-                            throw new NotImplementedException();
-                    }
-                    else
-                    {
-                        sb.Append(string.Format("{0}.{1}(", typeClsName, i.Name));
-                        AppendParameters(param, sb);
-                        sb.AppendLine(");");
                     }
                 }
-                else
+            }
+            arr = domain.LoadedTypes.Values.ToArray();
+            foreach (var type in arr)
+            {
+                if (type is CLR.TypeSystem.ILType)
                 {
-                    if (isProperty)
-                    {
-                        string[] t = i.Name.Split('_');
-                        string propType = t[0];
+                    if (type.HasGenericParameter)
+                        continue;
+                    var methods = type.GetMethods().ToList();
+                    foreach (var i in ((CLR.TypeSystem.ILType)type).GetConstructors())
+                        methods.Add(i);
 
-                        if (propType == "get")
-                        {
-                            bool isIndexer = param.Length > 0;
-                            if (isIndexer)
-                            {
-                                sb.AppendLine(string.Format("instance_of_this_method[{0}];", param[0].Name));
-                            }
-                            else
-                                sb.AppendLine(string.Format("instance_of_this_method.{0};", t[1]));
-                        }
-                        else if (propType == "set")
+                    foreach (var j in methods)
+                    {
+                        CLR.Method.ILMethod method = j as CLR.Method.ILMethod;
+                        if (method != null)
                         {
-                            bool isIndexer = param.Length > 1;
-                            if (isIndexer)
+                            if (method.GenericParameterCount > 0 && !method.IsGenericInstance)
+                                continue;
+                            var body = method.Body;
+                            foreach (var ins in body)
                             {
-                                sb.AppendLine(string.Format("instance_of_this_method[{0}] = {1};", param[0].Name, param[1].Name));
-                            }
-                            else
-                                sb.AppendLine(string.Format("instance_of_this_method.{0} = {1};", t[1], param[0].Name));
-                        }
-                        else
-                            throw new NotImplementedException();
-                    }
-                    else
-                    {
-                        sb.Append(string.Format("instance_of_this_method.{0}(", i.Name));
-                        AppendParameters(param, sb);
-                        sb.AppendLine(");");
-                    }
-                }
-                sb.AppendLine();
-
+                                switch (ins.Code)
+                                {
+                                    case Intepreter.OpCodes.OpCodeEnum.Newobj:
+                                        {
+                                            CLR.Method.CLRMethod m = domain.GetMethod(ins.TokenInteger) as CLR.Method.CLRMethod;
+                                            if (m != null)
+                                            {
+                                                if (m.DeclearingType.IsDelegate)
+                                                    continue;
+                                                Type t = m.DeclearingType.TypeForCLR;
+                                                CLRBindingGenerateInfo info;
+                                                if (!infos.TryGetValue(t, out info))
+                                                {
+                                                    info = CreateNewBindingInfo(t);
+                                                    infos[t] = info;
+                                                }
+                                                if (m.IsConstructor)
+                                                    info.Constructors.Add(m.ConstructorInfo);
+                                                else
+                                                    info.Methods.Add(m.MethodInfo);
+                                            }
+                                        }
+                                        break;
+                                    case Intepreter.OpCodes.OpCodeEnum.Ldfld:
+                                    case Intepreter.OpCodes.OpCodeEnum.Stfld:
+                                    case Intepreter.OpCodes.OpCodeEnum.Ldflda:
+                                    case Intepreter.OpCodes.OpCodeEnum.Ldsfld:
+                                    case Intepreter.OpCodes.OpCodeEnum.Ldsflda:
+                                    case Intepreter.OpCodes.OpCodeEnum.Stsfld:
+                                        {
+                                            var t = domain.GetType((int)(ins.TokenLong >> 32)) as CLR.TypeSystem.CLRType;
+                                            if(t != null)
+                                            {
+                                                var fi = t.GetField((int)ins.TokenLong);
+                                                if (fi != null && fi.IsPublic)
+                                                {
+                                                    CLRBindingGenerateInfo info;
+                                                    if (!infos.TryGetValue(t.TypeForCLR, out info))
+                                                    {
+                                                        info = CreateNewBindingInfo(t.TypeForCLR);
+                                                        infos[t.TypeForCLR] = info;
+                                                    }
+                                                    if(ins.Code == Intepreter.OpCodes.OpCodeEnum.Stfld || ins.Code == Intepreter.OpCodes.OpCodeEnum.Stsfld)
+                                                    {
+                                                        if (t.IsValueType)
+                                                        {
+                                                            info.ValueTypeNeeded = true;
+                                                            info.DefaultInstanceNeeded = true;
+                                                        }
+                                                    }
+                                                    if (t.TypeForCLR.CheckCanPinn() || !t.IsValueType)
+                                                        info.Fields.Add(fi);
+                                                }
+                                            }
+                                        }
+                                        break;
+                                    case Intepreter.OpCodes.OpCodeEnum.Ldtoken:
+                                        {
+                                            if (ins.TokenInteger == 0)
+                                            {
+                                                var t = domain.GetType((int)(ins.TokenLong >> 32)) as CLR.TypeSystem.CLRType;
+                                                if (t != null)
+                                                {
+                                                    var fi = t.GetField((int)ins.TokenLong);
+                                                    if (fi != null)
+                                                    {
+                                                        CLRBindingGenerateInfo info;
+                                                        if (!infos.TryGetValue(t.TypeForCLR, out info))
+                                                        {
+                                                            info = CreateNewBindingInfo(t.TypeForCLR);
+                                                            infos[t.TypeForCLR] = info;
+                                                        }
+                                                        info.Fields.Add(fi);
+                                                    }
+                                                }
+                                            }
+                                        }
+                                        break;
+                                    case Intepreter.OpCodes.OpCodeEnum.Newarr:
+                                        {
+                                            var t = domain.GetType(ins.TokenInteger) as CLR.TypeSystem.CLRType;
+                                            if(t != null)
+                                            {
+                                                CLRBindingGenerateInfo info;
+                                                if (!infos.TryGetValue(t.TypeForCLR, out info))
+                                                {
+                                                    info = CreateNewBindingInfo(t.TypeForCLR);
+                                                    infos[t.TypeForCLR] = info;
+                                                }
+                                                info.ArrayNeeded = true;
+                                            }
+                                        }
+                                        break;
+                                    case Intepreter.OpCodes.OpCodeEnum.Call:
+                                    case Intepreter.OpCodes.OpCodeEnum.Callvirt:
+                                        {
+                                            CLR.Method.CLRMethod m = domain.GetMethod(ins.TokenInteger) as CLR.Method.CLRMethod;
+                                            if (m != null)
+                                            {
+                                                //Cannot explicit call base class's constructor directly
+                                                if (m.IsConstructor)
+                                                    continue;
+                                                if (!m.MethodInfo.IsPublic)
+                                                    continue;
+                                                Type t = m.DeclearingType.TypeForCLR;
+                                                CLRBindingGenerateInfo info;
+                                                if (!infos.TryGetValue(t, out info))
+                                                {
+                                                    info = CreateNewBindingInfo(t);
+                                                    infos[t] = info;
+                                                }
 
-                if (!i.IsStatic && type.IsValueType && !type.IsPrimitive)//need to write back value type instance
-                {
-                    sb.AppendLine("            WriteBackInstance(__domain, ptr_of_this_method, __mStack, ref instance_of_this_method);");
-                    sb.AppendLine();
-                }
-                //Ref/Out
-                for (int j = param.Length; j > 0; j--)
-                {
-                    var p = param[j - 1];
-                    if (!p.ParameterType.IsByRef)
-                        continue;
-                    string tmp, clsName;
-                    bool isByRef;
-                    p.ParameterType.GetElementType().GetClassName(out tmp, out clsName, out isByRef);
-                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
-                    sb.AppendLine(@"            switch(ptr_of_this_method->ObjectType)
-            {
-                case ObjectTypes.StackObjectReference:
-                    {
-                        var ___dst = *(StackObject**)&ptr_of_this_method->Value;");
-                    GetRefWriteBackValueCode(p.ParameterType.GetElementType(), sb, p.Name);
-                    sb.Append(@"                    }
-                    break;
-                case ObjectTypes.FieldReference:
-                    {
-                        var ___obj = __mStack[ptr_of_this_method->Value];
-                        if(___obj is ILTypeInstance)
-                        {
-                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.Append(@";
-                        }
-                        else
-                        {
-                            var t = __domain.GetType(___obj.GetType()) as CLRType;
-                            t.GetField(ptr_of_this_method->ValueLow).SetValue(___obj, ");
-                    sb.Append(p.Name);
-                    sb.Append(@");
-                        }
-                    }
-                    break;
-                case ObjectTypes.StaticFieldReference:
-                    {
-                        var t = __domain.GetType(ptr_of_this_method->Value);
-                        if(t is ILType)
-                        {
-                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.Append(@";
-                        }
-                        else
-                        {
-                            ((CLRType)t).GetField(ptr_of_this_method->ValueLow).SetValue(null, ");
-                    sb.Append(p.Name);
-                    sb.Append(@");
+                                                info.Methods.Add(m.MethodInfo);
+                                            }
+                                        }
+                                        break;
+                                }
+                            }
                         }
                     }
-                    break;
-                 case ObjectTypes.ArrayReference:
-                    {
-                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
-                    sb.Append(clsName);
-                    sb.Append(@"[];
-                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = ");
-                    sb.Append(p.Name);
-                    sb.AppendLine(@";
-                    }
-                    break;
-            }");
-                    sb.AppendLine();
                 }
-                if (i.ReturnType != typeof(void))
-                {
-                    GetReturnValueCode(i.ReturnType, sb);
-                }
-                else
-                    sb.AppendLine("            return __ret;");
-                sb.AppendLine("        }");
-                sb.AppendLine();
-                idx++;
             }
-
-            return sb.ToString();
         }
 
-        static void AppendParameters(ParameterInfo[] param, StringBuilder sb)
+        static CLRBindingGenerateInfo CreateNewBindingInfo(Type t)
         {
-            bool first = true;
-            foreach (var j in param)
-            {
-                if (first)
-                    first = false;
-                else
-                    sb.Append(", ");
-                if (j.IsOut)
-                    sb.Append("out ");
-                else if (j.ParameterType.IsByRef)
-                    sb.Append("ref ");
-                sb.Append(j.Name);
-            }
-        }
+            CLRBindingGenerateInfo info = new CLRBindingGenerateInfo();
+            info.Type = t;
+            info.Methods = new HashSet<MethodInfo>();
+            info.Fields = new HashSet<FieldInfo>();
+            info.Constructors = new HashSet<ConstructorInfo>();
 
-        static void GetRefWriteBackValueCode(Type type, StringBuilder sb, string paramName)
-        {
-            if (type.IsPrimitive)
-            {
-                if (type == typeof(int))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(long))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Long;");
-                    sb.Append("                        *(long*)&___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(short))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(bool))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName + " ? 1 : 0;");
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(ushort))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(float))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Float;");
-                    sb.Append("                        *(float*)&___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(double))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Double;");
-                    sb.Append("                        *(double*)&___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(byte))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(sbyte))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(uint))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = (int)" + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(char))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
-                    sb.Append("                        ___dst->Value = (int)" + paramName);
-                    sb.AppendLine(";");
-                }
-                else if (type == typeof(ulong))
-                {
-                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Long;");
-                    sb.Append("                        *(ulong*)&___dst->Value = " + paramName);
-                    sb.AppendLine(";");
-                }
-                else
-                    throw new NotImplementedException();
-            }
-            else
-            {
-                if (!type.IsValueType)
-                {
-                    sb.Append(@"                        object ___obj = ");
-                    sb.Append(paramName);
-                    sb.AppendLine(";");
-
-                    sb.AppendLine(@"                        if (___obj is CrossBindingAdaptorType)
-                            ___obj = ((CrossBindingAdaptorType)___obj).ILInstance;
-                        __mStack[___dst->Value] = ___obj; ");
-                }
-                else
-                {
-                    sb.Append("                        __mStack[___dst->Value] = ");
-                    sb.Append(paramName);
-                    sb.AppendLine(";");
-                }
-            }
-        }
-
-        static void GetReturnValueCode(Type type, StringBuilder sb)
-        {
-            if (type.IsPrimitive)
-            {
-                if (type == typeof(int))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(long))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Long;");
-                    sb.AppendLine("            *(long*)&__ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(short))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(bool))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method ? 1 : 0;");
-                }
-                else if (type == typeof(ushort))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(float))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Float;");
-                    sb.AppendLine("            *(float*)&__ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(double))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Double;");
-                    sb.AppendLine("            *(double*)&__ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(byte))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(sbyte))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = result_of_this_method;");
-                }
-                else if (type == typeof(uint))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = (int)result_of_this_method;");
-                }
-                else if (type == typeof(char))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
-                    sb.AppendLine("            __ret->Value = (int)result_of_this_method;");
-                }
-                else if (type == typeof(ulong))
-                {
-                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Long;");
-                    sb.AppendLine("            *(ulong*)&__ret->Value = result_of_this_method;");
-                }
-                else
-                    throw new NotImplementedException();
-                sb.AppendLine("            return __ret + 1;");
-
-            }
-            else
-            {
-                if (!type.IsSealed && type != typeof(ILRuntime.Runtime.Intepreter.ILTypeInstance))
-                {
-                    sb.AppendLine(@"            object obj_result_of_this_method = result_of_this_method;
-            if(obj_result_of_this_method is CrossBindingAdaptorType)
-            {    
-                return ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance);
-            }");
-                }
-                sb.AppendLine("            return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);");
-            }
-        }
-
-        static string GetRetrieveValueCode(Type type, string realClsName)
-        {
-            if (type.IsByRef)
-                type = type.GetElementType();
-            if (type.IsPrimitive)
-            {
-                if (type == typeof(int))
-                {
-                    return "ptr_of_this_method->Value";
-                }
-                else if (type == typeof(long))
-                {
-                    return "*(long*)&ptr_of_this_method->Value";
-                }
-                else if (type == typeof(short))
-                {
-                    return "(short)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(bool))
-                {
-                    return "ptr_of_this_method->Value == 1";
-                }
-                else if (type == typeof(ushort))
-                {
-                    return "(ushort)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(float))
-                {
-                    return "*(float*)&ptr_of_this_method->Value";
-                }
-                else if (type == typeof(double))
-                {
-                    return "*(double*)&ptr_of_this_method->Value";
-                }
-                else if (type == typeof(byte))
-                {
-                    return "(byte)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(sbyte))
-                {
-                    return "(sbyte)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(uint))
-                {
-                    return "(uint)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(char))
-                {
-                    return "(char)ptr_of_this_method->Value";
-                }
-                else if (type == typeof(ulong))
-                {
-                    return "*(ulong*)&ptr_of_this_method->Value";
-                }
-                else
-                    throw new NotImplementedException();
-            }
-            else
-            {
-                return string.Format("({0})typeof({0}).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack))", realClsName);
-            }
+            return info;
         }
     }
 }

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingCodeGenerator.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 4e7ba7f4b3e5e4843a17ebdf3ff249ef
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 335 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingGeneratorExtensions.cs

@@ -0,0 +1,335 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class BindingGeneratorExtensions
+    {
+        internal static bool ShouldSkipField(this Type type, FieldInfo i)
+        {
+            if (i.IsPrivate)
+                return true;
+            //EventHandler is currently not supported
+            if (i.IsSpecialName)
+            {
+                return true;
+            }
+            if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
+                return true;
+            return false;
+        }
+
+        internal static bool ShouldSkipMethod(this Type type, MethodBase i)
+        {
+            if (i.IsPrivate)
+                return true;
+            if (i.IsGenericMethod)
+                return true;
+            //EventHandler is currently not supported
+            var param = i.GetParameters();
+            if (i.IsSpecialName)
+            {
+                string[] t = i.Name.Split('_');
+                if (t[0] == "add" || t[0] == "remove")
+                    return true;
+                if (t[0] == "get" || t[0] == "set")
+                {
+                    Type[] ts;
+                    if (t[1] == "Item")
+                    {
+                        var cnt = t[0] == "set" ? param.Length - 1 : param.Length;
+                        ts = new Type[cnt];
+                        for (int j = 0; j < cnt; j++)
+                        {
+                            ts[j] = param[j].ParameterType;
+                        }
+                    }
+                    else
+                        ts = new Type[0];
+                    var prop = type.GetProperty(t[1], ts);
+                    if (prop == null)
+                    {
+                        return true;
+                    }
+                    if (prop.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
+                        return true;
+                }
+            }
+            if (i.GetCustomAttributes(typeof(ObsoleteAttribute), true).Length > 0)
+                return true;
+            foreach (var j in param)
+            {
+                if (j.ParameterType.IsPointer)
+                    return true;
+            }
+            return false;
+        }
+
+        internal static void AppendParameters(this ParameterInfo[] param, StringBuilder sb)
+        {
+            bool first = true;
+            foreach (var j in param)
+            {
+                if (first)
+                    first = false;
+                else
+                    sb.Append(", ");
+                if (j.IsOut && j.ParameterType.IsByRef)
+                    sb.Append("out ");
+                else if (j.ParameterType.IsByRef)
+                    sb.Append("ref ");
+                sb.Append(j.Name);
+            }
+        }
+
+        internal static string GetRetrieveValueCode(this Type type, string realClsName)
+        {
+            if (type.IsByRef)
+                type = type.GetElementType();
+            if (type.IsPrimitive)
+            {
+                if (type == typeof(int))
+                {
+                    return "ptr_of_this_method->Value";
+                }
+                else if (type == typeof(long))
+                {
+                    return "*(long*)&ptr_of_this_method->Value";
+                }
+                else if (type == typeof(short))
+                {
+                    return "(short)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(bool))
+                {
+                    return "ptr_of_this_method->Value == 1";
+                }
+                else if (type == typeof(ushort))
+                {
+                    return "(ushort)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(float))
+                {
+                    return "*(float*)&ptr_of_this_method->Value";
+                }
+                else if (type == typeof(double))
+                {
+                    return "*(double*)&ptr_of_this_method->Value";
+                }
+                else if (type == typeof(byte))
+                {
+                    return "(byte)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(sbyte))
+                {
+                    return "(sbyte)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(uint))
+                {
+                    return "(uint)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(char))
+                {
+                    return "(char)ptr_of_this_method->Value";
+                }
+                else if (type == typeof(ulong))
+                {
+                    return "*(ulong*)&ptr_of_this_method->Value";
+                }
+                else
+                    throw new NotImplementedException();
+            }
+            else
+            {
+                return string.Format("({0})typeof({0}).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack))", realClsName);
+            }
+        }
+
+        internal static void GetRefWriteBackValueCode(this Type type, StringBuilder sb, string paramName)
+        {
+            if (type.IsPrimitive)
+            {
+                if (type == typeof(int))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(long))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Long;");
+                    sb.Append("                        *(long*)&___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(short))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(bool))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName + " ? 1 : 0;");
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(ushort))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(float))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Float;");
+                    sb.Append("                        *(float*)&___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(double))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Double;");
+                    sb.Append("                        *(double*)&___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(byte))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(sbyte))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(uint))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = (int)" + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(char))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Integer;");
+                    sb.Append("                        ___dst->Value = (int)" + paramName);
+                    sb.AppendLine(";");
+                }
+                else if (type == typeof(ulong))
+                {
+                    sb.AppendLine("                        ___dst->ObjectType = ObjectTypes.Long;");
+                    sb.Append("                        *(ulong*)&___dst->Value = " + paramName);
+                    sb.AppendLine(";");
+                }
+                else
+                    throw new NotImplementedException();
+            }
+            else
+            {
+                if (!type.IsValueType)
+                {
+                    sb.Append(@"                        object ___obj = ");
+                    sb.Append(paramName);
+                    sb.AppendLine(";");
+
+                    sb.AppendLine(@"                        if (___obj is CrossBindingAdaptorType)
+                            ___obj = ((CrossBindingAdaptorType)___obj).ILInstance;
+                        __mStack[___dst->Value] = ___obj; ");
+                }
+                else
+                {
+                    sb.Append("                        __mStack[___dst->Value] = ");
+                    sb.Append(paramName);
+                    sb.AppendLine(";");
+                }
+            }
+        }
+
+        internal static void GetReturnValueCode(this Type type, StringBuilder sb)
+        {
+            if (type.IsPrimitive)
+            {
+                if (type == typeof(int))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(long))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Long;");
+                    sb.AppendLine("            *(long*)&__ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(short))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(bool))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method ? 1 : 0;");
+                }
+                else if (type == typeof(ushort))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(float))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Float;");
+                    sb.AppendLine("            *(float*)&__ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(double))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Double;");
+                    sb.AppendLine("            *(double*)&__ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(byte))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(sbyte))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = result_of_this_method;");
+                }
+                else if (type == typeof(uint))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = (int)result_of_this_method;");
+                }
+                else if (type == typeof(char))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Integer;");
+                    sb.AppendLine("            __ret->Value = (int)result_of_this_method;");
+                }
+                else if (type == typeof(ulong))
+                {
+                    sb.AppendLine("            __ret->ObjectType = ObjectTypes.Long;");
+                    sb.AppendLine("            *(ulong*)&__ret->Value = result_of_this_method;");
+                }
+                else
+                    throw new NotImplementedException();
+                sb.AppendLine("            return __ret + 1;");
+
+            }
+            else
+            {
+                if (!type.IsSealed && type != typeof(ILRuntime.Runtime.Intepreter.ILTypeInstance))
+                {
+                    sb.AppendLine(@"            object obj_result_of_this_method = result_of_this_method;
+            if(obj_result_of_this_method is CrossBindingAdaptorType)
+            {    
+                return ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance);
+            }");
+                }
+                sb.AppendLine("            return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);");
+            }
+        }
+    }
+}

+ 2 - 2
Unity/Assets/PSD2UGUI/Effects/Collections/Pool.cs.meta → Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/BindingGeneratorExtensions.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
-guid: 7bf865b41b11e2146ae2b0bf6a0f8596
-timeCreated: 1487670263
+guid: 3e91768ea7d0ed240bd26b8251c32ee8
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 183 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/CommonBindingGenerator.cs

@@ -0,0 +1,183 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class CommonBindingGenerator
+    {
+        internal static string GenerateMiscRegisterCode(this Type type, string typeClsName, bool defaultCtor, bool newArr)
+        {
+            StringBuilder sb = new StringBuilder();
+
+            if (defaultCtor && !type.IsPrimitive && !type.IsAbstract)
+            {
+                var constructorFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
+                var hasDefaultConstructor = type.GetConstructor(constructorFlags, null, new Type[0], null) != null;
+
+                if (hasDefaultConstructor || type.IsValueType)
+                {
+                    sb.AppendLine(string.Format("            app.RegisterCLRCreateDefaultInstance(type, () => new {0}());", typeClsName));
+                }
+            }
+
+            if (newArr)
+            {
+                if (!type.IsAbstract || !type.IsSealed)
+                {
+                    sb.AppendLine(string.Format("            app.RegisterCLRCreateArrayInstance(type, s => new {0}[s]);", typeClsName));
+                }
+            }
+
+            return sb.ToString();
+        }
+        internal static string GenerateCommonCode(this Type type, string typeClsName)
+        {
+            if (!type.IsValueType)
+                return "";
+            StringBuilder sb = new StringBuilder();
+            if (type.IsPrimitive)
+            {
+                sb.AppendLine(string.Format("        static {0} GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack)", typeClsName));
+                sb.AppendLine("        {");
+                if (type.IsPrimitive || type.IsValueType)
+                    sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
+                sb.AppendLine(string.Format("            {0} instance_of_this_method;", typeClsName));
+                sb.Append(@"            switch(ptr_of_this_method->ObjectType)
+            {
+                case ObjectTypes.FieldReference:
+                    {
+                        var instance_of_fieldReference = __mStack[ptr_of_this_method->Value];
+                        if(instance_of_fieldReference is ILTypeInstance)
+                        {
+                            instance_of_this_method = (");
+                sb.Append(typeClsName);
+                sb.Append(")");
+                if(type == typeof(bool))
+                {
+                    sb.Append("((int)");
+                }
+                sb.Append("((ILTypeInstance)instance_of_fieldReference)[ptr_of_this_method->ValueLow]");
+                if (type == typeof(bool))
+                {
+                    sb.Append(" == 1);");
+                }
+                else
+                    sb.Append(";");
+                sb.Append(@"
+                        }
+                        else
+                        {
+                            var t = __domain.GetType(instance_of_fieldReference.GetType()) as CLRType;
+                            instance_of_this_method = (");
+                sb.Append(typeClsName);
+                sb.Append(")t.GetFieldValue(ptr_of_this_method->ValueLow, instance_of_fieldReference);");
+                sb.Append(@"
+                        }
+                    }
+                    break;
+                case ObjectTypes.StaticFieldReference:
+                    {
+                        var t = __domain.GetType(ptr_of_this_method->Value);
+                        if(t is ILType)
+                        {
+                            instance_of_this_method = (");
+                sb.Append(typeClsName);
+                sb.Append(")");
+                if (type == typeof(bool))
+                {
+                    sb.Append("((int)");
+                }
+                sb.Append("((ILType)t).StaticInstance[ptr_of_this_method->ValueLow]");
+                if (type == typeof(bool))
+                {
+                    sb.Append(" == 1);");
+                }
+                else
+                    sb.Append(";");
+                sb.Append(@"
+                        }
+                        else
+                        {
+                            instance_of_this_method = (");
+                sb.Append(typeClsName);
+                sb.Append(@")((CLRType)t).GetFieldValue(ptr_of_this_method->ValueLow, null);
+                        }
+                    }
+                    break;
+                case ObjectTypes.ArrayReference:
+                    {
+                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
+                sb.Append(typeClsName);
+                sb.AppendLine(@"[];
+                        instance_of_this_method = instance_of_arrayReference[ptr_of_this_method->ValueLow];                        
+                    }
+                    break;
+                default:");
+                sb.AppendLine(string.Format("                    instance_of_this_method = {0};", type.GetRetrieveValueCode(typeClsName)));
+                sb.AppendLine(@"                    break;
+            }
+            return instance_of_this_method;");
+                sb.AppendLine("        }");
+            }
+            if (!type.IsPrimitive && !type.IsAbstract)
+            {
+                sb.AppendLine(string.Format("        static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack, ref {0} instance_of_this_method)", typeClsName));
+                sb.AppendLine("        {");
+                sb.AppendLine(@"            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
+            switch(ptr_of_this_method->ObjectType)
+            {
+                case ObjectTypes.Object:
+                    {
+                        __mStack[ptr_of_this_method->Value] = instance_of_this_method;");
+                sb.Append(@"                    }
+                    break;
+                case ObjectTypes.FieldReference:
+                    {
+                        var ___obj = __mStack[ptr_of_this_method->Value];
+                        if(___obj is ILTypeInstance)
+                        {
+                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = instance_of_this_method");
+                sb.Append(@";
+                        }
+                        else
+                        {
+                            var t = __domain.GetType(___obj.GetType()) as CLRType;
+                            t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, instance_of_this_method");
+                sb.Append(@");
+                        }
+                    }
+                    break;
+                case ObjectTypes.StaticFieldReference:
+                    {
+                        var t = __domain.GetType(ptr_of_this_method->Value);
+                        if(t is ILType)
+                        {
+                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = instance_of_this_method");
+                sb.Append(@";
+                        }
+                        else
+                        {
+                            ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, instance_of_this_method");
+                sb.Append(@");
+                        }
+                    }
+                    break;
+                 case ObjectTypes.ArrayReference:
+                    {
+                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
+                sb.Append(typeClsName);
+                sb.AppendLine(@"[];
+                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = instance_of_this_method;
+                    }
+                    break;
+            }");
+                sb.AppendLine(@"        }");
+            }
+            return sb.ToString();
+        }
+    }
+}

+ 12 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/CommonBindingGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: d2c990d8ba791ea41bdf7f8a58a4a713
+timeCreated: 1503890052
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 180 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ConstructorBindingGenerator.cs

@@ -0,0 +1,180 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class ConstructorBindingGenerator
+    {
+        internal static string GenerateConstructorRegisterCode(this Type type, ConstructorInfo[] methods, HashSet<MethodBase> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+            int idx = 0;
+            foreach (var i in methods)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipMethod(i))
+                    continue;
+                var param = i.GetParameters();
+                StringBuilder sb2 = new StringBuilder();
+                sb2.Append("{");
+                bool first = true;
+                foreach (var j in param)
+                {
+                    if (first)
+                        first = false;
+                    else
+                        sb2.Append(", ");
+                    sb2.Append("typeof(");
+                    string tmp, clsName;
+                    bool isByRef;
+                    j.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
+                    sb2.Append(clsName);
+                    sb2.Append(")");
+                    if (isByRef)
+                        sb2.Append(".MakeByRefType()");
+                }
+                sb2.Append("}");
+                sb.AppendLine(string.Format("            args = new Type[]{0};", sb2));
+                sb.AppendLine("            method = type.GetConstructor(flag, null, args, null);");
+                sb.AppendLine(string.Format("            app.RegisterCLRMethodRedirection(method, Ctor_{0});",idx));
+
+                idx++;
+            }
+            return sb.ToString();
+        }
+
+        internal static string GenerateConstructorWraperCode(this Type type, ConstructorInfo[] methods, string typeClsName, HashSet<MethodBase> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+
+            int idx = 0;
+            foreach (var i in methods)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipMethod(i) || i.IsStatic)
+                    continue;
+                var param = i.GetParameters();
+                int paramCnt = param.Length;
+                sb.AppendLine(string.Format("        static StackObject* Ctor_{0}(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)", idx));
+                sb.AppendLine("        {");
+                sb.AppendLine("            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
+                sb.AppendLine("            StackObject* ptr_of_this_method;");
+                sb.AppendLine(string.Format("            StackObject* __ret = ILIntepreter.Minus(__esp, {0});", paramCnt));
+                for (int j = param.Length; j > 0; j--)
+                {
+                    var p = param[j - 1];
+                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
+                    string tmp, clsName;
+                    bool isByRef;
+                    p.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
+                    if (isByRef)
+                        sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
+                    sb.AppendLine(string.Format("            {0} {1} = {2};", clsName, p.Name, p.ParameterType.GetRetrieveValueCode(clsName)));
+                    if (!isByRef && !p.ParameterType.IsPrimitive)
+                        sb.AppendLine("            __intp.Free(ptr_of_this_method);");
+                }
+                sb.AppendLine();
+                sb.Append("            var result_of_this_method = ");
+                {
+                    string tmp, clsName;
+                    bool isByRef;
+                    type.GetClassName(out tmp, out clsName, out isByRef);
+                    sb.Append(string.Format("new {0}(", clsName));
+                    param.AppendParameters(sb);
+                    sb.AppendLine(");");
+
+                }
+                sb.AppendLine();
+                if (type.IsValueType)
+                {
+                    sb.AppendLine(@"            if(!isNewObj)
+            {
+                __ret--;
+                WriteBackInstance(__domain, __ret, __mStack, ref result_of_this_method);
+                return __ret;
+            }");
+                }
+
+                //Ref/Out
+                for (int j = param.Length; j > 0; j--)
+                {
+                    var p = param[j - 1];
+                    if (!p.ParameterType.IsByRef)
+                        continue;
+                    string tmp, clsName;
+                    bool isByRef;
+                    p.ParameterType.GetElementType().GetClassName(out tmp, out clsName, out isByRef);
+                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
+                    sb.AppendLine(@"            switch(ptr_of_this_method->ObjectType)
+            {
+                case ObjectTypes.StackObjectReference:
+                    {
+                        var ___dst = *(StackObject**)&ptr_of_this_method->Value;");
+                    p.ParameterType.GetElementType().GetRefWriteBackValueCode(sb, p.Name);
+                    sb.Append(@"                    }
+                    break;
+                case ObjectTypes.FieldReference:
+                    {
+                        var ___obj = __mStack[ptr_of_this_method->Value];
+                        if(___obj is ILTypeInstance)
+                        {
+                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.Append(@";
+                        }
+                        else
+                        {
+                            var t = __domain.GetType(___obj.GetType()) as CLRType;
+                            t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, ");
+                    sb.Append(p.Name);
+                    sb.Append(@");
+                        }
+                    }
+                    break;
+                case ObjectTypes.StaticFieldReference:
+                    {
+                        var t = __domain.GetType(ptr_of_this_method->Value);
+                        if(t is ILType)
+                        {
+                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.Append(@";
+                        }
+                        else
+                        {
+                            ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, ");
+                    sb.Append(p.Name);
+                    sb.Append(@");
+                        }
+                    }
+                    break;
+                 case ObjectTypes.ArrayReference:
+                    {
+                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
+                    sb.Append(clsName);
+                    sb.Append(@"[];
+                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.AppendLine(@";
+                    }
+                    break;
+            }");
+                    sb.AppendLine();
+                }
+                sb.AppendLine("            return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);");
+
+                sb.AppendLine("        }");
+                sb.AppendLine();
+                idx++;
+            }
+
+            return sb.ToString();
+        }
+    }
+}

+ 12 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ConstructorBindingGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 531c09b4001f4574dbe7f989aef71c18
+timeCreated: 1503890050
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 114 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/FieldBindingGenerator.cs

@@ -0,0 +1,114 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class FieldBindingGenerator
+    {
+        internal static string GenerateFieldRegisterCode(this Type type, FieldInfo[] fields, HashSet<FieldInfo> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+            int idx = 0;
+            foreach (var i in fields)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipField(i))
+                    continue;
+                if (i.IsSpecialName)
+                    continue;
+
+                sb.AppendLine(string.Format("            field = type.GetField(\"{0}\", flag);", i.Name));
+                sb.AppendLine(string.Format("            app.RegisterCLRFieldGetter(field, get_{0}_{1});", i.Name, idx));
+                if (!i.IsInitOnly && !i.IsLiteral)
+                {
+                    sb.AppendLine(string.Format("            app.RegisterCLRFieldSetter(field, set_{0}_{1});", i.Name, idx));
+                }
+
+                idx++;
+            }
+            return sb.ToString();
+        }
+
+        internal static string GenerateFieldWraperCode(this Type type, FieldInfo[] fields, string typeClsName, HashSet<FieldInfo> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+
+            int idx = 0;
+            foreach (var i in fields)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipField(i))
+                    continue;
+                sb.AppendLine(string.Format("        static object get_{0}_{1}(ref object o)", i.Name, idx));
+                sb.AppendLine("        {");
+                if (i.IsStatic)
+                {
+                    sb.AppendLine(string.Format("            return {0}.{1};", typeClsName, i.Name));
+                }
+                else
+                {
+                    sb.AppendLine(string.Format("            return (({0})o).{1};", typeClsName, i.Name));
+                }
+                sb.AppendLine("        }");
+
+                if (!i.IsInitOnly && !i.IsLiteral)
+                {
+                    sb.AppendLine(string.Format("        static void set_{0}_{1}(ref object o, object v)", i.Name, idx));
+                    sb.AppendLine("        {");
+                    string clsName, realClsName;
+                    bool isByRef;
+                    i.FieldType.GetClassName(out clsName, out realClsName, out isByRef);
+                    if (i.IsStatic)
+                    {
+                        sb.AppendLine(string.Format("            {0}.{1} = ({2})v;", typeClsName, i.Name, realClsName));
+                    }
+                    else
+                    {
+                        if (CheckCanPinn(type))
+                        {
+                            sb.AppendLine("            var h = GCHandle.Alloc(o, GCHandleType.Pinned);");
+                            sb.AppendLine(string.Format("            {0}* p = ({0} *)(void *)h.AddrOfPinnedObject();", typeClsName));
+                            sb.AppendLine(string.Format("            p->{0} = ({1})v;", i.Name, realClsName));
+                            sb.AppendLine("            h.Free();");
+                        }
+                        else
+                        {
+                            sb.AppendLine(string.Format("            (({0})o).{1} = ({2})v;", typeClsName, i.Name, realClsName));
+                        }
+                    }
+                    sb.AppendLine("        }");
+                }
+                idx++;
+            }
+
+            return sb.ToString();
+        }
+
+        internal static bool CheckCanPinn(this Type type)
+        {
+            if (type.IsValueType)
+            {
+                FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
+                bool res = true;
+                foreach(var i in fi)
+                {
+                    if(!i.FieldType.IsPrimitive)
+                    {
+                        res = false;
+                        break;
+                    }
+                }
+
+                return res;
+            }
+            else
+                return false;
+        }
+    }
+}

+ 12 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/FieldBindingGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 9c0373a996c429f46ac5a2a2711f1793
+timeCreated: 1503890051
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 320 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/MethodBindingGenerator.cs

@@ -0,0 +1,320 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class MethodBindingGenerator
+    {
+        internal static string GenerateMethodRegisterCode(this Type type, MethodInfo[] methods, HashSet<MethodBase> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+            int idx = 0;
+            foreach (var i in methods)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipMethod(i))
+                    continue;
+                bool isProperty = i.IsSpecialName;
+                var param = i.GetParameters();
+                StringBuilder sb2 = new StringBuilder();
+                sb2.Append("{");
+                bool first = true;
+                foreach (var j in param)
+                {
+                    if (first)
+                        first = false;
+                    else
+                        sb2.Append(", ");
+                    sb2.Append("typeof(");
+                    string tmp, clsName;
+                    bool isByRef;
+                    j.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
+                    sb2.Append(clsName);
+                    sb2.Append(")");
+                    if (isByRef)
+                        sb2.Append(".MakeByRefType()");
+                }
+                sb2.Append("}");
+                sb.AppendLine(string.Format("            args = new Type[]{0};", sb2));
+                sb.AppendLine(string.Format("            method = type.GetMethod(\"{0}\", flag, null, args, null);", i.Name));
+                sb.AppendLine(string.Format("            app.RegisterCLRMethodRedirection(method, {0}_{1});", i.Name, idx));
+
+                idx++;
+            }
+            return sb.ToString();
+        }
+
+        internal static string GenerateMethodWraperCode(this Type type, MethodInfo[] methods, string typeClsName, HashSet<MethodBase> excludes)
+        {
+            StringBuilder sb = new StringBuilder();
+
+            int idx = 0;
+            foreach (var i in methods)
+            {
+                if (excludes != null && excludes.Contains(i))
+                    continue;
+                if (type.ShouldSkipMethod(i))
+                    continue;
+                bool isProperty = i.IsSpecialName;
+                var param = i.GetParameters();
+                int paramCnt = param.Length;
+                if (!i.IsStatic)
+                    paramCnt++;
+                sb.AppendLine(string.Format("        static StackObject* {0}_{1}(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)", i.Name, idx));
+                sb.AppendLine("        {");
+                sb.AppendLine("            ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;");
+                sb.AppendLine("            StackObject* ptr_of_this_method;");
+                sb.AppendLine(string.Format("            StackObject* __ret = ILIntepreter.Minus(__esp, {0});", paramCnt));
+                for (int j = param.Length; j > 0; j--)
+                {
+                    var p = param[j - 1];
+                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
+                    string tmp, clsName;
+                    bool isByRef;
+                    p.ParameterType.GetClassName(out tmp, out clsName, out isByRef);
+                    if (isByRef)
+                        sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
+                    sb.AppendLine(string.Format("            {0} {1} = {2};", clsName, p.Name, p.ParameterType.GetRetrieveValueCode(clsName)));
+                    if (!isByRef && !p.ParameterType.IsPrimitive)
+                        sb.AppendLine("            __intp.Free(ptr_of_this_method);");
+                }
+                if (!i.IsStatic)
+                {
+                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", paramCnt));
+                    if (type.IsPrimitive)
+                        sb.AppendLine(string.Format("            {0} instance_of_this_method = GetInstance(__domain, ptr_of_this_method, __mStack);", typeClsName));
+                    else
+                    {
+                        if (type.IsValueType)
+                            sb.AppendLine("            ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);");
+                        sb.AppendLine(string.Format("            {0} instance_of_this_method;", typeClsName));
+                        sb.AppendLine(string.Format("            instance_of_this_method = {0};", type.GetRetrieveValueCode(typeClsName)));
+                        if (!type.IsValueType)
+                            sb.AppendLine("            __intp.Free(ptr_of_this_method);");
+                    }
+                }
+                sb.AppendLine();
+                if (i.ReturnType != typeof(void))
+                {
+                    sb.Append("            var result_of_this_method = ");
+                }
+                else
+                    sb.Append("            ");
+                if (i.IsStatic)
+                {
+                    if (isProperty)
+                    {
+                        string[] t = i.Name.Split('_');
+                        string propType = t[0];
+
+                        if (propType == "get")
+                        {
+                            bool isIndexer = param.Length > 0;
+                            if (isIndexer)
+                            {
+                                sb.AppendLine(string.Format("{1}[{0}];", param[0].Name, typeClsName));
+                            }
+                            else
+                                sb.AppendLine(string.Format("{1}.{0};", t[1], typeClsName));
+                        }
+                        else if (propType == "set")
+                        {
+                            bool isIndexer = param.Length > 1;
+                            if (isIndexer)
+                            {
+                                sb.AppendLine(string.Format("{2}[{0}] = {1};", param[0].Name, param[1].Name, typeClsName));
+                            }
+                            else
+                                sb.AppendLine(string.Format("{2}.{0} = {1};", t[1], param[0].Name, typeClsName));
+                        }
+                        else if (propType == "op")
+                        {
+                            switch (t[1])
+                            {
+                                case "Equality":
+                                    sb.AppendLine(string.Format("{0} == {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "Inequality":
+                                    sb.AppendLine(string.Format("{0} != {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "Addition":
+                                    sb.AppendLine(string.Format("{0} + {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "Subtraction":
+                                    sb.AppendLine(string.Format("{0} - {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "Multiply":
+                                    sb.AppendLine(string.Format("{0} * {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "Division":
+                                    sb.AppendLine(string.Format("{0} / {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "GreaterThan":
+                                    sb.AppendLine(string.Format("{0} > {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "GreaterThanOrEqual":
+                                    sb.AppendLine(string.Format("{0} >= {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "LessThan":
+                                    sb.AppendLine(string.Format("{0} < {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "LessThanOrEqual":
+                                    sb.AppendLine(string.Format("{0} <= {1};", param[0].Name, param[1].Name));
+                                    break;
+                                case "UnaryNegation":
+                                    sb.AppendLine(string.Format("-{0};", param[0].Name));
+                                    break;
+                                case "Implicit":
+                                case "Explicit":
+                                    {
+                                        string tmp, clsName;
+                                        bool isByRef;
+                                        i.ReturnType.GetClassName(out tmp, out clsName, out isByRef);
+                                        sb.AppendLine(string.Format("({1}){0};", param[0].Name, clsName));
+                                    }
+                                    break;
+                                default:
+                                    throw new NotImplementedException(i.Name);
+                            }
+                        }
+                        else
+                            throw new NotImplementedException();
+                    }
+                    else
+                    {
+                        sb.Append(string.Format("{0}.{1}(", typeClsName, i.Name));
+                        param.AppendParameters(sb);
+                        sb.AppendLine(");");
+                    }
+                }
+                else
+                {
+                    if (isProperty)
+                    {
+                        string[] t = i.Name.Split('_');
+                        string propType = t[0];
+
+                        if (propType == "get")
+                        {
+                            bool isIndexer = param.Length > 0;
+                            if (isIndexer)
+                            {
+                                sb.AppendLine(string.Format("instance_of_this_method[{0}];", param[0].Name));
+                            }
+                            else
+                                sb.AppendLine(string.Format("instance_of_this_method.{0};", t[1]));
+                        }
+                        else if (propType == "set")
+                        {
+                            bool isIndexer = param.Length > 1;
+                            if (isIndexer)
+                            {
+                                sb.AppendLine(string.Format("instance_of_this_method[{0}] = {1};", param[0].Name, param[1].Name));
+                            }
+                            else
+                                sb.AppendLine(string.Format("instance_of_this_method.{0} = {1};", t[1], param[0].Name));
+                        }
+                        else
+                            throw new NotImplementedException();
+                    }
+                    else
+                    {
+                        sb.Append(string.Format("instance_of_this_method.{0}(", i.Name));
+                        param.AppendParameters(sb);
+                        sb.AppendLine(");");
+                    }
+                }
+                sb.AppendLine();
+
+
+                if (!i.IsStatic && type.IsValueType && !type.IsPrimitive)//need to write back value type instance
+                {
+                    sb.AppendLine("            WriteBackInstance(__domain, ptr_of_this_method, __mStack, ref instance_of_this_method);");
+                    sb.AppendLine();
+                }
+                //Ref/Out
+                for (int j = param.Length; j > 0; j--)
+                {
+                    var p = param[j - 1];
+                    if (!p.ParameterType.IsByRef)
+                        continue;
+                    string tmp, clsName;
+                    bool isByRef;
+                    p.ParameterType.GetElementType().GetClassName(out tmp, out clsName, out isByRef);
+                    sb.AppendLine(string.Format("            ptr_of_this_method = ILIntepreter.Minus(__esp, {0});", param.Length - j + 1));
+                    sb.AppendLine(@"            switch(ptr_of_this_method->ObjectType)
+            {
+                case ObjectTypes.StackObjectReference:
+                    {
+                        var ___dst = *(StackObject**)&ptr_of_this_method->Value;");
+                    p.ParameterType.GetElementType().GetRefWriteBackValueCode(sb, p.Name);
+                    sb.Append(@"                    }
+                    break;
+                case ObjectTypes.FieldReference:
+                    {
+                        var ___obj = __mStack[ptr_of_this_method->Value];
+                        if(___obj is ILTypeInstance)
+                        {
+                            ((ILTypeInstance)___obj)[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.Append(@";
+                        }
+                        else
+                        {
+                            var t = __domain.GetType(___obj.GetType()) as CLRType;
+                            t.SetFieldValue(ptr_of_this_method->ValueLow, ref ___obj, ");
+                    sb.Append(p.Name);
+                    sb.Append(@");
+                        }
+                    }
+                    break;
+                case ObjectTypes.StaticFieldReference:
+                    {
+                        var t = __domain.GetType(ptr_of_this_method->Value);
+                        if(t is ILType)
+                        {
+                            ((ILType)t).StaticInstance[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.Append(@";
+                        }
+                        else
+                        {
+                            ((CLRType)t).SetStaticFieldValue(ptr_of_this_method->ValueLow, ");
+                    sb.Append(p.Name);
+                    sb.Append(@");
+                        }
+                    }
+                    break;
+                 case ObjectTypes.ArrayReference:
+                    {
+                        var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as ");
+                    sb.Append(clsName);
+                    sb.Append(@"[];
+                        instance_of_arrayReference[ptr_of_this_method->ValueLow] = ");
+                    sb.Append(p.Name);
+                    sb.AppendLine(@";
+                    }
+                    break;
+            }");
+                    sb.AppendLine();
+                }
+                if (i.ReturnType != typeof(void))
+                {
+                    i.ReturnType.GetReturnValueCode(sb);
+                }
+                else
+                    sb.AppendLine("            return __ret;");
+                sb.AppendLine("        }");
+                sb.AppendLine();
+                idx++;
+            }
+
+            return sb.ToString();
+        }
+    }
+}

+ 12 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/MethodBindingGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 3e7a4b0b251e3c24bbc7fbb93dc9d799
+timeCreated: 1503890050
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 46 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ValueTypeBindingGenerator.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Linq;
+using System.Text;
+using ILRuntime.Runtime.Enviorment;
+
+namespace ILRuntime.Runtime.CLRBinding
+{
+    static class ValueTypeBindingGenerator
+    {
+        internal static string GenerateValueTypeRegisterCode(this Type type, string typeClsName)
+        {
+            StringBuilder sb = new StringBuilder();
+
+            if (type.IsValueType && !type.IsPrimitive && !type.IsEnum)
+            {
+                sb.AppendLine("            app.RegisterCLRMemberwiseClone(type, PerformMemberwiseClone);");
+            }
+            return sb.ToString();
+        }
+
+        internal static string GenerateCloneWraperCode(this Type type, FieldInfo[] fields, string typeClsName)
+        {
+            if (!type.IsValueType || type.IsPrimitive) return string.Empty;
+            StringBuilder sb = new StringBuilder();
+
+            sb.AppendLine("        static object PerformMemberwiseClone(ref object o)");
+            sb.AppendLine("        {");
+            sb.AppendLine(string.Format("            return new {0}", typeClsName));
+            sb.AppendLine("            {");
+
+            foreach (var i in fields)
+            {
+                if (i.IsStatic || i.IsInitOnly || i.IsLiteral) continue;
+                sb.AppendLine(string.Format("                {0} = (({1}) o).{0},", i.Name, typeClsName));
+            }
+
+            sb.AppendLine("            };");
+            sb.AppendLine("        }");
+
+            return sb.ToString();
+
+        }
+    }
+}

+ 12 - 0
Unity/Assets/ILRuntime/ILRuntime/Runtime/CLRBinding/ValueTypeBindingGenerator.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: 668af796108e80c408d8997b279d5728
+timeCreated: 1503890050
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakPointContext.cs

@@ -34,7 +34,7 @@ namespace ILRuntime.Runtime.Debugger
             return null;
         }
 
-        string GetStackObjectValue(StackObject val, List<object> mStack)
+        string GetStackObjectValue(StackObject val, IList<object> mStack)
         {
             string v;
             switch (val.ObjectType)

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakPointContext.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: bfeefe6a2f833e2459c51dec09120b28
-timeCreated: 1496395689
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/BreakpointInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: c1e1d9861d746594485e121a90ef9c38
-timeCreated: 1496395689
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugMessageType.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: a564f69cbf727c844bb66d4ef14055b8
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugService.cs

@@ -451,7 +451,7 @@ namespace ILRuntime.Runtime.Debugger
             return null;
         }
 
-        unsafe bool GetValueExpandable(StackObject* esp, List<object> mStack)
+        unsafe bool GetValueExpandable(StackObject* esp, IList<object> mStack)
         {
             if (esp->ObjectType < ObjectTypes.Object)
                 return false;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugService.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: bca0b6c7c373ab74b96df13b5517171c
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebugSocket.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: e514adc780b8fd94d94c8bedf0cdc91f
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/DebuggerServer/DebuggerServer.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 52306472508bb034f8fd1b68d450ce8e
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSBindBreakpoint.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: aa0ea609114467a41941ebad10e2c081
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSDeleteBreakpoint.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 58263d5697f506248a2a9fe66a69f3b1
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSExecute.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 3a64e6abaa6367e4c86c71f47955ef15
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSResolveVariable.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 04bf766da67e49947812303010492217
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/CSStep.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 58bb0ff22cedb204ba27ae8a3653c925
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCAttachResult.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 4a95d12ad4b5f1e48b98c5dde9dcabf8
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCBindBreakpointResult.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 1a7539c200fc96b439bf2ecc8e06cb98
-timeCreated: 1496395687
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCBreakpointHit.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 22bab0c3783302649afbacc7867f772a
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCModuleLoaded.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 35a24baf8e5f2bd46a4a2a0352dc0bb8
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCResolveVariableResult.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: fd3187a64b8594444922906ec3e35098
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCStepComplete.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 0ed50f6dc3504124992baf704ff5bc3d
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/Protocol/SCThreadStarted.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 16dba5b2425526c41b18b579235f963a
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/StackFrameInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 1990fb3b0c78d1344b541383b5b4f514
-timeCreated: 1496395687
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/StepTypes.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: a9a918bf142c65744901f224ebcabd4d
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Debugger/VariableInfo.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ba46157c0b55fee46b03f075c5c7668e
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 63 - 20
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/AppDomain.cs

@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -13,20 +13,32 @@ using ILRuntime.CLR.Utils;
 using ILRuntime.Runtime.Intepreter;
 using ILRuntime.Runtime.Debugger;
 using ILRuntime.Runtime.Stack;
+using ILRuntime.Other;
 namespace ILRuntime.Runtime.Enviorment
 {
-    public unsafe delegate StackObject* CLRRedirectionDelegate(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj);
+    public unsafe delegate StackObject* CLRRedirectionDelegate(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj);
+    public delegate object CLRFieldGetterDelegate(ref object target);
+    public delegate void CLRFieldSetterDelegate(ref object target, object value);
+    public delegate object CLRMemberwiseCloneDelegate(ref object target);
+    public delegate object CLRCreateDefaultInstanceDelegate();
+    public delegate object CLRCreateArrayInstanceDelegate(int size);
+
     public class AppDomain
     {
         Queue<ILIntepreter> freeIntepreters = new Queue<ILIntepreter>();
         Dictionary<int, ILIntepreter> intepreters = new Dictionary<int, ILIntepreter>();
-        Dictionary<Type, CrossBindingAdaptor> crossAdaptors = new Dictionary<Type, CrossBindingAdaptor>();
-        Dictionary<string, IType> mapType = new Dictionary<string, IType>();
-        Dictionary<Type, IType> clrTypeMapping = new Dictionary<Type, IType>();
-        Dictionary<int, IType> mapTypeToken = new Dictionary<int, IType>();
-        Dictionary<int, IMethod> mapMethod = new Dictionary<int, IMethod>();
-        Dictionary<long, string> mapString = new Dictionary<long, string>();
+        Dictionary<Type, CrossBindingAdaptor> crossAdaptors = new Dictionary<Type, CrossBindingAdaptor>(new ByReferenceKeyComparer<Type>());
+        ThreadSafeDictionary<string, IType> mapType = new ThreadSafeDictionary<string, IType>();
+        Dictionary<Type, IType> clrTypeMapping = new Dictionary<Type, IType>(new ByReferenceKeyComparer<Type>());
+        ThreadSafeDictionary<int, IType> mapTypeToken = new ThreadSafeDictionary<int, IType>();
+        ThreadSafeDictionary<int, IMethod> mapMethod = new ThreadSafeDictionary<int, IMethod>();
+        ThreadSafeDictionary<long, string> mapString = new ThreadSafeDictionary<long, string>();
         Dictionary<System.Reflection.MethodBase, CLRRedirectionDelegate> redirectMap = new Dictionary<System.Reflection.MethodBase, CLRRedirectionDelegate>();
+        Dictionary<System.Reflection.FieldInfo, CLRFieldGetterDelegate> fieldGetterMap = new Dictionary<System.Reflection.FieldInfo, CLRFieldGetterDelegate>();
+        Dictionary<System.Reflection.FieldInfo, CLRFieldSetterDelegate> fieldSetterMap = new Dictionary<System.Reflection.FieldInfo, CLRFieldSetterDelegate>();
+        Dictionary<Type, CLRMemberwiseCloneDelegate> memberwiseCloneMap = new Dictionary<Type, CLRMemberwiseCloneDelegate>(new ByReferenceKeyComparer<Type>());
+        Dictionary<Type, CLRCreateDefaultInstanceDelegate> createDefaultInstanceMap = new Dictionary<Type, CLRCreateDefaultInstanceDelegate>(new ByReferenceKeyComparer<Type>());
+        Dictionary<Type, CLRCreateArrayInstanceDelegate> createArrayInstanceMap = new Dictionary<Type, CLRCreateArrayInstanceDelegate>(new ByReferenceKeyComparer<Type>());
         IType voidType, intType, longType, boolType, floatType, doubleType, objectType;
         DelegateManager dMgr;
         Assembly[] loadedAssemblies;
@@ -118,8 +130,16 @@ namespace ILRuntime.Runtime.Enviorment
         public IType DoubleType { get { return doubleType; } }
         public IType ObjectType { get { return objectType; } }
 
-        public Dictionary<string, IType> LoadedTypes { get { return mapType; } }
+        /// <summary>
+        /// Attention, this property isn't thread safe
+        /// </summary>
+        public Dictionary<string, IType> LoadedTypes { get { return mapType.InnerDictionary; } }
         internal Dictionary<MethodBase, CLRRedirectionDelegate> RedirectMap { get { return redirectMap; } }
+        internal Dictionary<FieldInfo, CLRFieldGetterDelegate> FieldGetterMap { get { return fieldGetterMap; } }
+        internal Dictionary<FieldInfo, CLRFieldSetterDelegate> FieldSetterMap { get { return fieldSetterMap; } }
+        internal Dictionary<Type, CLRMemberwiseCloneDelegate> MemberwiseCloneMap { get { return memberwiseCloneMap; } }
+        internal Dictionary<Type, CLRCreateDefaultInstanceDelegate> CreateDefaultInstanceMap { get { return createDefaultInstanceMap; } }
+        internal Dictionary<Type, CLRCreateArrayInstanceDelegate> CreateArrayInstanceMap { get { return createArrayInstanceMap; } }
         internal Dictionary<Type, CrossBindingAdaptor> CrossBindingAdaptors { get { return crossAdaptors; } }
         public DebugService DebugService { get { return debugService; } }
         internal Dictionary<int, ILIntepreter> Intepreters { get { return intepreters; } }
@@ -387,6 +407,36 @@ namespace ILRuntime.Runtime.Enviorment
                 redirectMap[mi] = func;
         }
 
+        public void RegisterCLRFieldGetter(FieldInfo f, CLRFieldGetterDelegate getter)
+        {
+            if (!fieldGetterMap.ContainsKey(f))
+                fieldGetterMap[f] = getter;
+        }
+
+        public void RegisterCLRFieldSetter(FieldInfo f, CLRFieldSetterDelegate setter)
+        {
+            if (!fieldSetterMap.ContainsKey(f))
+                fieldSetterMap[f] = setter;
+        }
+
+        public void RegisterCLRMemberwiseClone(Type t, CLRMemberwiseCloneDelegate memberwiseClone)
+        {
+            if (!memberwiseCloneMap.ContainsKey(t))
+                memberwiseCloneMap[t] = memberwiseClone;
+        }
+
+        public void RegisterCLRCreateDefaultInstance(Type t, CLRCreateDefaultInstanceDelegate createDefaultInstance)
+        {
+            if (!createDefaultInstanceMap.ContainsKey(t))
+                createDefaultInstanceMap[t] = createDefaultInstance;
+        }
+
+        public void RegisterCLRCreateArrayInstance(Type t, CLRCreateArrayInstanceDelegate createArray)
+        {
+            if (!createArrayInstanceMap.ContainsKey(t))
+                createArrayInstanceMap[t] = createArray;
+        }
+
         /// <summary>
         /// 更近类型名称返回类型
         /// </summary>
@@ -463,6 +513,7 @@ namespace ILRuntime.Runtime.Enviorment
                     mapTypeToken[bt.GetHashCode()] = bt;
                     if (!isByRef)
                     {
+                        mapType[fullname] = bt;
                         return bt;
                     }
                 }
@@ -476,7 +527,10 @@ namespace ILRuntime.Runtime.Enviorment
                     return res;
                 }
                 else
+                {
+                    mapType[fullname] = bt;
                     return bt;
+                }
             }
             else
             {
@@ -1069,17 +1123,6 @@ namespace ILRuntime.Runtime.Enviorment
             return null;
         }
 
-        internal int GetFieldIndex(object token, IType contextType, IMethod contextMethod)
-        {
-            FieldReference f = token as FieldReference;
-            var type = GetType(f.DeclaringType, contextType, contextMethod);
-            if(type != null)
-            {
-                return type.GetFieldIndex(token);
-            }
-            throw new KeyNotFoundException();
-        }
-
         internal long GetStaticFieldIndex(object token, IType contextType, IMethod contextMethod)
         {
             FieldReference f = token as FieldReference;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/AppDomain.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 7e82484243e934c46b52e732ea60be5a
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 13 - 13
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CLRRedirections.cs

@@ -15,7 +15,7 @@ namespace ILRuntime.Runtime.Enviorment
 {
     unsafe static class CLRRedirections
     {
-        public static StackObject* CreateInstance(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public static StackObject* CreateInstance(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             IType[] genericArguments = method.GenericArguments;
             if (genericArguments != null && genericArguments.Length == 1)
@@ -26,7 +26,7 @@ namespace ILRuntime.Runtime.Enviorment
                     return ILIntepreter.PushObject(esp, mStack, ((ILType)t).Instantiate());
                 }
                 else
-                    return ILIntepreter.PushObject(esp, mStack, Activator.CreateInstance(t.TypeForCLR));
+                    return ILIntepreter.PushObject(esp, mStack, ((CLRType)t).CreateDefaultInstance());
             }
             else
                 throw new EntryPointNotFoundException();
@@ -47,7 +47,7 @@ namespace ILRuntime.Runtime.Enviorment
                 throw new EntryPointNotFoundException();
         }*/
 
-        public static StackObject* CreateInstance2(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public static StackObject* CreateInstance2(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             var p = esp - 1;
             var t = mStack[p->Value] as Type;
@@ -81,7 +81,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return null;
         }*/
 
-        public static StackObject* GetType(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public static StackObject* GetType(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             var p = esp - 1;
             AppDomain dommain = intp.AppDomain;
@@ -94,7 +94,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return ILIntepreter.PushNull(p);
         }
 
-        public static StackObject* TypeEquals(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public static StackObject* TypeEquals(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             var ret = ILIntepreter.Minus(esp, 2);
             var p = esp - 1;
@@ -134,7 +134,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return null;
         }*/
 
-        public unsafe static StackObject* InitializeArray(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* InitializeArray(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             var ret = esp - 1 - 1;
             AppDomain domain = intp.AppDomain;
@@ -389,7 +389,7 @@ namespace ILRuntime.Runtime.Enviorment
             return null;
         }*/
 
-        public unsafe static StackObject* DelegateCombine(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* DelegateCombine(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             //Don't ask me why not esp -2, unity won't return the right result
             var ret = esp - 1 - 1;
@@ -496,7 +496,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return dele2;
         }*/
 
-        public unsafe static StackObject* DelegateRemove(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* DelegateRemove(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             //Don't ask me why not esp -2, unity won't return the right result
             var ret = esp - 1 - 1;
@@ -582,7 +582,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return null;
         }*/
 
-        public unsafe static StackObject* DelegateEqulity(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* DelegateEqulity(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             //Don't ask me why not esp -2, unity won't return the right result
             var ret = esp - 1 - 1;
@@ -667,7 +667,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return dele2 == null;
         }*/
 
-        public unsafe static StackObject* DelegateInequlity(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* DelegateInequlity(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             //Don't ask me why not esp -2, unity won't return the right result
             var ret = esp - 1 - 1;
@@ -747,7 +747,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return dele2 != null;
         }*/
 
-        public static StackObject* GetTypeFromHandle(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public static StackObject* GetTypeFromHandle(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             //Nothing to do
             return esp;
@@ -758,7 +758,7 @@ namespace ILRuntime.Runtime.Enviorment
             return param[0];
         }*/
 
-        public unsafe static StackObject* MethodInfoInvoke(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* MethodInfoInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             AppDomain domain = intp.AppDomain;
             //Don't ask me why not esp - 3, unity won't return the right result
@@ -834,7 +834,7 @@ namespace ILRuntime.Runtime.Enviorment
                 return ((MethodInfo)instance).Invoke(obj, (object[])p);
         }*/
 
-        public unsafe static StackObject* ObjectGetType(ILIntepreter intp, StackObject* esp, List<object> mStack, CLRMethod method, bool isNewObj)
+        public unsafe static StackObject* ObjectGetType(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
         {
             AppDomain domain = intp.AppDomain;
             var ret = esp - 1;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CLRRedirections.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: f69628ad8b7de48438cc264443cb5c7c
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CrossBindingAdaptor.cs

@@ -83,7 +83,7 @@ namespace ILRuntime.Runtime.Enviorment
                         return true;
                 }
             }
-            return false;
+            return res;
         }
 
         public IType MakeGenericInstance(KeyValuePair<string, IType>[] genericArguments)

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/CrossBindingAdaptor.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 89b5db64481707c44b294daa538d54df
-timeCreated: 1496471235
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 6 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/DelegateManager.cs

@@ -5,6 +5,7 @@ using System.Text;
 
 using ILRuntime.CLR.TypeSystem;
 using ILRuntime.CLR.Method;
+using ILRuntime.Other;
 using ILRuntime.Runtime.Intepreter;
 
 namespace ILRuntime.Runtime.Enviorment
@@ -15,7 +16,7 @@ namespace ILRuntime.Runtime.Enviorment
         List<DelegateMapNode> functions = new List<DelegateMapNode>();
         IDelegateAdapter zeroParamMethodAdapter = new MethodDelegateAdapter();
         IDelegateAdapter dummyAdapter = new DummyDelegateAdapter();
-        Dictionary<Type, Func<Delegate, Delegate>> clrDelegates = new Dictionary<Type, Func<Delegate, Delegate>>();
+        Dictionary<Type, Func<Delegate, Delegate>> clrDelegates = new Dictionary<Type, Func<Delegate, Delegate>>(new ByReferenceKeyComparer<Type>());
         Func<Delegate, Delegate> defaultConverter;
         Enviorment.AppDomain appdomain;
         public DelegateManager(Enviorment.AppDomain appdomain)
@@ -177,6 +178,10 @@ namespace ILRuntime.Runtime.Enviorment
                         i.ParameterType.GetClassName(out clsName, out rName, out isByRef);
                         sb.Append(rName);
                     }
+                    if (!first)
+                        sb.Append(", ");
+                    mi.ReturnType.GetClassName(out clsName, out rName, out isByRef);
+                    sb.Append(rName);
                 }
                 else
                 {

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/DelegateManager.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ea8e4e6294992d0489b4f3671c3a7b5e
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 0 - 28
Unity/Assets/ILRuntime/ILRuntime/Runtime/Enviorment/ILContext.cs

@@ -1,28 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-using ILRuntime.Runtime.Intepreter;
-using ILRuntime.Runtime.Stack;
-using ILRuntime.CLR.Method;
-namespace ILRuntime.Runtime.Enviorment
-{
-    public unsafe struct ILContext
-    {
-        public AppDomain AppDomain { get; private set; }
-        public StackObject* ESP { get; private set; }
-        public List<object> ManagedStack { get; private set; }
-        public IMethod Method { get; private set; }
-        public ILIntepreter Interpreter { get; private set; }
-
-        internal ILContext(AppDomain domain,ILIntepreter intpreter, StackObject* esp, List<object> mStack, IMethod method)
-        {
-            AppDomain = domain;
-            ESP = esp;
-            ManagedStack = mStack;
-            Method = method;
-            Interpreter = intpreter;
-        }
-    }
-}

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Extensions.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: cd8de1ae799f1ca47a32ba07ea2a5bf8
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 18 - 12
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs

@@ -2,10 +2,11 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-
 using ILRuntime.CLR.TypeSystem;
 using ILRuntime.CLR.Method;
+using ILRuntime.Runtime;
 using ILRuntime.Runtime.Stack;
+using ILRuntime.Other;
 using ILRuntime.Runtime.Enviorment;
 
 namespace ILRuntime.Runtime.Intepreter
@@ -640,14 +641,14 @@ namespace ILRuntime.Runtime.Intepreter
             }
         }
 
-        public unsafe StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, List<object> mStack)
+        public unsafe StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack)
         {
             var ebp = esp;
             esp = ILInvokeSub(intp, esp, mStack);
             return ClearStack(intp, esp, ebp, mStack);
         }
 
-        unsafe StackObject* ILInvokeSub(ILIntepreter intp, StackObject* esp, List<object> mStack)
+        unsafe StackObject* ILInvokeSub(ILIntepreter intp, StackObject* esp, IList<object> mStack)
         {
             var ebp = esp;
             bool unhandled;
@@ -673,7 +674,7 @@ namespace ILRuntime.Runtime.Intepreter
             return ret;
         }
 
-        unsafe StackObject* ClearStack(ILIntepreter intp, StackObject* esp, StackObject* ebp, List<object> mStack)
+        unsafe StackObject* ClearStack(ILIntepreter intp, StackObject* esp, StackObject* ebp, IList<object> mStack)
         {
             int paramCnt = method.ParameterCount;
             object retObj = null;
@@ -769,7 +770,7 @@ namespace ILRuntime.Runtime.Intepreter
         public Delegate GetConvertor(Type type)
         {
             if (converters == null)
-                converters = new Dictionary<System.Type, Delegate>();
+                converters = new Dictionary<System.Type, Delegate>(new ByReferenceKeyComparer<Type>());
             Delegate res;
             if (converters.TryGetValue(type, out res))
                 return res;
@@ -791,6 +792,8 @@ namespace ILRuntime.Runtime.Intepreter
             StringBuilder sb = new StringBuilder();
             sb.Append("Cannot find Delegate Adapter for:");
             sb.Append(method.ToString());
+            string clsName, rName;
+            bool isByRef;
             if (method.ReturnType.Name != "Void" || method.ParameterCount > 0)
             {
                 sb.AppendLine(", Please add following code:");
@@ -807,10 +810,11 @@ namespace ILRuntime.Runtime.Intepreter
                         else
                         {
                             sb.Append(", ");
-                        }                        
-                        sb.Append(i.TypeForCLR.FullName);
+                        }
+                        i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
+                        sb.Append(rName);                        
                     }
-                    sb.AppendLine(">");
+                    sb.AppendLine(">();");
                 }
                 else
                 {
@@ -826,12 +830,14 @@ namespace ILRuntime.Runtime.Intepreter
                         {
                             sb.Append(", ");
                         }
-                        sb.Append(i.TypeForCLR.FullName);
+                        i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
+                        sb.Append(rName);
                     }
                     if (!first)
                         sb.Append(", ");
-                    sb.Append(method.ReturnType.TypeForCLR.FullName);
-                    sb.AppendLine(">");
+                    method.ReturnType.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
+                    sb.Append(rName);
+                    sb.AppendLine(">();");
                 }
             }
             throw new KeyNotFoundException(sb.ToString());
@@ -844,7 +850,7 @@ namespace ILRuntime.Runtime.Intepreter
         IDelegateAdapter Next { get; }
         ILTypeInstance Instance { get; }
         ILMethod Method { get; }
-        StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, List<object> mStack);
+        StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack);
         IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method);
         bool IsClone { get; }
         IDelegateAdapter Clone();

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/DelegateAdapter.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: a6dd5ad6790a01b468a803875deb4a3f
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 159 - 72
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs

@@ -11,6 +11,7 @@ using ILRuntime.CLR.TypeSystem;
 using ILRuntime.Runtime.Intepreter.OpCodes;
 using ILRuntime.Runtime.Debugger;
 using ILRuntime.CLR.Utils;
+using ILRuntime.Other;
 
 namespace ILRuntime.Runtime.Intepreter
 {
@@ -61,7 +62,7 @@ namespace ILRuntime.Runtime.Intepreter
         }
         public object Run(ILMethod method, object instance, object[] p)
         {
-            List<object> mStack = stack.ManagedStack;
+            IList<object> mStack = stack.ManagedStack;
             int mStackBase = mStack.Count;
             StackObject* esp = stack.StackBase;
             if (method.HasThis)
@@ -77,7 +78,11 @@ namespace ILRuntime.Runtime.Intepreter
             esp = Execute(method, esp, out unhandledException);
             object result = method.ReturnType != domain.VoidType ? method.ReturnType.TypeForCLR.CheckCLRTypes(StackObject.ToObject((esp - 1), domain, mStack)) : null;
             //ClearStack
-            mStack.RemoveRange(mStackBase, mStack.Count - mStackBase);
+#if DEBUG
+            ((List<object>)mStack).RemoveRange(mStackBase, mStack.Count - mStackBase);
+#else
+            ((UncheckedList<object>)mStack).RemoveRange(mStackBase, mStack.Count - mStackBase);
+#endif
             return result;
         }
         internal StackObject* Execute(ILMethod method, StackObject* esp, out bool unhandledException)
@@ -105,7 +110,7 @@ namespace ILRuntime.Runtime.Intepreter
 
             esp = frame.BasePointer;
             StackObject* arg = Minus(frame.LocalVarPointer, method.ParameterCount);
-            List<object> mStack = stack.ManagedStack;
+            IList<object> mStack = stack.ManagedStack;
             int paramCnt = method.ParameterCount;
             if (method.HasThis)//this parameter is always object reference
             {
@@ -154,7 +159,7 @@ namespace ILRuntime.Runtime.Intepreter
                     }
                     else
                     {
-                        var obj = Activator.CreateInstance(t.TypeForCLR);
+                        var obj = ((CLRType) t).CreateDefaultInstance();
                         var loc = Add(v1, i);
                         loc->ObjectType = ObjectTypes.Object;
                         loc->Value = mStack.Count;
@@ -454,7 +459,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 else
                                                 {
                                                     var t = AppDomain.GetType(ip->TokenInteger);
-                                                    obj = ((CLRType)t).GetField(idx).GetValue(obj);
+                                                    obj = ((CLRType) t).GetFieldValue(idx, obj);
                                                     PushObject(objRef, mStack, obj);
                                                 }
                                             }
@@ -470,7 +475,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 }
                                                 else
                                                 {
-                                                    var obj = ((CLRType)t).GetField(idx).GetValue(null);
+                                                    var obj = ((CLRType)t).GetFieldValue(idx, null);
                                                     PushObject(objRef, mStack, obj);
                                                 }
                                             }
@@ -517,7 +522,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 else
                                                 {
                                                     var t = AppDomain.GetType(ip->TokenInteger);
-                                                    ((CLRType)t).GetField(idx).SetValue(obj, t.TypeForCLR.CheckCLRTypes(StackObject.ToObject(val, AppDomain, mStack)));
+                                                    ((CLRType)t).SetFieldValue(idx, ref obj, t.TypeForCLR.CheckCLRTypes(StackObject.ToObject(val, AppDomain, mStack)));
                                                 }
                                             }
                                             break;
@@ -530,7 +535,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                 }
                                                 else
                                                 {
-                                                    ((CLRType)t).GetField(objRef->ValueLow).SetValue(null, t.TypeForCLR.CheckCLRTypes(StackObject.ToObject(val, AppDomain, mStack)));
+                                                    ((CLRType)t).SetStaticFieldValue(objRef->ValueLow, t.TypeForCLR.CheckCLRTypes(StackObject.ToObject(val, AppDomain, mStack)));
                                                 }
                                             }
                                             break;
@@ -1279,6 +1284,9 @@ namespace ILRuntime.Runtime.Intepreter
                                     {
                                         switch (a->ObjectType)
                                         {
+                                            case ObjectTypes.Null:
+                                                transfer = true;
+                                                break;
                                             case ObjectTypes.Integer:
                                                 transfer = a->Value == b->Value;
                                                 break;
@@ -1319,6 +1327,9 @@ namespace ILRuntime.Runtime.Intepreter
                                     {
                                         switch (a->ObjectType)
                                         {
+                                            case ObjectTypes.Null:
+                                                transfer = false;
+                                                break;
                                             case ObjectTypes.Integer:
                                                 transfer = (uint)a->Value != (uint)b->Value;
                                                 break;
@@ -1668,8 +1679,13 @@ namespace ILRuntime.Runtime.Intepreter
                                     if (m == null)
                                     {
                                         //Irrelevant method
-                                        Free(esp - 1);
-                                        esp--;
+                                        int cnt = (int)ip->TokenLong;
+                                        //Balance the stack
+                                        for (int i = 0; i < cnt; i++)
+                                        {
+                                            Free(esp - 1);
+                                            esp--;
+                                        }
                                     }
                                     else
                                     {
@@ -1785,17 +1801,18 @@ namespace ILRuntime.Runtime.Intepreter
                                         {
                                             ILTypeInstance instance = obj as ILTypeInstance;
                                             StackObject* val = esp - 1;
-                                            instance.AssignFromStack(ip->TokenInteger, val, AppDomain, mStack);
+                                            instance.AssignFromStack((int)ip->TokenLong, val, AppDomain, mStack);
                                         }
                                         else
                                         {
                                             var t = obj.GetType();
-                                            var type = AppDomain.GetType(t);
+                                            var type = AppDomain.GetType((int)(ip->TokenLong >> 32));
                                             if (type != null)
                                             {
                                                 var val = esp - 1;
-                                                var f = ((CLRType)type).GetField(ip->TokenInteger);
-                                                f.SetValue(obj, f.FieldType.CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, domain, mStack), domain)));
+                                                var fieldToken = (int)ip->TokenLong;
+                                                var f = ((CLRType)type).GetField(fieldToken);
+                                                ((CLRType)type).SetFieldValue(fieldToken, ref obj, f.FieldType.CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, domain, mStack), domain)));
                                                 //Writeback
                                                 if (t.IsValueType)
                                                 {
@@ -1814,7 +1831,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                                 else
                                                                 {
                                                                     var it = AppDomain.GetType(oldObj.GetType());
-                                                                    ((CLRType)it).GetField(idx).SetValue(oldObj, obj);
+                                                                    ((CLRType)it).SetFieldValue(idx, ref oldObj, obj);
                                                                 }
                                                             }
                                                             break;
@@ -1828,7 +1845,7 @@ namespace ILRuntime.Runtime.Intepreter
                                                                 }
                                                                 else
                                                                 {
-                                                                    ((CLRType)it).GetField(idx).SetValue(null, obj);
+                                                                    ((CLRType)it).SetStaticFieldValue(idx, obj);
                                                                 }
                                                             }
                                                             break;
@@ -1858,16 +1875,17 @@ namespace ILRuntime.Runtime.Intepreter
                                         if (obj is ILTypeInstance)
                                         {
                                             ILTypeInstance instance = obj as ILTypeInstance;
-                                            instance.PushToStack(ip->TokenInteger, esp - 1, AppDomain, mStack);
+                                            instance.PushToStack((int)ip->TokenLong, esp - 1, AppDomain, mStack);
                                         }
                                         else
                                         {
-                                            var t = obj.GetType();
-                                            var type = AppDomain.GetType(t);
+                                            //var t = obj.GetType();
+                                            var type = AppDomain.GetType((int)(ip->TokenLong >> 32));
                                             if (type != null)
                                             {
-                                                var ft = ((CLRType)type).GetField(ip->TokenInteger);
-                                                var val = ft.GetValue(obj);
+                                                var token = (int)ip->TokenLong;
+                                                var ft = ((CLRType)type).GetField(token);
+                                                var val = ((CLRType)type).GetFieldValue(token, obj);
                                                 if (val is CrossBindingAdaptorType)
                                                     val = ((CrossBindingAdaptorType)val).ILInstance;
                                                 PushObject(esp - 1, mStack, val, ft.FieldType == typeof(object));
@@ -1892,7 +1910,7 @@ namespace ILRuntime.Runtime.Intepreter
                                         if (obj is ILTypeInstance)
                                         {
                                             ILTypeInstance instance = obj as ILTypeInstance;
-                                            instance.PushFieldAddress(ip->TokenInteger, esp - 1, mStack);
+                                            instance.PushFieldAddress((int)ip->TokenLong, esp - 1, mStack);
                                         }
                                         else
                                         {
@@ -1900,7 +1918,7 @@ namespace ILRuntime.Runtime.Intepreter
                                             objRef->ObjectType = ObjectTypes.FieldReference;
                                             objRef->Value = mStack.Count;
                                             mStack.Add(obj);
-                                            objRef->ValueLow = ip->TokenInteger;
+                                            objRef->ValueLow = (int)ip->TokenLong;
                                         }
                                     }
                                     else
@@ -1924,7 +1942,7 @@ namespace ILRuntime.Runtime.Intepreter
                                             int idx = (int)ip->TokenLong;
                                             var f = t.GetField(idx);
                                             StackObject* val = esp - 1;
-                                            f.SetValue(null, f.FieldType.CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, domain, mStack), domain)));
+                                            t.SetStaticFieldValue(idx, f.FieldType.CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, domain, mStack), domain)));
                                         }
                                     }
                                     else
@@ -1948,7 +1966,7 @@ namespace ILRuntime.Runtime.Intepreter
                                             CLRType t = type as CLRType;
                                             int idx = (int)ip->TokenLong;
                                             var f = t.GetField(idx);
-                                            var val = f.GetValue(null);
+                                            var val = t.GetFieldValue(idx, null);
                                             if (val is CrossBindingAdaptorType)
                                                 val = ((CrossBindingAdaptorType)val).ILInstance;
                                             PushObject(esp, mStack, val, f.FieldType == typeof(object));
@@ -2846,50 +2864,106 @@ namespace ILRuntime.Runtime.Intepreter
                                     var type = domain.GetType(ip->TokenInteger);
                                     if (type != null)
                                     {
-                                        var obj = RetriveObject(objRef, mStack);
-                                        Free(objRef);
-
-                                        if (obj != null)
+                                        objRef = GetObjectAndResolveReference(objRef);
+                                        if (objRef->ObjectType <= ObjectTypes.Double)
                                         {
-                                            if (obj is ILTypeInstance)
+                                            var tclr = type.TypeForCLR;
+                                            switch (objRef->ObjectType)
                                             {
-                                                if (((ILTypeInstance)obj).CanAssignTo(type))
+                                                case ObjectTypes.Integer:
+                                                    {
+                                                        if (tclr != typeof(int) && tclr != typeof(bool) && tclr != typeof(short) && tclr != typeof(byte) && tclr != typeof(ushort) && tclr !=typeof(uint))
+                                                        {
+                                                            objRef->ObjectType = ObjectTypes.Null;
+                                                            objRef->Value = -1;
+                                                            objRef->ValueLow = 0;
+                                                        }
+                                                    }
+                                                    break;
+                                                case ObjectTypes.Long:
+                                                    {
+                                                        if (tclr != typeof(long) && tclr != typeof(ulong))
+                                                        {
+                                                            objRef->ObjectType = ObjectTypes.Null;
+                                                            objRef->Value = -1;
+                                                            objRef->ValueLow = 0;
+                                                        }
+                                                    }
+                                                    break;
+                                                case ObjectTypes.Float:
+                                                    {
+                                                        if (tclr != typeof(float))
+                                                        {
+                                                            objRef->ObjectType = ObjectTypes.Null;
+                                                            objRef->Value = -1;
+                                                            objRef->ValueLow = 0;
+                                                        }
+                                                    }
+                                                    break;
+                                                case ObjectTypes.Double:
+                                                    {
+                                                        if (tclr != typeof(double))
+                                                        {
+                                                            objRef->ObjectType = ObjectTypes.Null;
+                                                            objRef->Value = -1;
+                                                            objRef->ValueLow = 0;
+                                                        }
+                                                    }
+                                                    break;
+                                                case ObjectTypes.Null:
+                                                    objRef->ObjectType = ObjectTypes.Null;
+                                                    objRef->Value = -1;
+                                                    objRef->ValueLow = 0;
+                                                    break;
+                                            }
+                                        }
+                                        else
+                                        {
+                                            var obj = RetriveObject(objRef, mStack);
+                                            Free(objRef);
+
+                                            if (obj != null)
+                                            {
+                                                if (obj is ILTypeInstance)
                                                 {
-                                                    esp = PushObject(objRef, mStack, obj);
+                                                    if (((ILTypeInstance)obj).CanAssignTo(type))
+                                                    {
+                                                        esp = PushObject(objRef, mStack, obj);
+                                                    }
+                                                    else
+                                                    {
+#if !DEBUG
+                                                        objRef->ObjectType = ObjectTypes.Null;
+                                                        objRef->Value = -1;
+                                                        objRef->ValueLow = 0;
+#endif
+                                                    }
                                                 }
                                                 else
                                                 {
+                                                    if (type.TypeForCLR.IsAssignableFrom(obj.GetType()))
+                                                    {
+                                                        esp = PushObject(objRef, mStack, obj, true);
+                                                    }
+                                                    else
+                                                    {
 #if !DEBUG
-                                                    objRef->ObjectType = ObjectTypes.Null;
-                                                    objRef->Value = -1;
-                                                    objRef->ValueLow = 0;
+                                                        objRef->ObjectType = ObjectTypes.Null;
+                                                        objRef->Value = -1;
+                                                        objRef->ValueLow = 0;
 #endif
+                                                    }
                                                 }
                                             }
                                             else
                                             {
-                                                if (type.TypeForCLR.IsAssignableFrom(obj.GetType()))
-                                                {
-                                                    esp = PushObject(objRef, mStack, obj, true);
-                                                }
-                                                else
-                                                {
 #if !DEBUG
                                                     objRef->ObjectType = ObjectTypes.Null;
                                                     objRef->Value = -1;
                                                     objRef->ValueLow = 0;
 #endif
-                                                }
                                             }
                                         }
-                                        else
-                                        {
-#if !DEBUG
-                                                objRef->ObjectType = ObjectTypes.Null;
-                                                objRef->Value = -1;
-                                                objRef->ValueLow = 0;
-#endif
-                                        }
                                     }
                                     else
                                         throw new NullReferenceException();
@@ -2907,7 +2981,15 @@ namespace ILRuntime.Runtime.Intepreter
                                     {
                                         if (type.TypeForCLR != typeof(ILTypeInstance))
                                         {
-                                            arr = Array.CreateInstance(type.TypeForCLR, cnt->Value);
+                                            if (type is CLRType)
+                                            {
+                                                arr = ((CLRType)type).CreateArrayInstance(cnt->Value);
+                                            }
+                                            else
+                                            {
+                                                arr = Array.CreateInstance(type.TypeForCLR, cnt->Value);
+                                            }
+
                                             //Register Type
                                             AppDomain.GetType(arr.GetType());
                                         }
@@ -3730,7 +3812,7 @@ namespace ILRuntime.Runtime.Intepreter
             return stack.PopFrame(ref frame, esp, mStack);
         }
 
-        object RetriveObject(StackObject* esp, List<object> mStack)
+        object RetriveObject(StackObject* esp, IList<object> mStack)
         {
             StackObject* objRef = GetObjectAndResolveReference(esp);
             if (objRef->ObjectType == ObjectTypes.Null)
@@ -3752,7 +3834,7 @@ namespace ILRuntime.Runtime.Intepreter
                         else
                         {
                             var t = AppDomain.GetType(obj.GetType());
-                            obj = ((CLRType)t).GetField(idx).GetValue(obj);
+                            obj = ((CLRType) t).GetFieldValue(idx, obj);
                         }
                     }
                     break;
@@ -3774,7 +3856,7 @@ namespace ILRuntime.Runtime.Intepreter
                         }
                         else
                         {
-                            obj = ((CLRType)t).GetField(idx).GetValue(null);
+                            obj = ((CLRType)t).GetFieldValue(idx, null);
                         }
                     }
                     break;
@@ -3887,7 +3969,7 @@ namespace ILRuntime.Runtime.Intepreter
             return res;
         }
 
-        void LoadFromFieldReference(object obj, int idx, StackObject* dst, List<object> mStack)
+        void LoadFromFieldReference(object obj, int idx, StackObject* dst, IList<object> mStack)
         {
             if (obj is ILTypeInstance)
             {
@@ -3896,12 +3978,11 @@ namespace ILRuntime.Runtime.Intepreter
             else
             {
                 CLRType t = AppDomain.GetType(obj.GetType()) as CLRType;
-                var fi = t.GetField(idx);
-                PushObject(dst, mStack, fi.GetValue(obj));
+                ILIntepreter.PushObject(dst, mStack, t.GetFieldValue(idx, obj));
             }
         }
 
-        void StoreValueToFieldReference(object obj, int idx, StackObject* val, List<object> mStack)
+        void StoreValueToFieldReference(object obj, int idx, StackObject* val, IList<object> mStack)
         {
             if (obj is ILTypeInstance)
             {
@@ -3910,19 +3991,18 @@ namespace ILRuntime.Runtime.Intepreter
             else
             {
                 CLRType t = AppDomain.GetType(obj.GetType()) as CLRType;
-                var fi = t.GetField(idx);
                 var v = obj.GetType().CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, AppDomain, mStack), AppDomain));
-                fi.SetValue(obj, v);
+                t.SetFieldValue(idx, ref obj, v);
             }
         }
 
-        void LoadFromArrayReference(object obj, int idx, StackObject* objRef, IType t, List<object> mStack)
+        void LoadFromArrayReference(object obj, int idx, StackObject* objRef, IType t, IList<object> mStack)
         {
             var nT = t.TypeForCLR;
             LoadFromArrayReference(obj, idx, objRef, nT, mStack);
         }
 
-        void LoadFromArrayReference(object obj, int idx, StackObject* objRef, Type nT, List<object> mStack)
+        void LoadFromArrayReference(object obj, int idx, StackObject* objRef, Type nT, IList<object> mStack)
         {
             if (nT.IsPrimitive)
             {
@@ -4002,13 +4082,13 @@ namespace ILRuntime.Runtime.Intepreter
             }
         }
 
-        void StoreValueToArrayReference(StackObject* objRef, StackObject* val, IType t, List<object> mStack)
+        void StoreValueToArrayReference(StackObject* objRef, StackObject* val, IType t, IList<object> mStack)
         {
             var nT = t.TypeForCLR;
             StoreValueToArrayReference(objRef, val, nT, mStack);
         }
 
-        void StoreValueToArrayReference(StackObject* objRef, StackObject* val, Type nT, List<object> mStack)
+        void StoreValueToArrayReference(StackObject* objRef, StackObject* val, Type nT, IList<object> mStack)
         {
             if (nT.IsPrimitive)
             {
@@ -4092,7 +4172,7 @@ namespace ILRuntime.Runtime.Intepreter
 
         StackObject* PushParameters(IMethod method, StackObject* esp, object[] p)
         {
-            List<object> mStack = stack.ManagedStack;
+            IList<object> mStack = stack.ManagedStack;
             var plist = method.Parameters;
             int pCnt = plist != null ? plist.Count : 0;
             int pCnt2 = p != null ? p.Length : 0;
@@ -4108,13 +4188,13 @@ namespace ILRuntime.Runtime.Intepreter
                     object obj = p[i];
                     if (obj is CrossBindingAdaptorType)
                         obj = ((CrossBindingAdaptorType)obj).ILInstance;
-                    esp = PushObject(esp, mStack, obj, isBox);
+                    esp = ILIntepreter.PushObject(esp, mStack, obj, isBox);
                 }
             }
             return esp;
         }
 
-        public void CopyToStack(StackObject* dst, StackObject* src, List<object> mStack)
+        public void CopyToStack(StackObject* dst, StackObject* src, IList<object> mStack)
         {
             *dst = *src;
             if (dst->ObjectType >= ObjectTypes.Object)
@@ -4140,10 +4220,15 @@ namespace ILRuntime.Runtime.Intepreter
                 else
                 {
                     var type = obj.GetType();
-                    if (!type.IsPrimitive && type.IsValueType)
+                    var typeFlags = type.GetTypeFlags();
+
+                    var isPrimitive = (typeFlags & CLR.Utils.Extensions.TypeFlags.IsPrimitive) != 0;
+                    var isValueType = (typeFlags & CLR.Utils.Extensions.TypeFlags.IsValueType) != 0;
+
+                    if (!isPrimitive && isValueType)
                     {
                         var t = domain.GetType(type);
-                        return ((CLRType)t).MemberwiseClone.Invoke(obj, null);
+                        return ((CLRType)t).PerformMemberwiseClone(obj);
                     }
                 }
             }
@@ -4237,17 +4322,19 @@ namespace ILRuntime.Runtime.Intepreter
                 throw new NotImplementedException();
         }
 
-        public static StackObject* PushObject(StackObject* esp, List<object> mStack, object obj, bool isBox = false)
+        public static StackObject* PushObject(StackObject* esp, IList<object> mStack, object obj, bool isBox = false)
         {
             if (obj != null)
             {
                 if (!isBox)
                 {
-                    if (obj.GetType().IsPrimitive)
+                    var typeFlags = obj.GetType().GetTypeFlags();
+
+                    if ((typeFlags & CLR.Utils.Extensions.TypeFlags.IsPrimitive) != 0)
                     {
                         UnboxObject(esp, obj);
                     }
-                    else if (obj.GetType().IsEnum)
+                    else if ((typeFlags & CLR.Utils.Extensions.TypeFlags.IsEnum) != 0)
                     {
                         esp->ObjectType = ObjectTypes.Integer;
                         esp->Value = Convert.ToInt32(obj);

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILIntepreter.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: a0bb99f039f2a6f418e5af5bef359bf5
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILRuntimeException.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: ebae8830490e95743a6bf3332b2ee263
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 11 - 16
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs

@@ -103,7 +103,7 @@ namespace ILRuntime.Runtime.Intepreter
     {
         protected ILType type;
         protected StackObject[] fields;
-        protected List<object> managedObjs;
+        protected IList<object> managedObjs;
         object clrInstance;
         Dictionary<ILMethod, IDelegateAdapter> delegates;
 
@@ -133,7 +133,7 @@ namespace ILRuntime.Runtime.Intepreter
         /// </summary>
         public bool Boxed { get; set; }
 
-        public List<object> ManagedObjects { get { return managedObjs; } }
+        public IList<object> ManagedObjects { get { return managedObjs; } }
 
         public object CLRInstance { get { return clrInstance; } set { clrInstance = value; } }
 
@@ -191,9 +191,7 @@ namespace ILRuntime.Runtime.Intepreter
                     if (Type.FirstCLRBaseType != null && Type.FirstCLRBaseType is Enviorment.CrossBindingAdaptor)
                     {
                         CLRType clrType = type.AppDomain.GetType(((Enviorment.CrossBindingAdaptor)Type.FirstCLRBaseType).BaseCLRType) as CLRType;
-                        var field = clrType.GetField(index);
-                        var obj = field.GetValue(clrInstance);
-                        return obj;
+                        return clrType.GetFieldValue(index, clrInstance);
                     }
                     else
                         throw new TypeLoadException();
@@ -229,8 +227,7 @@ namespace ILRuntime.Runtime.Intepreter
                     if (Type.FirstCLRBaseType != null && Type.FirstCLRBaseType is Enviorment.CrossBindingAdaptor)
                     {
                         CLRType clrType = type.AppDomain.GetType(((Enviorment.CrossBindingAdaptor)Type.FirstCLRBaseType).BaseCLRType) as CLRType;
-                        var field = clrType.GetField(index);
-                        field.SetValue(clrInstance, value);
+                        clrType.SetFieldValue(index, ref clrInstance, value);
                     }
                     else
                         throw new TypeLoadException();
@@ -249,7 +246,7 @@ namespace ILRuntime.Runtime.Intepreter
                 InitializeFields((ILType)type.BaseType);
         }
 
-        internal unsafe void PushFieldAddress(int fieldIdx, StackObject* esp, List<object> managedStack)
+        internal unsafe void PushFieldAddress(int fieldIdx, StackObject* esp, IList<object> managedStack)
         {
             esp->ObjectType = ObjectTypes.FieldReference;
             esp->Value = managedStack.Count;
@@ -257,7 +254,7 @@ namespace ILRuntime.Runtime.Intepreter
             esp->ValueLow = fieldIdx;
         }
 
-        internal unsafe void PushToStack(int fieldIdx, StackObject* esp, Enviorment.AppDomain appdomain, List<object> managedStack)
+        internal unsafe void PushToStack(int fieldIdx, StackObject* esp, Enviorment.AppDomain appdomain, IList<object> managedStack)
         {
             if (fieldIdx < fields.Length && fieldIdx >= 0)
                 PushToStackSub(ref fields[fieldIdx], fieldIdx, esp, managedStack);
@@ -266,16 +263,14 @@ namespace ILRuntime.Runtime.Intepreter
                 if (Type.FirstCLRBaseType != null && Type.FirstCLRBaseType is Enviorment.CrossBindingAdaptor)
                 {
                     CLRType clrType = appdomain.GetType(((Enviorment.CrossBindingAdaptor)Type.FirstCLRBaseType).BaseCLRType) as CLRType;
-                    var field = clrType.GetField(fieldIdx);
-                    var obj = field.GetValue(clrInstance);
-                    ILIntepreter.PushObject(esp, managedStack, obj);
+                    ILIntepreter.PushObject(esp, managedStack, clrType.GetFieldValue(fieldIdx, clrInstance));
                 }
                 else
                     throw new TypeLoadException();
             }
         }
 
-        unsafe void PushToStackSub(ref StackObject field, int fieldIdx, StackObject* esp, List<object> managedStack)
+        unsafe void PushToStackSub(ref StackObject field, int fieldIdx, StackObject* esp, IList<object> managedStack)
         {
             *esp = field;
             if (field.ObjectType >= ObjectTypes.Object)
@@ -290,7 +285,7 @@ namespace ILRuntime.Runtime.Intepreter
             InitializeFields(type);
         }
 
-        internal unsafe void AssignFromStack(int fieldIdx, StackObject* esp, Enviorment.AppDomain appdomain, List<object> managedStack)
+        internal unsafe void AssignFromStack(int fieldIdx, StackObject* esp, Enviorment.AppDomain appdomain, IList<object> managedStack)
         {
             if (fieldIdx < fields.Length && fieldIdx >= 0)
                 AssignFromStackSub(ref fields[fieldIdx], fieldIdx, esp, managedStack);
@@ -300,14 +295,14 @@ namespace ILRuntime.Runtime.Intepreter
                 {
                     CLRType clrType = appdomain.GetType(((Enviorment.CrossBindingAdaptor)Type.FirstCLRBaseType).BaseCLRType) as CLRType;
                     var field = clrType.GetField(fieldIdx);
-                    field.SetValue(clrInstance, field.FieldType.CheckCLRTypes(ILIntepreter.CheckAndCloneValueType(StackObject.ToObject(esp, appdomain, managedStack), appdomain)));
+                    clrType.SetFieldValue(fieldIdx, ref clrInstance, field.FieldType.CheckCLRTypes(ILIntepreter.CheckAndCloneValueType(StackObject.ToObject(esp, appdomain, managedStack), appdomain)));
                 }
                 else
                     throw new TypeLoadException();
             }
         }
 
-        unsafe void AssignFromStackSub(ref StackObject field, int fieldIdx, StackObject* esp, List<object> managedStack)
+        unsafe void AssignFromStackSub(ref StackObject field, int fieldIdx, StackObject* esp, IList<object> managedStack)
         {
             esp = ILIntepreter.GetObjectAndResolveReference(esp);
             field = *esp;

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/ILTypeInstance.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 8e3c8c218911bc64b836907739e04af8
-timeCreated: 1496395688
+timeCreated: 1503890051
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/OpCodes/OpCode.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 2ef7583a5c2cabb4499ec0d948f9c7bb
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Intepreter/OpCodes/OpCodeEnum.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 7332c8f3285dcec4cbc6bd1494a4d846
-timeCreated: 1496395688
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 20 - 7
Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/RuntimeStack.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 
 using ILRuntime.CLR.Method;
+using ILRuntime.Other;
 using ILRuntime.Runtime.Intepreter;
 
 namespace ILRuntime.Runtime.Stack
@@ -13,8 +14,15 @@ namespace ILRuntime.Runtime.Stack
         ILIntepreter intepreter;
         StackObject* pointer;
         StackObject* endOfMemory;
+
         IntPtr nativePointer;
-        List<object> managedStack = new List<object>(32);
+
+#if DEBUG
+        IList<object> managedStack = new List<object>(32);
+#else
+        IList<object> managedStack = new UncheckedList<object>(32);
+#endif
+
         Stack<StackFrame> frames = new Stack<StackFrame>();
         const int MAXIMAL_STACK_OBJECTS = 1024 * 16;
 
@@ -41,7 +49,7 @@ namespace ILRuntime.Runtime.Stack
             }
         }
 
-        public List<object> ManagedStack { get { return managedStack; } }
+        public IList<object> ManagedStack { get { return managedStack; } }
 
         public void InitializeFrame(ILMethod method, StackObject* esp, out StackFrame res)
         {
@@ -69,18 +77,19 @@ namespace ILRuntime.Runtime.Stack
             frames.Push(frame);
         }
 
-        public StackObject* PopFrame(ref StackFrame frame, StackObject* esp, List<object> mStack)
+        public StackObject* PopFrame(ref StackFrame frame, StackObject* esp, IList<object> mStack)
         {
             if (frames.Count > 0 && frames.Peek().BasePointer == frame.BasePointer)
                 frames.Pop();
             else
                 throw new NotSupportedException();
             StackObject* returnVal = esp - 1;
-            StackObject* ret = frame.LocalVarPointer - frame.Method.ParameterCount;
+            var method = frame.Method;
+            StackObject* ret = ILIntepreter.Minus(frame.LocalVarPointer, method.ParameterCount);
             int mStackBase = frame.ManagedStackBase;
-            if (frame.Method.HasThis)
+            if (method.HasThis)
                 ret--;
-            if(frame.Method.ReturnType != intepreter.AppDomain.VoidType)
+            if(method.ReturnType != intepreter.AppDomain.VoidType)
             {
                 *ret = *returnVal;
                 if(ret->ObjectType == ObjectTypes.Object)
@@ -91,7 +100,11 @@ namespace ILRuntime.Runtime.Stack
                 }
                 ret++;
             }
-            mStack.RemoveRange(mStackBase, mStack.Count - mStackBase);
+#if DEBUG
+            ((List<object>)mStack).RemoveRange(mStackBase, mStack.Count - mStackBase);
+#else
+            ((UncheckedList<object>)mStack).RemoveRange(mStackBase, mStack.Count - mStackBase);
+#endif
             return ret;
         }
 

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/RuntimeStack.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 55de1685a7e4efa41b7f1f73a81aa07c
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackFrame.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 7394a29fd3deece4ea7c3d67f055faca
-timeCreated: 1496395688
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 6 - 7
Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackObject.cs

@@ -17,7 +17,7 @@ namespace ILRuntime.Runtime.Stack
         public int ValueLow;
 
         //IL2CPP can't process esp->ToObject() properly, so I can only use static function for this
-        public static unsafe object ToObject(StackObject* esp, ILRuntime.Runtime.Enviorment.AppDomain appdomain, List<object> mStack)
+        public static unsafe object ToObject(StackObject* esp, ILRuntime.Runtime.Enviorment.AppDomain appdomain, IList<object> mStack)
         {
             switch (esp->ObjectType)
             {
@@ -54,8 +54,8 @@ namespace ILRuntime.Runtime.Stack
                             }
                             else
                                 t = appdomain.GetType(obj.GetType());
-                            var fi = ((CLRType)t).GetField(esp->ValueLow);
-                            return fi.GetValue(obj);
+
+                            return ((CLRType)t).GetFieldValue(esp->ValueLow, obj);
                         }
                     }
                 case ObjectTypes.ArrayReference:
@@ -74,8 +74,7 @@ namespace ILRuntime.Runtime.Stack
                         else
                         {
                             CLR.TypeSystem.CLRType type = (CLR.TypeSystem.CLRType)t;
-                            var fi = type.GetField(esp->ValueLow);
-                            return fi.GetValue(null);
+                            return type.GetFieldValue(esp->ValueLow, null);
                         }
                     }
                 case ObjectTypes.StackObjectReference:
@@ -89,7 +88,7 @@ namespace ILRuntime.Runtime.Stack
             }
         }
 
-        public unsafe static void Initialized(ref StackObject esp, int idx, Type t, IType fieldType, List<object> mStack)
+        public unsafe static void Initialized(ref StackObject esp, int idx, Type t, IType fieldType, IList<object> mStack)
         {
             if (t.IsPrimitive)
             {
@@ -128,7 +127,7 @@ namespace ILRuntime.Runtime.Stack
                     esp.Value = idx;
                     if (fieldType is CLRType)
                     {
-                        mStack[idx] = Activator.CreateInstance(t);
+                        mStack[idx] = ((CLRType)fieldType).CreateDefaultInstance();
                     }
                     else
                     {

+ 1 - 1
Unity/Assets/ILRuntime/ILRuntime/Runtime/Stack/StackObject.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 5b8d9c7d873701f46b5f31887c908e61
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Code.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 40bcd801e7a1d4c44a8a0fd2eb72617e
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/CodeReader.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 132761467bbd32d4fa4a028592dca8fc
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/CodeWriter.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: d3ec49d880028814fa48da946e502290
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Document.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: e2b7c3598c4acd8468a789a015751a95
-timeCreated: 1496395689
+timeCreated: 1503890052
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/ExceptionHandler.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 21a64ac0bb4ca454a97ca76de8e7e9d6
-timeCreated: 1496395687
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/ILProcessor.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 4646551666f3dff4aab91bb8cecdc72d
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/Instruction.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 4b72b64dc0086154682d81c571aca5c0
-timeCreated: 1496395687
+timeCreated: 1503890050
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

+ 1 - 1
Unity/Assets/ILRuntime/Mono.Cecil.20/MonoCecil/Mono.Cecil.Cil/MethodBody.cs.meta

@@ -1,6 +1,6 @@
 fileFormatVersion: 2
 guid: 06e529fcda5f9094c9e3aef33b4579a7
-timeCreated: 1496395686
+timeCreated: 1503890049
 licenseType: Free
 MonoImporter:
   serializedVersion: 2

Некоторые файлы не были показаны из-за большого количества измененных файлов