[英]C# interface in IronPython
I am trying to implement a C# interface in IronPython but am having some trouble. 我正在尝试在IronPython中实现C#接口,但遇到了一些麻烦。 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:
我之前使用另一个C#接口做到了这一点,但是遇到了另一个我无法解决如何在IronPython中进行子类化以成功使用它的方法,这里是:
The C# interface that I want to implement in IronPython: 我想在IronPython中实现的C#接口:
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 C#接口实现
using System;
namespace Accord.Genetic
{
public interface IFitnessFunction
{
double Evaluate(IChromosome chromosome);
}
}
IronPython implementation: IronPython实现:
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.
我发现此实例中的接口实际上是在Accord.Math.Random命名空间中调用其他方法,因此我去寻找这些方法。
I also ended up using a more updated version of the IRandomNumberGenerator interface, which was IRandomNumberGenerator[T] 我还最终使用了IRandomNumberGenerator接口的更新版本,即IRandomNumberGenerator [T]
Heres the C# version 继承人的C#版本
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 继承人的IronPython版本
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 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.