-1

I have a method that takes an Enum value as a parameter, but not all enums are valid. I want to do something like this

public void Method<T>(T type) where T : Enum, IValidEnum{}

public enum ValidEnum1 : IValidEnum{}
public enum ValidEnum2 : IValidEnum{}
public enum NotValidEnum{}

Method(ValidEnum1.Value) // ok
Method(ValidEnum2.Value) // ok
Method(NotValidEnum.Value) // Exeption

but enums cannot use inheritance. Any advice, please

korjek
  • 1
  • 1
  • 1
    You can use the built-in [Enum.IsDefined](https://learn.microsoft.com/en-us/dotnet/api/system.enum.isdefined?redirectedfrom=MSDN&view=net-7.0#System_Enum_IsDefined_System_Type_System_Object_) method. Enums are essentially aliases for integral types. An `int` based enum is still an int, there's no inheritance involved – Panagiotis Kanavos Apr 10 '23 at 14:07
  • What do you mean by "valid" and "invalid"? – Julian Apr 10 '23 at 14:09
  • If you need to constrain to a specific type, then do not use generics at all, but use that specific enum type. If you are using `[Flags]` enums then you might be able to validate using bitmasks. If the dup doesn't help you then you will need to explain more about the scenario and show the possible variations that you want to support and those that you want to prevent. – Chris Schaller Apr 10 '23 at 14:16
  • The edit didn't clarify anything. Enums are named values, not types. You can't specify that `ValidEnum1` and `ValidEnum2` contain all the values of `ValidEnum`. You'll have to enter these manually. If `ValidEnum` is a subset of both `ValidEnum1` and `ValidEnum2` you can use `Enum.IsDefined()` with *any* value. – Panagiotis Kanavos Apr 10 '23 at 14:36
  • 1
    You cannot declare a method that takes an open-ended number of enums that must still be from a limited set (as you can with classes). Depending on your scenario you can use overloads, or validate that `T` is an enum you are willing to accept at runtime (for example, by checking its originating assembly or a custom attribute defined on the enum). Because enums are themselves fairly weak syntactic sugar over integers, and nothing prevents you from casting any integral value to any enum, the usefulness of such things is limited. – Jeroen Mostert Apr 10 '23 at 14:48
  • c# doesn't support interfaces for enums. You can add attributes, but [there is no way to add use an attribute as a generic constraint](https://stackoverflow.com/q/4146674). See [Constraints on type parameters (C# Programming Guide)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters). For some workarounds see [Behaviour to simulate an enum implementing an interface](https://stackoverflow.com/q/3007596). – dbc Apr 10 '23 at 16:28
  • For fast generic solutions you can use [this](https://www.nuget.org/packages/KGySoft.CoreLibraries) library (disclaimer: written by me). It offers a couple of extension methods that can be used like `mySimpleEnum.IsDedined()` or `myFlagsEnum.AllFlagsDefined()`. See an online example [here](https://dotnetfiddle.net/PITQFg). – György Kőszeg Apr 20 '23 at 08:36

1 Answers1

-1

You are trying to do processing based on values coming from multiple different enum types? I think of each enum type defined in code as being an exhaustive distinct set for what that enum is for. You can’t have the same value twice in an enum, but you could have the same value from 2 separate enum s and this is what breaks the way you are trying to use them with inheritance. There is nothing actually being inherited, nor is there any spec being implemented.

Perhaps some more context of what you are trying to do would help. Are these preexisting enums you are trying to incorporate into new app logic?