0

i have multiple c# classes, they have completely different functionalities and i write a lot of them daily and i don't want to build everytime i add a class, but all classes share a function which is called Run() and it takes no parameters also the constructors never take a parameter, is it possible get a c# file from a path compile it and then making an instance of it and calling the Run() from that instance?

all i do to make a class do its job is

var x = new xclass(); //the constructor never takes a param
x.Run();

but what i want to do is

var x = CreateInstance(getClassbyPath("xxx.cs"));
x.Run();
user1492051
  • 886
  • 8
  • 22
  • 1
    Why don't you want to compile them? You cannot run C# files, you could load the compiled dlls by reflection and call the method. Have you already looked at http://msdn.microsoft.com/de-de/library/wccyzw83(v=vs.110).aspx? – ElGauchooo Mar 26 '14 at 07:32
  • 2
    you can't create a instance from cs file directly. You need to create output library (.dll) to use that class. C# allows you to create dll from your cs file on the fly. by using the newly created dll you can create the instance using reflection. – petchirajan Mar 26 '14 at 07:33
  • 1
    Check this http://www.codeproject.com/Articles/26312/Dynamic-Code-Integration-with-CodeDom – Mihai Hantea Mar 26 '14 at 07:35
  • 1
    Check out [this similar question](http://stackoverflow.com/questions/18362368/loading-dlls-at-runtime-in-c-sharp). Answers there show how to build generic loading mechanism. – default locale Mar 26 '14 at 07:36

2 Answers2

2

Take a look at the System.CodeDom namespace.

Håkan Fahlstedt
  • 2,040
  • 13
  • 17
1

Check this Dynamic-Code-Integration-with-CodeDom

Small example from the above link

class Program
{
  static void Main( string[] args )
  {
      Test1();
  }
  private static void Test1()
  {
     //
     // Create an instance of type Foo and call Print
     //
     string FooSource = @"
        class Foo
        {
           public void Print()
           {
              System.Console.WriteLine(""Hello from class Foo"");
           }
        }";

     Assembly assembly = CompileSource(FooSource);
     object myFoo = assembly.CreateInstance("Foo");
     // myFoo.Print(); // - Print not a member of System.Object
     // ((Foo)myFoo).Print(); // - Type Foo unknown
  }
}


private static Assembly CompileSource( string sourceCode )
{
   CodeDomProvider cpd = new CSharpCodeProvider();
   CompilerParameters cp = new CompilerParameters();
   cp.ReferencedAssemblies.Add("System.dll");
   cp.ReferencedAssemblies.Add("ClassLibrary1.dll");
   cp.GenerateExecutable = false;
   // Invoke compilation.
   CompilerResults cr = cpd.CompileAssemblyFromSource(cp, sourceCode);

   return cr.CompiledAssembly;
}

Update: Another way you can do it by using a plugin architecture, as starting poing check this answers. You can put the plugins in a folder and detect when new plugins are added and load and run them. But this implies to create a new dll for each of your class and implement the common interface.

Community
  • 1
  • 1
Mihai Hantea
  • 1,741
  • 13
  • 16