Dynamic custom means (Part 1) c # in .NET

xiaoxiao2021-03-06  41

I always want to take a time to write the results of the research results before, now I can finally, .NET provides us with a lot of new, fun functions, if not, not bad. My definition of dynamic customization is: When writing programs (even in compilers), they don't have to define what they want to use, they are running when they are running, even when the 'dynamic' is generated when running. Script customization is one of them. Here is an easy-to-use script custom method for .NET, which takes advantage of the dynamic compilation function provided by .NET Framework, and the amount of code is also very small. The efficiency is not low. For example (I will not give example, just look at it, don't care too much) I realize a container MYCONTAINER loaded with myclass, I consider that I need to limit myclass's method mycontainer.add, such as Myclass A, can not Add myclass b to MyContainer. If I hardly encode, if the limit changes (such as A, B can coexist, and a, c can't coexist), then my code must change (in fact, it is to change a few lines of code and recompilate it. Troubled), but first programs can no longer run again, must be restarted. But if you use the script to check, you can monitor the script file, if the script changes, recompile the script, dynamically change the program, then your program does not have to restart, no need to re-need Compilation (but you must make a choice, the content in the container is not necessarily 'security', because the limit is changed, the past is correct now, the container is at least emptied). So how do you customize this problem? My program is as follows: (Borrowing from NETRON Graphlib) First, define an interface iscript,

public interface IScript {void Intialise (MyContainer m); bool Check (MyObject o);} in MyContainer.Add added followed by a few lines of code public int Add (MyObject o) {CodeDomProvider provider = null; provider = new Microsoft.CSharp. CsharpcodeProvider (); //. NET At least provides C # and VB.NET compiler, / / ​​may have a third party, I don't know.

// This part can be used FactoryMethod // I just demonstrates the direct use of the C # ICodeCompiler compiler = provider.CreateCompiler (); // get the compiler CompilerParameters parms = new CompilerParameters (); CompilerResults results = null; // compiler configuration parameters parms .MainClass = "Script"; parms.GenerateExecutable = false; parms.GenerateInMemory = true; parms.TempFiles = new TempFileCollection (Path.GetTempPath (), false); parms.IncludeDebugInformation = false; parms.ReferencedAssemblies.Add ( "System. dll "); // add references foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies ()) {parms.ReferencedAssemblies.Add (asm.Location);} TextReader reader = File.OpenText (" CheckScript.cs "); // Of course, you can read the configuration, or monitor, I don't write String Source = Reader.ReadToend (); results = compiler.compileassemblyFrom Source (parms, Source); Assembly ass = results.CompiledAssembly; foreach (Type type in ass.GetTypes ()) {if (type is IScripter) {IScript Checker = ass.CreateInstance (type.FullName); Checker.Intialise (this ); If (! Checker.check (o)) return; // Divided me, don't write more beautiful}} // code part} script examples (Checkscript.cs) Using System; use system. Diagnostics; namespace mynamespace.scripts {public class script: iscript, system.idisposable {private static int counter = 0;

// counter MyContainer M; public void initialize (mycontainer m) {this.m = m;} public bool check (myObject o) {// With the code you like, be sure to return! Otherwise it will be abnormal! } #Region "IDisposable importation" public virtual void dispose () {Dispose (true); system.gc.suppressFinalize (this);} public virtual void dispose (Bool Disposing) {if ((Disposing) {

// free Other state (managed objects).} // free your owlds. // set large fields to null.

~ Script () {// simply call dispose (false);} #ENDREGON}

} All, but twenty lines, but also more practical, as long as the design is reasonable, only the script is compiled when loading, and the rest of the normal program is not numbed, and if it is designed Ok, you can dynamically support multiple scripts, plus the compilation function provided by the third party, it is so convenient. It is recommended to use the place where it is especially easy to change the rules. The disadvantage is that the underlying code can be seen directly by others. If the modification of the script is not appropriate (especially if there is a modification of the heart), the program is easy to collapse. So it is recommended to use only to customize restrictions and check. Don't use simple and changeable services. As for the dynamic customization, the next reply.

转载请注明原文地址:https://www.9cbs.com/read-52522.html

New Post(0)