Monday 24 June 2019

generics - Enum flags wrapper in C#

I am refactoring some code for several enum collections that support the [Flags] attribute.



I am trying to come up with a generic class that allows for common methods (adding, removing and checking enums exist in the collection etc).



I began with this code:



public class EnumFlags
{
protected T collection;


public void Add(T value)
{
this.collection = this.collection | value;
}
}


However, I cannot use the | operator on type T. I cannot add a constraint for T to be an enum (where T : Enum is not allowed).




Any ideas to approaching this problem?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...