English 中文(简体)
c 动态说明
原标题:c# and dynamic string scripting

我对我如何执行我正在从事的项目的关键部分抱着一些想法。 主要是数据制图,在这里,我复制了实地X并将其纳入实地。 然而,在过渡阶段需要一定的能力来积极改变这种价值(利用操纵)。

我所希望的是文本箱,用户可以进入文字,允许他们使用描述性语言(最好是VB文本)修改这一价值。 这样,他们就能够简单地操纵这一榜样,而这个例子将包含以下内容:

Mid({input_value}, 2, 4)

不适用

{input_value} 

将由操作时间的实际价值取代。

因此,例如,如果“地面X”的投入是“试验”,并且使用上述开端=2和长度=4的范例,则“现场”所节省的价值将是“他的”

我知道,我如何能够从C#号的文字中操作VB,这不是一个问题。 然而,能否在运行时间操作和评价上文提到的管道,并将产出重新记录为C#变量?

否则,没有人对我如何处理这一问题提出建议?

Many thanks

最佳回答

您不妨研究一个DLR-based 语文,例如铁蓬松或铁鲁比。 这两部法律都允许植树,Michael Foord拥有一个tutorial

If you use the standard DLR interfaces I believe you can embed any language including DLRBasic and the ASP Classic Compiler. Ben Hall has an article on IronRuby embedding in a production application for Red Gate.

我认为,你需要审查下文所示的“可变(Seta)”和“可变(GetVariable)()”方法,例如从文字上确定和返还数据:

public string evaluate(string x, string code)
{
    scope.SetVariable("x", x);
    scope.SetVariable("button", this.button);

    try
    {
        ScriptSource source = engine.CreateScriptSourceFromString(code,
            SourceCodeKind.Statements);

        source.Execute(scope);
    }
    catch (Exception ex)
    {
        return "Error executing code: " + ex.ToString();
    }

    if (!scope.VariableExists("x"))
    {
        return "x was deleted";
    }
    string result = scope.GetVariable<object>("x").ToString();
    return result;
}

这个例子取自http://www.avespace.org.uk/ironpython/dlr_hosting.shtml” rel=“nofollow”http://www.avespace.org.uk/ironpython/dlr_hosting.shtml

问题回答

这里是使用时间汇编表述的一种工作例子。 我借阅了这个概念和大多数法典

static void Main(string[] args)
{
    string input = "This is a test";
    string method = "Mid(x, 2, 4)";  //  x  represents the input value
    string output = Convert(method, input);
    Console.WriteLine("Result: " + output);
    Console.ReadLine();
}

// Convert input using given vbscript logic and return as output string
static string Convert(string vbscript, string input)
{
    var func = GetFunction(vbscript);
    return func(input);
}

// Create a function from a string of vbscript that can be applied
static Func<string, string> GetFunction(string vbscript)
{
    // generate simple code snippet to evaluate expression
    VBCodeProvider prov = new VBCodeProvider();
    CompilerResults results = prov.CompileAssemblyFromSource(
        new CompilerParameters(new[] { "System.Core.dll" }),
        @"
Imports System
Imports System.Linq.Expressions
Imports Microsoft.VisualBasic

Class MyConverter

Public Shared Function Convert() As Expression(Of Func(Of String, String))
    return Function(x) " + vbscript + @"
End Function

End Class
"
        );

    // make sure no errors occurred in the conversion process
    if (results.Errors.Count == 0)
    {
        // retrieve the newly prepared function by executing the code
        var expr = (Expression<Func<string, string>>)
            results.CompiledAssembly.GetType("MyConverter")
                .GetMethod("Convert").Invoke(null, null);
        Func<string, string> func = expr.Compile();

        // create a compiled function ready to apply and return
        return func;
    }
    else
    {
        return null;
    }
}




相关问题
Anyone feel like passing it forward?

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. ...

NSArray s, Primitive types and Boxing Oh My!

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 ...

C# Marshal / Pinvoke CBitmap?

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 ...

How to Use Ghostscript DLL to convert PDF to PDF/A

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, ...

Linqy no matchy

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. ...

热门标签