public void ProcessRewriteToString(ModuleDefinition systemModuleDefinition, TypeDefinition enumTypeDefinition) { if (enumTypeDefinition.Methods.Any(x => x.Name == "ToString")) { return; } var method = EnumExtensionUtility.MakeToString(enumTypeDefinition); enumTypeDefinition.Methods.Add(method); var moduleDefinition = enumTypeDefinition.Module; EnumExtensionUtility.ImplementNoFlag <T>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, method); }
private static MethodDefinition Implement(ModuleDefinition moduleDefinition, ModuleDefinition systemModuleDefinition, string methodName, TypeDefinition enumTypeDefinition, TypeDefinition privateImplementationDetails) { var toStringMethodDefinition = new MethodDefinition(methodName, MethodAttributes.Static | MethodAttributes.Final | MethodAttributes.Public | MethodAttributes.HideBySig, moduleDefinition.TypeSystem.String) { AggressiveInlining = true, Parameters = { new ParameterDefinition("value", ParameterAttributes.None, new ByReferenceType(moduleDefinition.ImportReference(enumTypeDefinition))) } }; privateImplementationDetails.Methods.Add(toStringMethodDefinition); var hasFlag = enumTypeDefinition.HasCustomAttributes && enumTypeDefinition.CustomAttributes.Any(customAttribute => customAttribute.AttributeType.FullName == "System.FlagsAttribute"); switch (enumTypeDefinition.Fields[0].FieldType.Name) { case "Byte": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <byte>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <byte>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "SByte": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <sbyte>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <sbyte>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "Int16": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <short>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <short>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "UInt16": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <ushort>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <ushort>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "Int32": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <int>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <int>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "UInt32": if (hasFlag) { EnumExtensionUtility.ImplementFlags32 <uint>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <uint>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "Int64": if (hasFlag) { EnumExtensionUtility.ImplementFlags64 <long>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <long>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; case "UInt64": if (hasFlag) { EnumExtensionUtility.ImplementFlags64 <ulong>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } else { EnumExtensionUtility.ImplementNoFlag <ulong>(systemModuleDefinition, moduleDefinition, enumTypeDefinition, toStringMethodDefinition); } break; } return(toStringMethodDefinition); }