What you want to achieve is possible, but BEWARE !! Each time you compile code, it compiles as an assembly and is loaded into memory. If you change the "script" code and recompile it, it will be loaded again as another assembly. This can cause a “memory leak” (although this is not a real leak), and there is no way to unload these unused assemblies.
The only solution is to create another AppDomain and load this assembly into this AppDomain, and then unload if the code changes and do it again. But it’s harder to do.
UPDATE
For compilation, see here: http://support.microsoft.com/kb/304655
Then you will need to load the assembly using Assembly.LoadFrom .
// assuming the assembly has only ONE class // implementing the interface and method is void private static void CallDoSomething(string assemblyPath, Type interfaceType, string methodName, object[] parameters) { Assembly assembly = Assembly.LoadFrom(assemblyPath); Type t = assembly.GetTypes().Where(x=>x.GetInterfaces().Count(y=>y==interfaceType)>0).FirstOrDefault(); if (t == null) { throw new ApplicationException("No type implements this interface"); } MethodInfo mi = t.GetMethods().Where(x => x.Name == methodName).FirstOrDefault(); if (mi == null) { throw new ApplicationException("No such method"); } mi.Invoke(Activator.CreateInstance(t), parameters); }
source share