简体   繁体   中英

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. 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".

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:

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.

I can call the "stock" System.Linq extension methods such as Single just fine:

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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