From 4fc03008002d828e3dc84feb510c04964d1be822 Mon Sep 17 00:00:00 2001 From: liiir1985 Date: Sun, 19 Dec 2021 21:55:42 +0800 Subject: [PATCH] Removed more .net 7 usage for better compatibility --- .../ILRuntimeDebugEngine.csproj | 2 +- .../ILRuntimeDebuggerLauncher.csproj | 2 +- ILRuntime/CLR/Method/CLRMethod.cs | 2 +- ILRuntime/CLR/Method/ILMethod.cs | 15 +++++++---- ILRuntime/CLR/TypeSystem/CLRType.cs | 4 +-- ILRuntime/Other/UncheckedStack.cs | 5 +++- ILRuntime/Reflection/Extensions.cs | 3 ++- ILRuntime/Reflection/ILRuntimeMethodInfo.cs | 7 ++++- ILRuntime/Reflection/ILRuntimePropertyInfo.cs | 8 +++++- ILRuntime/Reflection/ILRuntimeType.cs | 2 ++ .../Runtime/CLRBinding/CLRBindingUtils.cs | 1 - ILRuntime/Runtime/Enviorment/AppDomain.cs | 5 ++-- .../Enviorment/CrossBindingCodeGenerator.cs | 8 +++--- .../Runtime/Enviorment/InvocationContext.cs | 27 +++++++++++++++---- ILRuntime/Runtime/ILRuntimeJITAttribute.cs | 1 - .../RegisterVM/AsyncJITCompileWorker.cs | 1 - .../Runtime/Stack/StackObjectAllocator.cs | 1 - 17 files changed, 65 insertions(+), 29 deletions(-) diff --git a/Debugging/ILRuntimeDebugEngine/ILRuntimeDebugEngine.csproj b/Debugging/ILRuntimeDebugEngine/ILRuntimeDebugEngine.csproj index cf87e5ab..a8567f45 100644 --- a/Debugging/ILRuntimeDebugEngine/ILRuntimeDebugEngine.csproj +++ b/Debugging/ILRuntimeDebugEngine/ILRuntimeDebugEngine.csproj @@ -9,7 +9,7 @@ Properties ILRuntimeDebugEngine ILRuntimeDebugEngine - v4.5 + v4.7.1 512 diff --git a/Debugging/ILRuntimeDebuggerLauncher/ILRuntimeDebuggerLauncher.csproj b/Debugging/ILRuntimeDebuggerLauncher/ILRuntimeDebuggerLauncher.csproj index 67666688..7791c09c 100644 --- a/Debugging/ILRuntimeDebuggerLauncher/ILRuntimeDebuggerLauncher.csproj +++ b/Debugging/ILRuntimeDebuggerLauncher/ILRuntimeDebuggerLauncher.csproj @@ -29,7 +29,7 @@ Properties ILRuntimeDebuggerLauncher ILRuntimeDebuggerLauncher - v4.6 + v4.7.1 true true true diff --git a/ILRuntime/CLR/Method/CLRMethod.cs b/ILRuntime/CLR/Method/CLRMethod.cs index 3c48f3a1..5dc656ad 100644 --- a/ILRuntime/CLR/Method/CLRMethod.cs +++ b/ILRuntime/CLR/Method/CLRMethod.cs @@ -417,7 +417,7 @@ public IMethod MakeGenericMethod(IType[] genericArguments) } argString = argString.Substring(0, argString.Length - 2); - throw new Exception($"MakeGenericMethod failed : {def.DeclaringType.FullName}.{def.Name}<{argString}>"); + throw new Exception(string.Format("MakeGenericMethod failed : {0}.{1}<{}>", def.DeclaringType.FullName, def.Name, argString)); } #endif var res = new CLRMethod(t, declaringType, appdomain); diff --git a/ILRuntime/CLR/Method/ILMethod.cs b/ILRuntime/CLR/Method/ILMethod.cs index 3ac41b1b..6423d78b 100644 --- a/ILRuntime/CLR/Method/ILMethod.cs +++ b/ILRuntime/CLR/Method/ILMethod.cs @@ -405,8 +405,9 @@ void PrewarmBody(HashSet alreadyPrewarmed) case OpCodeEnum.Callvirt: { var m = appdomain.GetMethod(ins.TokenInteger); - if (m is ILMethod ilm) + if (m is ILMethod) { + ILMethod ilm = (ILMethod)m; //如果参数alreadyPrewarmed不为空,则不仅prewarm当前方法,还会递归prewarm所有子调用 //如果参数alreadyPrewarmed为空,则只prewarm当前方法 if (alreadyPrewarmed != null) @@ -414,8 +415,9 @@ void PrewarmBody(HashSet alreadyPrewarmed) ilm.Prewarm(alreadyPrewarmed); } } - else if (m is CLRMethod clrm) + else if (m is CLRMethod) { + CLRMethod clrm = (CLRMethod)m; ILRuntime.CLR.Utils.Extensions.GetTypeFlags(clrm.DeclearingType.TypeForCLR); } } @@ -456,8 +458,9 @@ void PrewarmBodyRegister(HashSet alreadyPrewarmed) case OpCodeREnum.Callvirt: { var m = appdomain.GetMethod(ins.Operand); - if (m is ILMethod ilm) + if (m is ILMethod) { + ILMethod ilm = (ILMethod)m; //如果参数alreadyPrewarmed不为空,则不仅prewarm当前方法,还会递归prewarm所有子调用 //如果参数alreadyPrewarmed为空,则只prewarm当前方法 if (alreadyPrewarmed != null) @@ -465,8 +468,9 @@ void PrewarmBodyRegister(HashSet alreadyPrewarmed) ilm.Prewarm(alreadyPrewarmed); } } - else if (m is CLRMethod clrm) + else if (m is CLRMethod) { + CLRMethod clrm = (CLRMethod)m; ILRuntime.CLR.Utils.Extensions.GetTypeFlags(clrm.DeclearingType.TypeForCLR); } } @@ -510,8 +514,9 @@ private void Prewarm(HashSet alreadyPrewarmed) { t = appdomain.GetType(v.VariableType, DeclearingType, this); } - if (t is CLRType ct) + if (t is CLRType) { + CLRType ct = (CLRType)t; var fields = ct.Fields; ILRuntime.CLR.Utils.Extensions.GetTypeFlags(ct.TypeForCLR); } diff --git a/ILRuntime/CLR/TypeSystem/CLRType.cs b/ILRuntime/CLR/TypeSystem/CLRType.cs index 5d6bfe85..e49a684e 100644 --- a/ILRuntime/CLR/TypeSystem/CLRType.cs +++ b/ILRuntime/CLR/TypeSystem/CLRType.cs @@ -346,7 +346,7 @@ public object PerformMemberwiseClone(object target) if (memberwiseClone != null) { - var del = (Func)memberwiseClone.CreateDelegate(typeof(Func)); + var del = (Func)Delegate.CreateDelegate(typeof(Func), memberwiseClone); memberwiseCloneDelegate = (ref object t) => del(t); } else @@ -967,7 +967,7 @@ public IType MakeGenericInstance(KeyValuePair[] genericArguments) } argString = argString.Substring(0, argString.Length - 2); - throw new Exception($"MakeGenericType failed : {clrType.FullName}<{argString}>"); + throw new Exception(string.Format("MakeGenericType failed : {0}<{1}>", clrType.FullName, argString)); } #endif var res = new CLRType(newType, appdomain); diff --git a/ILRuntime/Other/UncheckedStack.cs b/ILRuntime/Other/UncheckedStack.cs index 7a69279c..fb1760de 100644 --- a/ILRuntime/Other/UncheckedStack.cs +++ b/ILRuntime/Other/UncheckedStack.cs @@ -9,7 +9,10 @@ [Serializable] [DebuggerDisplay("Count = {Count}")] [ComVisible(false)] -public class UncheckedStack : IEnumerable, IEnumerable, ICollection, IReadOnlyCollection +public class UncheckedStack : IEnumerable, IEnumerable, ICollection +#if NET_4_6 || NET_STANDARD_2_0 + , IReadOnlyCollection +#endif { [Serializable] public struct Enumerator : IEnumerator, IDisposable, IEnumerator diff --git a/ILRuntime/Reflection/Extensions.cs b/ILRuntime/Reflection/Extensions.cs index a52b66e7..141899c4 100644 --- a/ILRuntime/Reflection/Extensions.cs +++ b/ILRuntime/Reflection/Extensions.cs @@ -51,7 +51,8 @@ public static object CreateInstance(this CustomAttribute attribute, IType at, Ru { foreach (var j in attribute.Fields) { - var field = it.GetField(j.Name, out int index); + int index; + var field = it.GetField(j.Name, out index); if (field != null) ((ILRuntime.Runtime.Intepreter.ILTypeInstance)ins)[index] = j.Argument.Value; } diff --git a/ILRuntime/Reflection/ILRuntimeMethodInfo.cs b/ILRuntime/Reflection/ILRuntimeMethodInfo.cs index 4e535c81..ed2218bc 100644 --- a/ILRuntime/Reflection/ILRuntimeMethodInfo.cs +++ b/ILRuntime/Reflection/ILRuntimeMethodInfo.cs @@ -178,10 +178,14 @@ public override Type ReturnType { get { - return method.ReturnType?.ReflectionType; + if (method.ReturnType != null) + return method.ReturnType.ReflectionType; + else + return null; } } +#if NET_4_6 || NET_STANDARD_2_0 public override Delegate CreateDelegate(Type delegateType) { throw new NotSupportedException("please use CreateDelegate(Type delegateType, object target)"); @@ -216,5 +220,6 @@ public override Delegate CreateDelegate(Type delegateType, object target) } return del.GetConvertor(delegateType); } +#endif } } diff --git a/ILRuntime/Reflection/ILRuntimePropertyInfo.cs b/ILRuntime/Reflection/ILRuntimePropertyInfo.cs index c7dff38a..133e0736 100644 --- a/ILRuntime/Reflection/ILRuntimePropertyInfo.cs +++ b/ILRuntime/Reflection/ILRuntimePropertyInfo.cs @@ -141,7 +141,13 @@ public override Type PropertyType } } - public ILRuntime.Mono.Cecil.TypeReference Definition => definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters [ 0 ].ParameterType; + public ILRuntime.Mono.Cecil.TypeReference Definition + { + get + { + return definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters[0].ParameterType; + } + } public override Type DeclaringType diff --git a/ILRuntime/Reflection/ILRuntimeType.cs b/ILRuntime/Reflection/ILRuntimeType.cs index b2402802..5e0bd7dc 100644 --- a/ILRuntime/Reflection/ILRuntimeType.cs +++ b/ILRuntime/Reflection/ILRuntimeType.cs @@ -613,10 +613,12 @@ public override int GetHashCode() { return type.GetHashCode(); } +#if NET_4_6 || NET_STANDARD_2_0 public override bool Equals(Type o) { return o is ILRuntimeType ? ((ILRuntimeType)o).type == type : false; } +#endif public override bool Equals(object o) { return o is ILRuntimeType ? ((ILRuntimeType)o).type == type : false; diff --git a/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs b/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs index d8592166..66a4abd1 100644 --- a/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs +++ b/ILRuntime/Runtime/CLRBinding/CLRBindingUtils.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace ILRuntime.Runtime.CLRBinding { diff --git a/ILRuntime/Runtime/Enviorment/AppDomain.cs b/ILRuntime/Runtime/Enviorment/AppDomain.cs index 854f1f17..c6570eac 100644 --- a/ILRuntime/Runtime/Enviorment/AppDomain.cs +++ b/ILRuntime/Runtime/Enviorment/AppDomain.cs @@ -801,7 +801,7 @@ public void InitializeBindings(bool isThread = false) UnityEngine.Debug.Log("CLRBindingUtils.Initialize Done in thread.."); #endif }); - thread.Name = $"CLRBindings-Thread #{thread.ManagedThreadId}"; + thread.Name = string.Format("CLRBindings-Thread #{0}",thread.ManagedThreadId); thread.Start(); } else @@ -853,8 +853,9 @@ public IType GetType(string fullname) for (int i = 0; i < genericArguments.Length; i++) { string key = null; - if (bt is ILType ilt) + if (bt is ILType) { + ILType ilt = (ILType)bt; key = ilt.TypeDefinition.GenericParameters[i].FullName; } else diff --git a/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs b/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs index f20f30d8..3cc0c1fe 100644 --- a/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs +++ b/ILRuntime/Runtime/Enviorment/CrossBindingCodeGenerator.cs @@ -138,7 +138,7 @@ static void GenerateCrossBindingMethodBody(StringBuilder sb, List vi StringBuilder sBuilder = new StringBuilder(); var p = i.GetParameters()[0]; p.ParameterType.GetClassName(out clsName, out realClsName, out isByRef, true); - pName = $"this [{realClsName + " " + p.Name}]"; + pName = string.Format("this [{0}]", realClsName + " " + p.Name); isIndexFunc = true; } @@ -192,8 +192,8 @@ static void GenerateCrossBindingMethodBody(StringBuilder sb, List vi if (isProperty) { string baseMethodName = isIndexFunc - ? $"base[{i.GetParameters()[0].Name}]" - : $"base.{i.Name.Substring(4)}"; + ? string.Format("base[{0}]", i.GetParameters()[0].Name) + : string.Format("base.{0}", i.Name.Substring(4)); if (isGetter) { sb.AppendLine(string.Format(" return {0};", baseMethodName)); @@ -597,7 +597,7 @@ static void GenInitParams(StringBuilder sb, ParameterInfo[] param) { if (p.IsOut) { - sb.AppendLine($" {p.Name} = default({p.ParameterType.GetElementType().FullName});"); + sb.AppendLine(string.Format(" {0} = default({1});", p.Name, p.ParameterType.GetElementType().FullName)); } } } diff --git a/ILRuntime/Runtime/Enviorment/InvocationContext.cs b/ILRuntime/Runtime/Enviorment/InvocationContext.cs index 567a8755..41bef847 100644 --- a/ILRuntime/Runtime/Enviorment/InvocationContext.cs +++ b/ILRuntime/Runtime/Enviorment/InvocationContext.cs @@ -215,16 +215,31 @@ internal void SetInvoked(StackObject* esp) internal StackObject* ESP { - get => esp; + get + { + return esp; + } set { esp = value; } } - internal ILIntepreter Intepreter => intp; + internal ILIntepreter Intepreter + { + get + { + return intp; + } + } - internal IList ManagedStack => mStack; + internal IList ManagedStack + { + get + { + return mStack; + } + } public void PushBool(bool val) { @@ -300,7 +315,8 @@ public void PushValueType(ref T obj) Type t = typeof(T); bool needPush = false; StackObject* res = default(StackObject*); - if (domain.ValueTypeBinders.TryGetValue(t, out var binder)) + ValueTypeBinder binder; + if (domain.ValueTypeBinders.TryGetValue(t, out binder)) { var binderT = binder as ValueTypeBinder; if (binderT != null) @@ -492,7 +508,8 @@ public T ReadValueType() CheckReturnValue(); Type t = typeof(T); T res = default(T); - if (domain.ValueTypeBinders.TryGetValue(t, out var binder)) + ValueTypeBinder binder; + if (domain.ValueTypeBinders.TryGetValue(t, out binder)) { var binderT = binder as ValueTypeBinder; if (binderT != null) diff --git a/ILRuntime/Runtime/ILRuntimeJITAttribute.cs b/ILRuntime/Runtime/ILRuntimeJITAttribute.cs index 63a15fba..87e5ae4c 100644 --- a/ILRuntime/Runtime/ILRuntimeJITAttribute.cs +++ b/ILRuntime/Runtime/ILRuntimeJITAttribute.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace ILRuntime.Runtime { diff --git a/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs b/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs index 07e828d9..b9ad5fb2 100644 --- a/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs +++ b/ILRuntime/Runtime/Intepreter/RegisterVM/AsyncJITCompileWorker.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Text; using System.Threading; -using System.Threading.Tasks; using ILRuntime.CLR.Method; namespace ILRuntime.Runtime.Intepreter.RegisterVM diff --git a/ILRuntime/Runtime/Stack/StackObjectAllocator.cs b/ILRuntime/Runtime/Stack/StackObjectAllocator.cs index 3a670346..1c353465 100644 --- a/ILRuntime/Runtime/Stack/StackObjectAllocator.cs +++ b/ILRuntime/Runtime/Stack/StackObjectAllocator.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using System.Threading.Tasks; namespace ILRuntime.Runtime.Stack {