是否可以编写一个C#程序集,在加载时会将方法从另一个程序集注入一个类? 如果是,注入的方法是否可以使用DLR的语言,如IronPython? namespace IronPython.Runtime { public class Bytes : IList, ICodeFormattable, IExpressionSerializable { internal byte[] _bytes; //I WANT TO INJECT THIS METHOD public byte[] getbytes() { return _bytes; } } } 我需要这种方法,如果可能的话我想避免重新编译IronPython。
我有以下C#代码将其编译为MyMath.dll程序集。 namespace MyMath { public class Arith { public Arith() {} public int Add(int x, int y) { return x + y; } } } 我有以下IronPython代码来使用此对象。 import clr clr.AddReferenceToFile(“MyMath.dll”) import MyMath arith = Arith() print arith.Add(10,20) 当我使用IronPython运行此代码时,我收到以下错误。 回溯(最近的呼叫最后): 在初始化中将文件ipycallcs,行未知 NameError:未定义名称“Arith” 可能有什么问题? 添加 arith = Arith()应该是arith = MyMath.Arith()
我有一个嵌入了IronPython的应用程序,并使用它来执行用户编写的脚本。 只安装我的应用程序时,一切都按预期工作。 我已经嵌入了IronPython 2.7.4 dll(安装后我的exe和IronPython dll在同一个文件夹中)。 但是,在某些客户端计算机上安装了IronPython 2.7.2。 它将其dll安装到GAC中,我的应用程序最终使用它们,而不是我随应用程序和应用程序一起提供的dll。 这导致我的应用程序失败,因为我使用了2.7.2中没有的属性。 问题是.NET出于某种原因认为这些程序集具有相同的版本(2.7.0.40)。 如下图所示,文件版本不同: 正确的一个是我随应用程序发货的那个,左边一个是IronPython 2.7.2附带的一个。 我没有向GAC注册任何内容,但这是在GAC中注册的内容(IronPython安装添加了它): C:\ $ gacutil /l | findstr IronPython IronPython, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL IronPython.Modules, Version=2.7.0.40, Culture=neutral, PublicKeyToken=7f709c5b713576e1, processorArchitecture=MSIL 如您所见,它们已注册为2.7.0.40版。 我的问题是 – 如何强制我的应用程序使用2.7.4.1000版本的IronPython程序集而不是在GAC中注册的2.7.2.1001? 为什么.NET忽略版本号的第三个组件并且可以更改? 编辑 : 如果它很重要,安装了IronPython 2.7.2,我的程序失败并出现以下错误: 未处理的exception:System.MissingMethodException:找不到方法:’Boolean IronPython.Hosting.PythonConsoleOptions.get_BasicConsole()’。
如何从IronPython脚本访问C#类? C#: public class MyClass { } public enum MyEnum { One, Two } var engine = Python.CreateEngine(options); var scope = engine.CreateScope(); scope.SetVariable(“t”, new MyClass()); var src = engine.CreateScriptSourceFromFile(…); src.Execute(scope); IronPython脚本: class_name = type(t).__name__ # MyClass class_module = type(t).__module__ # __builtin__ # So this supposed to work … mc = MyClass() # ??? me = […]
我目前使用此方法从Visual Studio 2010中的C#应用程序调用C#脚本文件( http://www.csscript.net ): var script = new AsmHelper(CSScript.Load( @”C:\temp\CSScriptTest\CSScriptTest\cstest.cs”)); script.Invoke(“*.LoopTest”); 但是我注意到这个性能大约是使用这种方法调用类似IronPython脚本所需的两倍: var ironPythonRuntime = Python.CreateRuntime(); dynamic loadPython = ironPythonRuntime.UseFile( @”C:\temp\IronPythonTest\IronPythonTest\pytest.py”); loadPython.LoopTest(); 有没有一种方法可以调用性能更好的C#脚本?
我有一个class级图书馆。 我可以像往常一样从铁python控制台访问该程序集。 我的目标是创建一个Silverlight类库,它使用python脚本来访问我所拥有的WPF类库。 可能吗? 有没有其他方法来实现这个或任何解决方法。 我可以提供我现在正在做的样本,如果需要更多细节。 谢谢
我想使用此代码在Visual Studio WPF应用程序中使用一些python文件。 var ipy = Python.CreateRuntime(); dynamic test = ipy.UseFile(“Test.py”); string whatever = test.pythonString() MessageBox.Show(whatever); Test.py看起来像这样: def pythonString: return “Whatever” 一切都按预期工作,但如果我添加任何导入,如 from moviepy.editor import * 对于python代码,消息框不会出现。 为什么要在Visual Studio中使用库使python工作,为什么会发生这种情况? 额外信息:我在Windows 7上使用Visual Studio 2015。 我像这样设置IronPython: https : //blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-ac-4-0-program/
当我从标准库中加载模块作为此应用程序中的源.py文件时,结束后经过的时间大约等于4秒: Stopwatch watch = Stopwatch.StartNew(); ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); ICollection paths = engine.GetSearchPaths(); paths.Add(@”C:\Users\Montgomery1944\Documents\Libs\IronPython-2.7.1\Lib”); engine.SetSearchPaths(paths); ScriptSource source = engine.CreateScriptSourceFromString( @”from distutils import dir_util import ftplib import os import tempfile import zipfile”); source.Execute(scope); watch.Stop(); Console.WriteLine(watch.Elapsed.Seconds); Console.ReadLine(); 但是当我从使用脚本编译的标准库加载模块时: import System import pyc def get_files(dir): res = [file for file in System.IO.Directory.GetFiles(dir, ‘*.py’)] dirs […]
我有一个.NET应用程序,还有一个IronPython脚本标签页。 脚本工作正常,但我非常希望能够在应用程序中调试它们并切换断点。 有这个问题的解决方案吗?
Mike Stall的博客中的这篇文章指出 最后一个表达式结果存储为全局变量”_” ,因此您可以像这样检索它: object o = m_python.GetVariable(“_”); 但目前在v2范围内执行后没有这样的变量,例如, scriptEngine.CreateScriptSourceFromString(“a=1+2;a+1”, SourceCodeKind.Statements); 有没有办法在没有显式定义变量的情况下访问结果(a+1) ?