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

dev
walon 2025-06-16 17:02:31 +08:00
parent 0fe0a91793
commit b84d158fac
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) private void AnalyzeEnum(IMethod method, TypeDef typeDef)
{ {
const int extraSearchInstructionCount = 3; const int extraSearchInstructionCount = 3;
TypeDef parseType = GetMethodGenericParameter(method)?.ToTypeDefOrRef()?.ResolveTypeDef(); TypeSig parseTypeSig = GetMethodGenericParameter(method);
TypeDef parseType = parseTypeSig?.ToTypeDefOrRef().ResolveTypeDef();
switch (method.Name) switch (method.Name)
{ {
case "Parse": case "Parse":
{ {
if (parseType != null) if (parseTypeSig != null)
{ {
// Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive) // Enum.Parse<T>(string name) or Enum.Parse<T>(string name, bool caseInsensitive)
if (parseType != null)
{
if (IsAnyEnumItemRenamed(parseType)) if (IsAnyEnumItemRenamed(parseType))
{ {
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed."); Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.Parse<T> field of T:{parseType.FullName} is renamed.");
} }
} }
else 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) // Enum.Parse(Type type, string name) or Enum.Parse(Type type, string name, bool ignoreCase)
TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef(); TypeDef enumType = FindLatestTypeOf(method.GetParamCount() + extraSearchInstructionCount)?.ResolveTypeDef();
@ -220,15 +228,22 @@ namespace Obfuz.ObfusPasses.SymbolObfus
} }
case "TryParse": 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) // 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)) if (IsAnyEnumItemRenamed(parseType))
{ {
Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed."); Debug.LogError($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T:{parseType.FullName} is renamed.");
} }
} }
else else
{
Debug.LogWarning($"[ReflectionCompatibilityDetector] Reflection compatibility issue in {_curCallingMethod}: Enum.TryParse<T> field of T should not be renamed.");
}
}
else
{ {
throw new Exception("impossible"); throw new Exception("impossible");
} }