C # - load a text file as a class

Is there a way to load a .txt file as a class from which my main program can call functions? I am basically trying to add mod support to my simple application where the user can select options from each file. The file follows a similar (but not the same) format with a bunch of voids (functions) called in the main program.

How can i do this? Is it possible to do this (maybe not, answering my own question?), Given that it will not be compiled with the rest of the program?

Thank!

+5
source share
3 answers

, : # ?

, CodeDom . .

  • CodeDom
  • /
  • , Linq , ( )

var csc = new CSharpCodeProvider( new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } } );
var cp = new CompilerParameters() {
    GenerateExecutable = false,
    OutputAssembly = outputAssemblyName,
    GenerateInMemory = true
};

cp.ReferencedAssemblies.Add( "mscorlib.dll" );
cp.ReferencedAssemblies.Add( "System.dll" );

StringBuilder sb = new StringBuilder();

// The string can contain any valid c# code

sb.Append( "namespace Foo{" );
sb.Append( "using System;" );
sb.Append( "public static class MyClass{");
sb.Append( "}}" );

// "results" will usually contain very detailed error messages
var results = csc.CompileAssemblyFromSource( cp, sb.ToString() );

. : #. IronPython DLR .

+6

Framework,

Managed Extensibility Framework - ,

http://mef.codeplex.com/

Framework (MEF) .NET, , . MEF .

MEF .

DLL ( )

The plugin developer is expected to provide a DLL containing the plugin code. However, if necessary, you can compile the source code into a DLL using Roslyn . Keep in mind that if you want to support getting the source code of a plugin, you also need to provide support to help the user deal with compiler errors and debug the plugin inside your structure. It might be wiser to require them to provide a DLL.

+1
source

All Articles