1

Sorry I'm new at programming using CodeDom, I have this problem that I couldn't solve. Was thinking if some of you folks know the solution.

I have the following code inside a class file:

public class AnotherCustomClass {
    public string Employer { get; set; }
    public DateTime? DateOfHire { get; set; }
}

public class CustomClass {
    public int X { get; set; }
    public int Y { get; set; }
    public AnotherCustomClass[] YetAnother { get; set; }
}

public void DoSomething()
{
    string expression = @"using System;  
    namespace MyNamespace {
        public class MyClass {
            public static int DoStuff(CustomClass myCustomClass) {
                return myCustomClass.X + myCustomClass.Y;
            }
        }
    }";

    CSharpCodeProvider provider = new CSharpCodeProvider();
    CompilerParameters assemblies = new CompilerParameters(new[] { "System.Core.dll" });
    CompilerResults results = provider.CompileAssemblyFromSource(assemblies, expression);
    Type temporaryClass = results.CompiledAssembly.GetType("MyNamespace.MyClass");
    MethodInfo temporaryFunction = temporaryClass.GetMethod("DoStuff");
    CustomClass data = new CustomClass() { X = 1, Y = 2 };
    object result = temporaryFunction.Invoke(null, new object[] { data });
    return result;
}

I would like to input the data (a custom created class, that has an array of another custom class inside) variable as the parameter in the DoStuff function, I keep on having errors. Is there a way to solve this issue?

  • 2
    Sorry, you question is not clear. Please explain what you want to do with it. – Siva Gopal Nov 18 '15 at 08:33
  • ok so you want to compile this? thats the question? add some extra information. for example where is `CustomClass` and where you have stucked? – M.kazem Akhgary Nov 18 '15 at 08:35
  • Possible duplicate of [Is it possible to dynamically compile and execute C# code fragments?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments) – Siva Gopal Nov 18 '15 at 08:36
  • Thanks a lot guys. I just updated my question. I want to know how the CodeDom can interpret the CustomClass. – Totoy Maskulado Nov 18 '15 at 08:42
  • Which errors do you get? At which position in your code are the errors raised? – Markus Nov 18 '15 at 08:45
  • @Markus, got the error on the declaration of Type temporaryClass. This is the error: An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: Could not load file or assembly 'file:///C:\Users\xxxxx\AppData\Local\Temp\3abopb1p.dll' or one of its dependencies. The system cannot find the file specifie – Totoy Maskulado Nov 18 '15 at 08:50
  • You cant. the assemblies are not same. you have to create a library. which is `dll` that contains the classes you need. and then put the compiled library behind your program and add the reference to assembly. like behind this. `new[] { "System.Core.dll" , "YourLibrary.dll" }`. also you need to add `using` statement to import the namespace. just in your string code `using System; using LibraryNamespace; ` – M.kazem Akhgary Nov 19 '15 at 00:38
  • @M.kazemAkhgary, you're right! I did exactly what you stated. It works fine now. Can you answer it so It can be marked as the correct answer? Thank you very much! :3 – Totoy Maskulado Nov 19 '15 at 03:07

1 Answers1

0

Multiple errors, change this way:

string expression = @"using System;  
namespace MyNamespace {
    public class MyClass {
        public static int DoStuff(CustomClass myCustomClass) {
            return myCustomClass.X + myCustomClass.Y;
        }
    }

    public class CustomClass
    {
        public int X;
        public int Y;
        public CustomClass(int x, int y) { X = x; Y = y; }
    }
}";

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters assemblies = new CompilerParameters(new string[] { "System.dll" });
CompilerResults results = provider.CompileAssemblyFromSource(assemblies, expression);
Type temporaryClass = results.CompiledAssembly.GetType("MyNamespace.MyClass");
Type parClass = results.CompiledAssembly.GetType("MyNamespace.CustomClass");
MethodInfo temporaryFunction = temporaryClass.GetMethod("DoStuff");
object mc = parClass.GetConstructor(new Type[] { typeof(int), typeof(int) }).Invoke(new object[]{1,2});
object result = temporaryFunction.Invoke(null, new object[] { mc });
owairc
  • 1,890
  • 1
  • 10
  • 9
  • I think in this answer, you created new "class" during runtime. Am I correct? I need to input the class, not create it during runtime. Though this may help. I updated my question again, thanks for your response! – Totoy Maskulado Nov 19 '15 at 00:31
  • Then you have to add a reference to the dll containing your customclass when compiling. Just like system.dll, add myclass.dll (and remove che declaration from Realtime compiled code) – owairc Nov 19 '15 at 09:14