牛刀小試:IronPython的指令碼執行功能

來源:互聯網
上載者:User
昨天和今天一直在捉摸這個IronPython(鐵蟒蛇?),早就聽說這個東西,但真的精力有限,所以一直沒有學習。最近因為要考慮為我們的應用程式添加指令碼功能,所以開始簡單試用了一下。
文法嗎,沒有什麼好說的,網上的資料滿天飛,本身也內建了非常好的教程。
我比較喜歡的功能有:
— 很好的與.NET和Com互操作;
— 可以產生靜態DLL到檔案中,建立的檔案包含PDB,因此你可以做斷點、察看變數,暈死,強;
— 使用.NET可以非常方便的使用他的引擎,網上的很多資料都舊了些,所以我自己寫了一個,下面的示範建立的代碼與你自己的程式產生了互動。 using System;
using IronPython.Hosting;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            A a = new A();
            string code = @"A.Amount = 3";

            PythonEngine engine = new PythonEngine();
            EngineModule dbafModule = engine.CreateModule("DBAF", true);
            dbafModule.Globals.Add("A", a);
            engine.Execute(code, dbafModule);

            Console.WriteLine(a.Amount);
            Console.ReadLine();
        }
    }

    public class A {

        private int _amount;

        public int Amount {
            get { return _amount; }
            set { _amount = value; }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.