I'm pretty close to Generics (I think).
However, I just thought that System.Enum is not easy to implement as a generic type. I have this class:
public class Button<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable { public TEnum Identifier { get; private set;
and
public abstract class AbstractInputDevice<TEnum> where TEnum : struct, IConvertible, IComparable, IFormattable { private List<Button<TEnum>> _buttons = new List<Button<TEnum>>(); public Button<TEnum> GetButton(TEnum Identifier){ foreach(Button<TEnum> button in _buttons){ if(button.Identifier == Identifier)
InputDevice might look like this:
public class Keyboard : AbstractInputDevice<KeyCode> { private void Useless(){ Button<KeyCode> = GetButton(KeyCode.A); } }
The compiler throws a compilation error right here:
if(button.Identifier == Identifier)
I believe that I can not compare these two TEnums, because in fact they are not known as Enums.
Therefore, the comparison method is not available.
I used this resource:
Create a generic method restricting T to Enum
I appreciate any better solution or fix.
(But I want to save the Enum entry as the GetButton(EnumEntry) parameter)
source share