涉及泛型和静态方法的奇怪问题

发布于 2024-09-11 21:12:51 字数 578 浏览 3 评论 0原文

我有许多数据类,它们共享一个抽象基类,因此我可以通用地(某种程度上)使用它们。它们每个都有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名的好友 2024-09-18 21:12:51

实现此目的的一种方法是对 Lerp() 函数使用委托。如果它们都共享相同的签名,那就更简单了。

例如,

public static Template CreateTemplate<T>( ... , Func<T, T, int, Template> lerp)
    where T : CommonClass
{
    XmlNode mineDataMin = mineDataMaster.SelectSingleNode("DataMinimum");
    XmlNode mineDataMax = mineDataMaster.SelectSingleNode("DataMaximum");
    return lerp(new T(mineDataMin), new T(mineDataMax), _strength);
}

_template = CreateTemplate( ... , MineInfo.Lerp);

或者如果它们没有共同的签名,请使用具有“最大公分母”的委托作为签名来调用实际的 lerp 函数。

_template = CreateTemplate( ... ,
    (min, max, strength) =>
    {
        return SomeOtherInfoInfo.Lerp(min, max); //doesn't use strength
    });

否则总会有反思。

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

public static Template CreateTemplate<T>( ... , Func<T, T, int, Template> lerp)
    where T : CommonClass
{
    XmlNode mineDataMin = mineDataMaster.SelectSingleNode("DataMinimum");
    XmlNode mineDataMax = mineDataMaster.SelectSingleNode("DataMaximum");
    return lerp(new T(mineDataMin), new T(mineDataMax), _strength);
}

_template = CreateTemplate( ... , MineInfo.Lerp);

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.

_template = CreateTemplate( ... ,
    (min, max, strength) =>
    {
        return SomeOtherInfoInfo.Lerp(min, max); //doesn't use strength
    });

Otherwise there's always reflection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文