0

Is it possible to restrict a generic type to a class that implements a particular attribute?

If I have an attribute called MyCustomAttribute... I want to write something like this:

public class SomeRandomClass<T>
    where T : MyCustomAttribute
{
}

but not where T is a derived class of MyCustomAttribute, but a class that implements the attribute.

Thanks

edit:

I mean implemented like this:

[MyCustomAttribute]
public class SomeOtherClass
{
}
Beakie
  • 1,948
  • 3
  • 20
  • 46

1 Answers1

0

If you are looking for generic parameter constraint which will require type argument to be marked with some attribute, then its not possible.

Here is list of possible constraints: Constraints on Type Parameters (C# Programming Guide). You can add following six constraints for type argument

  1. to be a value type
  2. to be a reference type
  3. to have a public parameterless constructor
  4. be or derive from specified base class
  5. implement specified interface
  6. be or derive from other argument
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459