我已经使用.net v4.5在visual studio 2013中编写了这段代码.我遇到的问题是我现在不得不下载到.net v3.5并且动态关键字抛出错误,因为缺少程序集引用.在.net v3.5中是否有与"动态"相同的类型,或者我可以通过以下方式获得相同的结果?
我以为我可能在这里找到了答案,但是当我添加.Attributes或.Text属性修改时var会抛出错误.
private void CreateControl<T>(string objText,
Panel pnl,
string HTMLTag = "<td>",
string applicantID = "",
EventHandler hndl = null)
{
pnl.Controls.Add(new LiteralControl(HTMLTag));
dynamic obj = Activator.CreateInstance(typeof(T));
obj.Text = objText;
if (applicantID != string.Empty)
{
obj.Attributes.Add("ApplicantID", applicantID);
}
if (hndl != null)
{
obj.Click += new EventHandler(hndl);
}
pnl.Controls.Add(obj);
pnl.Controls.Add(new LiteralControl(HTMLTag.Insert(1, "/")));
}
Run Code Online (Sandbox Code Playgroud) 我正在做的例子可以在这里找到.所以我试图在C#脚本中运行IronPython:
蟒蛇:
def hello(name):
print "Hello " + name + "! Welcome to IronPython!"
return
def add(x, y):
print "%i + %i = %i" % (x, y, (x + y))
return
def multiply(x, y):
print "%i * %i = %i" % (x, y, (x * y))
return
Run Code Online (Sandbox Code Playgroud)
C#:
using IronPython.Hosting;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
using System;
namespace IntroIronPython
{
class IronPythonMain
{
static void Main(string[] args)
{
// Create a new ScriptRuntime for IronPython
Console.WriteLine("Loading IronPython Runtime...");
ScriptRuntime …Run Code Online (Sandbox Code Playgroud)