CompileAssemblyFromSource + Obfuscation = don't work

189 views Asked by At

i have working CompileAssemblyFromSource code. But when i use any code protector like RedGate SmartAssembly or Themida it's stop working and i get error "Could not load file or assembly or one of its dependencies". Can you please help me with that?

using System;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;

namespace stringToCode
{
    public class Program
    {
        public static int q = 0;
        static void Main(string[] args)
        {
            try
            {
                string source = "namespace stringToCode { public class FooClass { public void Execute() { Program.q = 1; } } }";

                Console.WriteLine("q=" + q);
                using (var foo = new CSharpCodeProvider())
                {
                    var parameters = new CompilerParameters();
                    parameters.GenerateInMemory = true;

                    foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        try
                        {
                            string location = assembly.Location;
                            if (!String.IsNullOrEmpty(location))
                            {
                                parameters.ReferencedAssemblies.Add(location);
                            }
                        }
                        catch (NotSupportedException)
                        { }
                    }

                    var res = foo.CompileAssemblyFromSource(parameters, source);
                    var type = res.CompiledAssembly.GetType("stringToCode.FooClass");
                    var obj = Activator.CreateInstance(type);
                    var output = type.GetMethod("Execute").Invoke(obj, new object[] { });

                    Console.WriteLine("q=" + q);
                }
            }
            catch (Exception e)
            {
               Console.WriteLine(e.Message);;
            }
            Console.ReadLine();
        }
    }
}
1

There are 1 answers

0
SLI On BEST ANSWER

sorry for my question. I just understand why it happens) This is code is example. The main problem i have with code that i get from server. So when i obfuscate my vars i don't obfuscare them at my "online" code that i use with CompileAssemblyFromSource. So this just can't work. Because vars don't have same names.