第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

.NET是否可以在運(yùn)行時(shí)編譯和執(zhí)行新代碼?

.NET是否可以在運(yùn)行時(shí)編譯和執(zhí)行新代碼?

阿晨1998 2019-10-29 12:58:06
注意:數(shù)學(xué)表達(dá)式評(píng)估不是此問題的重點(diǎn)。我想在.NET運(yùn)行時(shí)編譯并執(zhí)行新代碼。 話雖如此...我想允許用戶在文本框中輸入以下任何方程式:x = x / 2 * 0.07914x = x^2 / 5并將該等式應(yīng)用于輸入數(shù)據(jù)點(diǎn)。輸入的數(shù)據(jù)點(diǎn)由x表示,每個(gè)數(shù)據(jù)點(diǎn)由用戶指定的方程式處理。我?guī)啄昵熬瓦@樣做了,但是我不喜歡該解決方案,因?yàn)樗枰獮槊看斡?jì)算都解析方程的文本:float ApplyEquation (string equation, float dataPoint){    // parse the equation string and figure out how to do the math    // lots of messy code here...}當(dāng)您處理大量數(shù)據(jù)點(diǎn)時(shí),這會(huì)帶來很多開銷。我希望能夠即時(shí)將方程式轉(zhuǎn)換為一個(gè)函數(shù),以便僅將其解析一次。它看起來像這樣:FunctionPointer foo = ConvertEquationToCode(equation);....x = foo(x);  // I could then apply the equation to my incoming data like this函數(shù)ConvertEquationToCode將解析方程式,并返回一個(gè)指向應(yīng)用適當(dāng)數(shù)學(xué)的函數(shù)的指針。該應(yīng)用程序基本上將在運(yùn)行時(shí)編寫新代碼。.NET有可能嗎?
查看完整描述

3 回答

?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

是! 使用在Microsoft.CSharp,System.CodeDom.Compiler和System.Reflection命名空間中找到的方法。這是一個(gè)簡(jiǎn)單的控制臺(tái)應(yīng)用程序,它使用一個(gè)方法(“ Add42”)編譯一個(gè)類(“ SomeClass”),然后允許您調(diào)用該方法。這是一個(gè)簡(jiǎn)單的示例,我對(duì)其進(jìn)行了格式化,以防止?jié)L動(dòng)條出現(xiàn)在代碼顯示中。這只是為了演示在運(yùn)行時(shí)編譯和使用新代碼。


using Microsoft.CSharp;

using System;

using System.CodeDom.Compiler;

using System.Reflection;


namespace RuntimeCompilationTest {

    class Program

    {

        static void Main(string[] args) {

            string sourceCode = @"

                public class SomeClass {

                    public int Add42 (int parameter) {

                        return parameter += 42;

                    }

                }";

            var compParms = new CompilerParameters{

                GenerateExecutable = false, 

                GenerateInMemory = true

            };

            var csProvider = new CSharpCodeProvider();

            CompilerResults compilerResults = 

                csProvider.CompileAssemblyFromSource(compParms, sourceCode);

            object typeInstance = 

                compilerResults.CompiledAssembly.CreateInstance("SomeClass");

            MethodInfo mi = typeInstance.GetType().GetMethod("Add42");

            int methodOutput = 

                (int)mi.Invoke(typeInstance, new object[] { 1 }); 

            Console.WriteLine(methodOutput);

            Console.ReadLine();

        }

    }

}


查看完整回答
反對(duì) 回復(fù) 2019-10-29
  • 3 回答
  • 0 關(guān)注
  • 560 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)