2022-09-22 08:56:07 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
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 GenericClass
|
|
|
|
|
{
|
|
|
|
|
public TypeDef Type { get; }
|
|
|
|
|
|
|
|
|
|
public List<TypeSig> KlassInst { get; }
|
|
|
|
|
|
|
|
|
|
private readonly int _hashCode;
|
|
|
|
|
|
|
|
|
|
public GenericClass(TypeDef type, List<TypeSig> classInst)
|
|
|
|
|
{
|
|
|
|
|
Type = type;
|
|
|
|
|
KlassInst = classInst;
|
|
|
|
|
_hashCode = ComputHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GenericClass ToGenericShare()
|
|
|
|
|
{
|
|
|
|
|
return new GenericClass(Type, MetaUtil.ToShareTypeSigs(KlassInst));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
if (obj is GenericClass gc)
|
|
|
|
|
{
|
|
|
|
|
return Type == gc.Type && EqualityUtil.EqualsTypeSigArray(KlassInst, gc.KlassInst);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return _hashCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int ComputHashCode()
|
|
|
|
|
{
|
|
|
|
|
int hash = TypeEqualityComparer.Instance.GetHashCode(Type);
|
|
|
|
|
if (KlassInst != null)
|
|
|
|
|
{
|
|
|
|
|
hash = HashUtil.CombineHash(hash, HashUtil.ComputHash(KlassInst));
|
|
|
|
|
}
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 15:16:43 +08:00
|
|
|
|
public TypeSig ToTypeSig()
|
|
|
|
|
{
|
|
|
|
|
return new GenericInstSig(this.Type.ToTypeSig().ToClassOrValueTypeSig(), this.KlassInst);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
public static GenericClass ResolveClass(TypeSpec type, GenericArgumentContext ctx)
|
|
|
|
|
{
|
|
|
|
|
var sig = type.TypeSig.ToGenericInstSig();
|
|
|
|
|
if (sig == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
TypeDef def = type.ResolveTypeDef();
|
2022-09-26 12:49:10 +08:00
|
|
|
|
if (def == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"type:{type} ResolveTypeDef() == null");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-09-22 08:56:07 +08:00
|
|
|
|
var klassInst = ctx != null ? sig.GenericArguments.Select(ga => MetaUtil.Inflate(ga, ctx)).ToList() : sig.GenericArguments.ToList();
|
|
|
|
|
return new GenericClass(def, klassInst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|