Monday, June 24, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...