C# Member 'System.Char[] System.String::ToCharArray()' is declared in another module and needs to be imported in Mono.Cecil

StackOverflow https://stackoverflow.com/questions/21789638

  •  11-10-2022
  •  | 
  •  

문제

I use Mono.Cecil to encrypt my strings in my assemblies.

but in

myAssemblyDefinition.Write(myAssemblyPath);

I get an error:

Member 'System.Char[] System.String::ToCharArray()' is declared in another module and needs to be imported

I tried to import String.ToCharArray method with all these lines:

myAssemblyDefinition.MainModule.Import(stringTypeReference.Resolve());
myAssemblyDefinition.MainModule.Import(stringTypeReference.Resolve().Module.Types.Where(x => x.Name == "String").First());
MethodDefinition toCharArrayMethod = stringTypeReference.Resolve().Module.Types.Where(x => x.Name == "String").First().Methods.Where(x => x.Name == "ToCharArray").First();
myAssemblyDefinition.MainModule.Import(toCharArrayMethod);
myAssemblyDefinition.MainModule.Import(typeof(System.String));

but my problem still exists. I used ToCharArray method for inject decryptMethod to my assembly. Can anybody help to me to solve this or is there a sample code to encrypt string for obfuscating with Mono.Cecil 0.9.5 version?

도움이 되었습니까?

해결책

If your target assembly's runtime version is same with your injection code's runtime version. Try following lines.

var toCharArray =
 asm.MainModule.Import(typeof(string).GetMethod("ToCharArray", new Type[] { }));
proccer.Emit(OpCodes.Callvirt, toCharArray);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top