修复 ReflectionCompatibilityDetector处理Enum.TryParse<T>,并且T包含泛型参数时抛出异常的bug

1.x
walon 2025-06-16 17:02:31 +08:00
parent 2d9782fdb7
commit 26b4828094
1 changed files with 22 additions and 7 deletions

View File

@ -190,20 +190,28 @@ namespace Obfuz.ObfusPasses.SymbolObfus
private void AnalyzeEnum(IMethod method, TypeDef typeDef)
{
const int extraSearchInstructionCount = 3;
TypeDef parseType = GetMethodGenericParameter(method)?.ToTypeDefOrRef()?.ResolveTypeDef();
TypeSig parseTypeSig = GetMethodGenericParameter(method);
TypeDef parseType = parseTypeSig?.ToTypeDefOrRef().ResolveTypeDef();
switch (method.Name)
{
case "Parse":
{
if (parseType != null)
if (parseTypeSig != null)
{
// Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive)
if (parseType != null)
{
if (IsAnyEnumItemRenamed(parseType))
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed.");
}
}
else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T should not be renamed.");
}
}
else
{
// Enum.Parse(Type type, string name) or Enum.Parse(Type type, string name, bool ignoreCase)
TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef();
@ -220,15 +228,22 @@ namespace Obfuz.ObfusPasses.SymbolObfus
}
case "TryParse":
{
if (parseType != null)
if (parseTypeSig != null)
{
// Enum.TryParse<T>(string name, out T result) or Enum.TryParse<T>(string name, bool ignoreCase, out T result)
if (parseType != null)
{
if (IsAnyEnumItemRenamed(parseType))
{
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed.");
}
}
else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T should not be renamed.");
}
}
else
{
throw new Exception("impossible");
}