Short question, Is there a way in .NET 4.0 to take a string that represents the method body, and compile it into a Func/Action, or is there a library to do so?
查明:
我需要的是不会造成任何损失的东西,它必须是完全有活力的,如 j(Eval())。 我需要在不造成损失的情况下,把扼杀变成一个富人/行动。
Short question, Is there a way in .NET 4.0 to take a string that represents the method body, and compile it into a Func/Action, or is there a library to do so?
查明:
我需要的是不会造成任何损失的东西,它必须是完全有活力的,如 j(Eval())。 我需要在不造成损失的情况下,把扼杀变成一个富人/行动。
您可使用CSharpCodeProvider
类别将源代码编成一个组。
例如:
var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var options = new CompilerParameters { OutputAssembly = path);
var results = compiler.CompileAssemblyFromFile(options, sourceFile);
为了汇编单一功能,你可以用适当的<代码>(<>>>>使用的表格填写单一功能,以创建完整的源文档,然后通过反思获得代表:
var assembly = results.CompiledAssembly;
var method = assembly.GetType("WrapperClassName").GetMethod("MethodName");
var delegate = (Action)Delegate.CreateDelegate(typeof(Action), method);
https://stackoverflow.com/questions/3111190/compiling-net-code-in-net-4/3111228 全例:
static readonly Assembly[] References = new[] { typeof(Enumerable).Assembly, typeof(Component).Assembly };
public Action CompileMethodstring source) {
var options = new CompilerParameters(References.Select(a => a.Location).ToArray()) {
GenerateInMemory = true
};
string fullSource = @"public static class HolderClass { public static void Execute() {
" + source + "
} }";
try {
var compiler = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var results = compiler.CompileAssemblyFromSource(options, fullSource);
if (results.Errors.Count > 0)
throw new InvalidOperationException(String.Join(
Environment.NewLine,
results.Errors.Cast<CompilerError>().Select(ce => ce.ErrorText)
));
return (Action)Delegate.CreateDelegate(
typeof(Action),
results.CompiledAssembly.GetType("HolderClass").GetMethod("Execute")
);
} finally { options.TempFiles.Delete(); }
}
也可使用CS-cast.Net。 它是根植的文字平台,也是你做的:
dynamic script = CSScript.LoadCode(@"using System;
public class Script
{
public void SayHello(string greeting)
{
Console.WriteLine(greeting);
}
}")
.CreateObject("*");
script.SayHello("Hello World!");
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...