1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        一文看懂 C# 腳本

        共 2244字,需瀏覽 5分鐘

         ·

        2021-12-29 12:41



        前言

        有些情況下,需要在程序運(yùn)行期間動(dòng)態(tài)執(zhí)行C#代碼,比如,將某些經(jīng)常改變的算法保存在配置文件中,在運(yùn)行期間從配置文件中讀取并執(zhí)行運(yùn)算。這時(shí)可以使用C#腳本來(lái)完成這些工作。

        正文

        使用C#腳本需要引用庫(kù)Microsoft.CodeAnalysis.CSharp.Scripting,下面是一些示例:最基本的用法是計(jì)算算數(shù)表達(dá)式:
        Console.Write("測(cè)試基本算數(shù)表達(dá)式:(1+2)*3/4");
        var?res?=?await?CSharpScript.EvaluateAsync("(1+2)*3/4");
        Console.WriteLine(res);

        如果需要使用比較復(fù)雜的函數(shù),可以使用WithImports引入名稱(chēng)空間:

        Console.WriteLine("測(cè)試Math函數(shù):Sqrt(2)");
        res?=?await?CSharpScript.EvaluateAsync("Sqrt(2)",?ScriptOptions.Default.WithImports("System.Math"));
        Console.WriteLine(res);

        不僅是計(jì)算函數(shù),其它函數(shù)比如IO,也是可以的:

        Console.WriteLine(@"測(cè)試輸入輸出函數(shù):Directory.GetCurrentDirectory()");
        res?=?await?CSharpScript.EvaluateAsync("Directory.GetCurrentDirectory()",
        ?????ScriptOptions.Default.WithImports("System.IO"));
        Console.WriteLine(res);

        字符串函數(shù)可以直接調(diào)用:

        Console.WriteLine(@"測(cè)試字符串函數(shù):""Hello"".Length");
        res?=?await?CSharpScript.EvaluateAsync(@"""Hello"".Length");
        Console.WriteLine(res);

        如果需要傳遞變量,可以將類(lèi)的實(shí)例作為上下文進(jìn)行傳遞,下面的例子中使用了Student類(lèi):

        Console.WriteLine(@"測(cè)試變量:");
        var?student?=?new?Student?{?Height?=?1.75M,?Weight?=?75?};
        await?CSharpScript.RunAsync("BMI=Weight/Height/Height",?globals:?student);
        Console.WriteLine(student.BMI);

        類(lèi)Student:

        public?class?Student
        {
        ????public?Decimal?Height?{?get;?set;?}
        ????public?Decimal?Weight?{?get;?set;?}
        ????public?Decimal?BMI?{?get;?set;?}
        ????public?string?Status?{?get;?set;?}?=?string.Empty;
        }

        重復(fù)使用的腳本可以復(fù)用:

        Console.WriteLine(@"測(cè)試腳本編譯復(fù)用:");
        var?scriptBMI?=?CSharpScript.Create("Weight/Height/Height",?globalsType:?typeof(Student));
        scriptBMI.Compile();

        Console.WriteLine((await?scriptBMI.RunAsync(new?Student?{?Height?=?1.72M,?Weight?=?65?})).ReturnValue);

        在腳本中也可以定義函數(shù):

        Console.WriteLine(@"測(cè)試腳本中定義函數(shù):");
        string?script1?=?"decimal?Bmi(decimal?w,decimal?h)?{?return?w/h/h;?}?return?Bmi(Weight,Height);";

        var?result?=?await?CSharpScript.EvaluateAsync<decimal>(script1,?globals:?student);
        Console.WriteLine(result);

        在腳本中也可以定義變量:

        Console.WriteLine(@"測(cè)試腳本中的變量:");
        var?script?=??CSharpScript.Create("int?x=1;");
        script?=??script.ContinueWith("int?y=1;");
        script?=??script.ContinueWith("return?x+y;");
        Console.WriteLine((await?script.RunAsync()).ReturnValue);

        完整的實(shí)例可以從github下載:https://github.com/zhenl/CSharpScriptDemo

        轉(zhuǎn)自:尋找無(wú)名的特質(zhì)

        鏈接:cnblogs.com/zhenl/p/15714453.html

        瀏覽 49
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            性ⅹxxxxxxxx毛茸茸 | 精品久久国产精品 | 国产精品无码免费一级毛住A | 一级操逼图| 一级全黄120分钟免费 | 黄色片网站下载 | 久久久国产91桃色一区二区三区 | 囯产精品久久久久久久 | 一级黄色片免费在线观看 | 天天操夜夜拍 |