涉及泛型和静态方法的奇怪问题
我有许多数据类,它们共享一个抽象基类,因此我可以通用地(某种程度上)使用它们。它们每个都有一个名为 Lerp 的静态方法,我经常将其与其他几行代码一起使用。由于 DRY,我想将其重构为一个方法,但似乎没有办法这样做。我该如何解决这个问题?
有需要的话可以提供代码。
代码基本上是这样的:
XmlNode mineDataMin = mineDataMaster.SelectSingleNode("DataMinimum");
XmlNode mineDataMax = mineDataMaster.SelectSingleNode("DataMaximum");
_mineTemplate = MineInfo.Lerp(
new MineInfo(mineDataMin),
new MineInfo(mineDataMax),
_strength);
其中类 MineInfo 可以是几个类之一,所有这些类都共享一个抽象类,该抽象类用于能够通用地处理它们中的任何一个。 Lerp是一个静态方法,这就是麻烦的根源。
I have a number of data classes, which share an abstract base class so i can work with them generically (sort of). They each have a static method called Lerp, which i use frequently along with a couple of other lines. I wanted to refactor this out into a method because of DRY,but it seems there's no way to do so. How do i get around this?
Can provide code if neccessary.
The code is basically this:
XmlNode mineDataMin = mineDataMaster.SelectSingleNode("DataMinimum");
XmlNode mineDataMax = mineDataMaster.SelectSingleNode("DataMaximum");
_mineTemplate = MineInfo.Lerp(
new MineInfo(mineDataMin),
new MineInfo(mineDataMax),
_strength);
where the class MineInfo can be one of a few classes, that all share an abstract class which is used for being able to deal with any of them generically. Lerp is a static method, which is the source of the trouble.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的一种方法是对
Lerp()
函数使用委托。如果它们都共享相同的签名,那就更简单了。例如,
或者如果它们没有共同的签名,请使用具有“最大公分母”的委托作为签名来调用实际的 lerp 函数。
否则总会有反思。
One way you can do this is to use delegation for your
Lerp()
function. It would be simplest if they all share the same signature.e.g.,
Or if they don't have a common signature, use a delegate with the "largest common denominator" for a signature to call the actual lerp function.
Otherwise there's always reflection.