2022-09-22 08:56:07 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-09-26 12:49:10 +08:00
|
|
|
|
using UnityEngine;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
|
|
|
|
|
namespace HybridCLR.Editor.Meta
|
|
|
|
|
{
|
|
|
|
|
public class GenericMethod
|
|
|
|
|
{
|
|
|
|
|
public MethodDef Method { get; }
|
|
|
|
|
|
|
|
|
|
public List<TypeSig> KlassInst { get; }
|
|
|
|
|
|
|
|
|
|
public List<TypeSig> MethodInst { get; }
|
|
|
|
|
|
|
|
|
|
private readonly int _hashCode;
|
|
|
|
|
|
|
|
|
|
public GenericMethod(MethodDef method, List<TypeSig> classInst, List<TypeSig> methodInst)
|
|
|
|
|
{
|
|
|
|
|
Method = method;
|
|
|
|
|
KlassInst = classInst;
|
|
|
|
|
MethodInst = methodInst;
|
|
|
|
|
_hashCode = ComputHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GenericMethod ToGenericShare()
|
|
|
|
|
{
|
2023-08-22 11:16:31 +08:00
|
|
|
|
ICorLibTypes corLibTypes = Method.Module.CorLibTypes;
|
|
|
|
|
return new GenericMethod(Method, MetaUtil.ToShareTypeSigs(corLibTypes, KlassInst), MetaUtil.ToShareTypeSigs(corLibTypes, MethodInst));
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
GenericMethod o = (GenericMethod)obj;
|
|
|
|
|
return Method == o.Method
|
2022-10-17 12:16:18 +08:00
|
|
|
|
&& MetaUtil.EqualsTypeSigArray(KlassInst, o.KlassInst)
|
|
|
|
|
&& MetaUtil.EqualsTypeSigArray(MethodInst, o.MethodInst);
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return _hashCode;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 14:47:27 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{Method}|{string.Join(",", (IEnumerable<TypeSig>)KlassInst ?? Array.Empty<TypeSig>())}|{string.Join(",", (IEnumerable<TypeSig>)MethodInst ?? Array.Empty<TypeSig>())}";
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
private int ComputHashCode()
|
|
|
|
|
{
|
|
|
|
|
int hash = MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(Method);
|
|
|
|
|
if (KlassInst != null)
|
|
|
|
|
{
|
|
|
|
|
hash = HashUtil.CombineHash(hash, HashUtil.ComputHash(KlassInst));
|
|
|
|
|
}
|
|
|
|
|
if (MethodInst != null)
|
|
|
|
|
{
|
|
|
|
|
hash = HashUtil.CombineHash(hash, HashUtil.ComputHash(MethodInst));
|
|
|
|
|
}
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 15:16:43 +08:00
|
|
|
|
public MethodSpec ToMethodSpec()
|
|
|
|
|
{
|
|
|
|
|
IMethodDefOrRef mt = KlassInst != null ?
|
|
|
|
|
(IMethodDefOrRef)new MemberRefUser(this.Method.Module, Method.Name, Method.MethodSig, new TypeSpecUser(new GenericInstSig(this.Method.DeclaringType.ToTypeSig().ToClassOrValueTypeSig(), this.KlassInst)))
|
|
|
|
|
: this.Method;
|
|
|
|
|
return new MethodSpecUser(mt, new GenericInstMethodSig(MethodInst));
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
public static GenericMethod ResolveMethod(IMethod method, GenericArgumentContext ctx)
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log($"== resolve method:{method}");
|
|
|
|
|
TypeDef typeDef = null;
|
|
|
|
|
List<TypeSig> klassInst = null;
|
|
|
|
|
List<TypeSig> methodInst = null;
|
|
|
|
|
|
|
|
|
|
MethodDef methodDef = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var decalringType = method.DeclaringType;
|
|
|
|
|
typeDef = decalringType.ResolveTypeDef();
|
|
|
|
|
if (typeDef == null)
|
|
|
|
|
{
|
2022-09-22 12:11:26 +08:00
|
|
|
|
return null;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
GenericInstSig gis = decalringType.TryGetGenericInstSig();
|
|
|
|
|
if (gis != null)
|
|
|
|
|
{
|
|
|
|
|
klassInst = ctx != null ? gis.GenericArguments.Select(ga => MetaUtil.Inflate(ga, ctx)).ToList() : gis.GenericArguments.ToList();
|
|
|
|
|
}
|
|
|
|
|
methodDef = method.ResolveMethodDef();
|
|
|
|
|
if (methodDef == null)
|
|
|
|
|
{
|
2022-10-14 22:32:15 +08:00
|
|
|
|
Debug.LogWarning($"method:{method} ResolveMethodDef() == null");
|
2022-09-22 08:56:07 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (method is MethodSpec methodSpec)
|
|
|
|
|
{
|
|
|
|
|
methodInst = ctx != null ? methodSpec.GenericInstMethodSig.GenericArguments.Select(ga => MetaUtil.Inflate(ga, ctx)).ToList()
|
|
|
|
|
: methodSpec.GenericInstMethodSig.GenericArguments.ToList();
|
|
|
|
|
}
|
|
|
|
|
return new GenericMethod(methodDef, klassInst, methodInst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|