'(C# / Unity) pass generic type into Interface extension
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
