很多时候,很多应用都会使用到某些计算公式,而这些计算公式一段时间之后可能会调整。目前,一般处理的办法有硬编码、数据库表配置以及脚本。自ironpython发布之后,.net动态脚本的威力日增,以至于有DLR的诞生...
         以下是有关代码:(使用ironpython自带的示例文件first.py)          
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IronPython.Hosting;

namespace IronPythonIntegrationDemo
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            Console.WriteLine(
"请输入阶乘数字");
            
long num = long.Parse(Console.ReadLine());
            PythonEngine engine 
= new PythonEngine();
            engine.Import(
"Site");
            CompiledCode cc 
= engine.CompileFile(@"D:\IronPython-1.1\Tutorial\first.py");
            cc.Execute();
            engine.Execute(
string.Format("total = factorial({0})", num));
            
long total = engine.EvaluateAs<long>("total");

            Console.WriteLine(total);
            Console.Read();
        }

    }

}

         这样,可以把动态脚本引擎集成进自己的程序里。当计算公式发生变化时,代码不需要修改,而只修改脚本即可。
         我觉得还是蛮好的,起码在程序的扩展性上。
         first.py文件内容如下:
#####################################################################################
#
#
  Copyright (c) Microsoft Corporation. All rights reserved.
#
#
  This source code is subject to terms and conditions of the Shared Source License
#
  for IronPython. A copy of the license can be found in the License.html file
#
  at the root of this distribution. If you can not locate the Shared Source License
#
  for IronPython, please send an email to ironpy@microsoft.com.
#
  By using this source code in any fashion, you are agreeing to be bound by
#
  the terms of the Shared Source License for IronPython.
#
#
  You must not remove this notice, or any other, from this software.
#
#
#####################################################################################

def add(a, b):
    
"add(a, b) -> returns a + b"
    
return a + b

def factorial(n):
    
"factorial(n) -> returns factorial of n"
    
if n <= 1return 1
    
return n * factorial(n-1)

hi 
= "Hello from IronPython!"
posted on 2007-08-18 20:14  分享 共赢  阅读(564)  评论(0编辑  收藏  举报