(C# / Unity) 将泛型类型传递到接口扩展中
我正在尝试创建一个接口来处理不同类型的输入,其中一个输入类 (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论