简体   繁体   中英

C# interface in IronPython

I am trying to implement a C# interface in IronPython but am having some trouble. I've done this before with another C# interface but have come across another one which I've not been able to resolve how to subclass in IronPython to use it successfully, here it is:

The C# interface that I want to implement in IronPython:

using System;
namespace Accord.Math.Random

{
    public interface IRandomNumberGenerator
    {
        float Mean
        {
            get;
        }
        float Variance
        {
            get;
        }
        float Next();
        void SetSeed(int seed);
    }
}

Here is what I have done before successfully in the past:

C# interface to implement

using System;
namespace Accord.Genetic
{
    public interface IFitnessFunction
    {
        double Evaluate(IChromosome chromosome);
    }
}

IronPython implementation:

class FitnessFunction(AG.IFitnessFunction):
    def Evaluate(self, chromosome):
        #some fitness calculation using chromosome
        return Fitness

Any help would be so much appreciated!

I sorted it out! I figured that the interface in this instance was actually calling some other methods in the Accord.Math.Random namespace, so I went looking for which methods these were.

I also ended up using a more updated version of the IRandomNumberGenerator interface, which was IRandomNumberGenerator[T]

Heres the C# version

using System;
namespace Accord.Math.Random
{
    public interface IRandomNumberGenerator<T>
    {
        T[] Generate(int samples);
        T[] Generate(int samples, T[] result);
        T Generate();
    }
}

Heres the working IronPython version

class RandomNumberGenerator(AM.Random.IRandomNumberGenerator[System.Double]):
    def __init__(self):
        self.actual = AM.Random.ZigguratUniformOneGenerator()
    def Generate(self):
        return self.actual.Generate()
    def Generate(self, samples):
        return self.actual.Generate(samples)
    def Generate(self, samples, result):
        return self.actual.Generate(samples, result)

I've so far been able to use it for my purposes. If anyone notices any issues in the code or knows of a more proper implementation, please post!

Cheers

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