简体   繁体   English

无法在IronPython中使用自定义扩展方法

[英]Can't use custom extension methods with IronPython

I have some extension methods I defined in my C# class, which I can import just fine into an IronPython script. 我在C#类中定义了一些扩展方法,可以将它们很好地导入IronPython脚本中。 However, when I attempt to call one of these methods (the "Find" method): 但是,当我尝试调用以下方法之一(“查找”方法)时:

cmd.SetSpending(galaxy.Mod.Technologies.Find("Propulsion"), 100);

I get an error: "expected Predicate[Technology], found str". 我收到一个错误:“预期谓词[技术],找到了str”。

I don't understand what's wrong - the extension method takes as its first parameter (the "this" parameter) an IEnumerable, which is what galaxy.Mod.Technologies is, and as its second a string, which is what I'm passing in. I'm importing it like so: 我不明白这是怎么回事-扩展方法将IEnumerable作为其第一个参数(“ this”参数),即galaxy.Mod.Technologies,将其作为第二个字符串,即我要传递的字符串in。我将其导入为:

import FrEee;
import FrEee.Utility;
clr.ImportExtensions(FrEee.Utility.Extensions);

where FrEee.Utility.Extensions is a namespace containing CommonExtensions.cs, in which the Find method is defined. 其中FrEee.Utility.Extensions是包含CommonExtensions.cs的名称空间,在其中定义了Find方法。

I can call the "stock" System.Linq extension methods such as Single just fine: 我可以将“股票” System.Linq扩展方法称为Single很好:

techs = galaxy.Mod.Technologies;
tech = techs.Single(lambda t: t.Name == "Propulsion");

This accomplishes the exact same thing as my Find method, but I really would like to be able to use custom extension methods. 这完成了与我的Find方法完全相同的操作,但是我真的很希望能够使用自定义扩展方法。 Are they simply not supported in IronPython, or are only extension methods that take a Predicate supported for some reason? IronPython是否只是不支持它们,还是由于某些原因仅支持采用谓词的扩展方法?

It seems you have a name conflict. 看来您有名字冲突。 .Net has it's own .Find() extension method defined. .Net有自己定义的.Find()扩展方法。 Rename yours to something else (like FindName()) and it should work. 将您的名称重命名为其他名称(如FindName()),它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM