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