(C# / Unity) 将泛型类型传递到接口扩展中

发布于 2025-01-13 14:19:43 字数 1747 浏览 3 评论 0原文

我正在尝试创建一个接口来处理不同类型的输入,其中一个输入类 (InputValue) 无法修改,而另一个输入类 (GenericInputValue) 可以具有不同的输入类型值类型

本质上我需要传递 GenericInputValue 作为 IInputHandler 的类型(注意:不一样)

请参阅下面代码中的这一行:

public class JoystickInputHandler : MonoBehaviour, IInputHandler<GenericInputValue<???>>

我该如何替换 ???

public interface IInputHandler<in T>
{
    void OnMove(T value);
    void OnButton(T value);
}

// handles standard `InputValue` from Unity Input System
public class NormalInputHandler : MonoBehaviour : IInputHandler<InputValue>
{
    public void OnMove(InputValue value) {}
    public void OnButton(InputValue value) {}
}

// handles generic input values with different types
public class JoystickInputHandler : MonoBehaviour, IInputHandler<GenericInputValue<???>>
{
    // note: each method has a different value type
    public void OnMove(GenericInputValue<Vector2> value) {}
    public void OnButton(GenericInputValue<bool> value) {}
}

// stores a generic data value without object boxing
public class GenericInputValue<T>()
{
    private T value;
    public GenericInputValue(T value) { this.value = value };
    public T get() { return this.value; }
} 

如果我只能使用 IInputHandler ,那么 GenericInputValue 将需要装箱/取消装箱 object 来存储不同类型的数据,我'我试图避免 例如

public class GenericInputValue
{
    private object value;
    public GenericInputValue(object value) { this.value = value; }
    public T Get<T>() { return (T)this.value; }
}

谢谢

I'm trying to create an interface to handle different types of input where one input class (InputValue) cannot be modified and the other (GenericInputValue<T>) can have different value types

essentially I need to pass the GenericInputValue<T> as the type for IInputHandler<T> (note: not the same <T>)

refer to this line in the code below:

public class JoystickInputHandler : MonoBehaviour, IInputHandler<GenericInputValue<???>>

what do i replace the ??? with?

public interface IInputHandler<in T>
{
    void OnMove(T value);
    void OnButton(T value);
}

// handles standard `InputValue` from Unity Input System
public class NormalInputHandler : MonoBehaviour : IInputHandler<InputValue>
{
    public void OnMove(InputValue value) {}
    public void OnButton(InputValue value) {}
}

// handles generic input values with different types
public class JoystickInputHandler : MonoBehaviour, IInputHandler<GenericInputValue<???>>
{
    // note: each method has a different value type
    public void OnMove(GenericInputValue<Vector2> value) {}
    public void OnButton(GenericInputValue<bool> value) {}
}

// stores a generic data value without object boxing
public class GenericInputValue<T>()
{
    private T value;
    public GenericInputValue(T value) { this.value = value };
    public T get() { return this.value; }
} 

if I can only use IInputHandler<GenericInputValue> then GenericInputValue will need to box/unbox an object to store different types of data, which I'm trying to avoid
eg

public class GenericInputValue
{
    private object value;
    public GenericInputValue(object value) { this.value = value; }
    public T Get<T>() { return (T)this.value; }
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文