C# - Load a text file as a class

后端 未结 3 1444
北恋
北恋 2021-01-05 18:34

Is there any way to load a .txt file as a class, which my main program can then call functions from? I\'m basically trying to add mod support to my simple app, where the us

3条回答
  •  旧巷少年郎
    2021-01-05 19:28

    You could load a text file and then use some Compiler-as-a-service (either Microsoft's pre-release Roslyn project or Mono.CSharp). Then you can take the compiled outputs and run them in your program.

    If it's at all possible to get your user to hand you a compiled assembly instead of an uncompiled source file, you should consider the Managed Extensibility Framework or Mono.Addins.

    The second approach (extension assemblies) seems better to me for at least two reasons: 1) your user can use whatever development environment they'd like to make sure their code compiles correctly before handing it over to you, and 2) you have less work to do to validate input and handle failures.

提交回复
热议问题