fix: 泛型限定
This commit is contained in:
1 file changed
+36
-15
@@ -51,15 +51,15 @@ namespace ShrinkEventBus.CodeGen
|
|||||||
var awake = type.Methods.FirstOrDefault(m => m.Name == "Awake" && !m.IsStatic);
|
var awake = type.Methods.FirstOrDefault(m => m.Name == "Awake" && !m.IsStatic);
|
||||||
if (awake == null)
|
if (awake == null)
|
||||||
{
|
{
|
||||||
var baseAwake = FindVirtualMethodInBase(type, "Awake");
|
var baseAwakeRef = FindBaseMethodReference(type, "Awake", module);
|
||||||
if (baseAwake != null)
|
if (baseAwakeRef != null)
|
||||||
{
|
{
|
||||||
awake = new MethodDefinition("Awake",
|
awake = new MethodDefinition("Awake",
|
||||||
MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual,
|
MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual,
|
||||||
module.TypeSystem.Void);
|
module.TypeSystem.Void);
|
||||||
var il = awake.Body.GetILProcessor();
|
var il = awake.Body.GetILProcessor();
|
||||||
il.Emit(OpCodes.Ldarg_0);
|
il.Emit(OpCodes.Ldarg_0);
|
||||||
il.Emit(OpCodes.Call, module.ImportReference(baseAwake));
|
il.Emit(OpCodes.Call, baseAwakeRef);
|
||||||
il.Emit(OpCodes.Ldarg_0);
|
il.Emit(OpCodes.Ldarg_0);
|
||||||
il.Emit(OpCodes.Call, _autoRegisterMethodRef);
|
il.Emit(OpCodes.Call, _autoRegisterMethodRef);
|
||||||
il.Emit(OpCodes.Ret);
|
il.Emit(OpCodes.Ret);
|
||||||
@@ -91,8 +91,8 @@ namespace ShrinkEventBus.CodeGen
|
|||||||
var onDestroy = type.Methods.FirstOrDefault(m => m.Name == "OnDestroy" && !m.IsStatic);
|
var onDestroy = type.Methods.FirstOrDefault(m => m.Name == "OnDestroy" && !m.IsStatic);
|
||||||
if (onDestroy == null)
|
if (onDestroy == null)
|
||||||
{
|
{
|
||||||
var baseOnDestroy = FindVirtualMethodInBase(type, "OnDestroy");
|
var baseOnDestroyRef = FindBaseMethodReference(type, "OnDestroy", module);
|
||||||
if (baseOnDestroy != null)
|
if (baseOnDestroyRef != null)
|
||||||
{
|
{
|
||||||
onDestroy = new MethodDefinition("OnDestroy",
|
onDestroy = new MethodDefinition("OnDestroy",
|
||||||
MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual,
|
MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Virtual,
|
||||||
@@ -101,7 +101,7 @@ namespace ShrinkEventBus.CodeGen
|
|||||||
il.Emit(OpCodes.Ldarg_0);
|
il.Emit(OpCodes.Ldarg_0);
|
||||||
il.Emit(OpCodes.Call, _unregisterInstanceMethodRef);
|
il.Emit(OpCodes.Call, _unregisterInstanceMethodRef);
|
||||||
il.Emit(OpCodes.Ldarg_0);
|
il.Emit(OpCodes.Ldarg_0);
|
||||||
il.Emit(OpCodes.Call, module.ImportReference(baseOnDestroy));
|
il.Emit(OpCodes.Call, baseOnDestroyRef);
|
||||||
il.Emit(OpCodes.Ret);
|
il.Emit(OpCodes.Ret);
|
||||||
type.Methods.Add(onDestroy);
|
type.Methods.Add(onDestroy);
|
||||||
return;
|
return;
|
||||||
@@ -126,23 +126,46 @@ namespace ShrinkEventBus.CodeGen
|
|||||||
instructions.ForEach(i => processor.Body.Instructions.Insert(0, i));
|
instructions.ForEach(i => processor.Body.Instructions.Insert(0, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MethodDefinition FindVirtualMethodInBase(TypeDefinition type, string methodName)
|
private static MethodReference FindBaseMethodReference(TypeDefinition type, string methodName, ModuleDefinition module)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseType = type.BaseType?.Resolve();
|
var baseTypeRef = type.BaseType;
|
||||||
while (baseType != null)
|
while (baseTypeRef != null)
|
||||||
{
|
{
|
||||||
var method = baseType.Methods.FirstOrDefault(
|
var baseTypeDef = baseTypeRef.Resolve();
|
||||||
|
if (baseTypeDef == null) break;
|
||||||
|
|
||||||
|
var method = baseTypeDef.Methods.FirstOrDefault(
|
||||||
m => m.Name == methodName && !m.IsStatic && m.IsVirtual);
|
m => m.Name == methodName && !m.IsStatic && m.IsVirtual);
|
||||||
if (method != null) return method;
|
|
||||||
baseType = baseType.BaseType?.Resolve();
|
if (method != null)
|
||||||
|
{
|
||||||
|
if (baseTypeRef is GenericInstanceType genericInstance)
|
||||||
|
{
|
||||||
|
var methodRef = new MethodReference(
|
||||||
|
method.Name,
|
||||||
|
module.ImportReference(method.ReturnType),
|
||||||
|
module.ImportReference(genericInstance))
|
||||||
|
{
|
||||||
|
HasThis = method.HasThis,
|
||||||
|
ExplicitThis = method.ExplicitThis,
|
||||||
|
CallingConvention = method.CallingConvention
|
||||||
|
};
|
||||||
|
return methodRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
return module.ImportReference(method);
|
||||||
|
}
|
||||||
|
|
||||||
|
baseTypeRef = baseTypeDef.BaseType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// ignored
|
// ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,9 +184,7 @@ namespace ShrinkEventBus.CodeGen
|
|||||||
{
|
{
|
||||||
ModuleDefinition result = null;
|
ModuleDefinition result = null;
|
||||||
var visited = new HashSet<string>();
|
var visited = new HashSet<string>();
|
||||||
SearchRecursive(
|
SearchRecursive(_assemblyResolver.Resolve(module.Assembly.Name), ref result, visited);
|
||||||
_assemblyResolver.Resolve(module.Assembly.Name),
|
|
||||||
ref result, visited);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user