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

时间:2022-09-23 08:09:25

I have some extension methods I defined in my C# class, which I can import just fine into an IronPython script. However, when I attempt to call one of these methods (the "Find" method):

我有一些我在C#类中定义的扩展方法,我可以很好地导入到IronPython脚本中。但是,当我尝试调用其中一种方法时(“查找”方法):

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:

我不明白什么是错的 - 扩展方法将第一个参数(“this”参数)作为IEnumerable,这就是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:

我可以调用“stock”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. Are they simply not supported in IronPython, or are only extension methods that take a Predicate supported for some reason?

这实现了与我的Find方法完全相同的东西,但我真的希望能够使用自定义扩展方法。它们是不是在IronPython中不受支持,或者只是因某些原因而支持Predicate的扩展方法?

1 个解决方案

#1


0  

It seems you have a name conflict. .Net has it's own .Find() extension method defined. Rename yours to something else (like FindName()) and it should work.

看来你的名字有冲突。 .Net有自己定义的.Find()扩展方法。将您的名称重命名为其他内容(如FindName()),它应该可以正常工作。

#1


0  

It seems you have a name conflict. .Net has it's own .Find() extension method defined. Rename yours to something else (like FindName()) and it should work.

看来你的名字有冲突。 .Net有自己定义的.Find()扩展方法。将您的名称重命名为其他内容(如FindName()),它应该可以正常工作。