C# 4.0 Dynamic --通过DLR直接调用IronPython

本文假设你己安装了VS2010 Beta2和 IronPython 2.6 for .net 4.0 Beta2, 首先创建一个Console App并引用如下Dll (可以在你的IronPython安装目录中找到)

  IronPython.dll

  IronPython.Modules.dll

  Microsoft.Dynamic.dll

  Microsoft.Scripting.dll

  然后用下面的代码替换掉默认生成的代码 

using System;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
namespace Decorator.Try
{
    class Program
    {
        static void Main(string[] args)
        {
            Action action = () => Console.WriteLine("Hello World");
            ScriptRuntime runtime = Python.CreateRuntime();
            dynamic script = runtime.UseFile("script.py");           
            dynamic decorator = script.decorator(action);
            decorator.execute();
        }       
    }
}

  在创建的工程的bin/debug目录下,添加script.py.代码如下,一个很简单的wrapper,仅供验证之用。

def wrapper(function):
    def inner(self):
        print "Decorator... "
        return function(self)
    return inner
class decorator(object):
    def __init__(self, function):
        self.action = function
    @wrapper
    def execute(self):
        self.action()

  运行Console app。输出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值