Generic constraint violation at runtime?
I'm getting the following exception while trying to create a new instance of a class that heavily relies on generics:

    System.TypeLoadException

    GenericArguments[0], 'TOutPacket', on     
    'Library.Net.Relay`4[TInPacket,TOutPacket,TCryptograph,TEndian]' 
    violates the constraint of type parameter 'TInPacket'.

I'm puzzled as to why this happens. Aren't generic constraints checked at compile time? (and my code compiles)

My googling brought me to the conclusion that this has something to do with either of these causes, or (sometimes?) both:

- The order in which the generic constraints (`where`) are defined in the classes;
- The use of the self-referencing generic pattern (conter-intuitive but very legal, see [Eric Lippert's blog post][1])

One thing I am not ready to sacrifice is the self-referencing pattern. I absolutely need it for a specific purpose.

However, I'd like some help to point out where and why this problem occurs. As the library is massive and makes huge generic patterns, I think it would be best to progressively give code bits on request.
Generic constraint violation at runtime?
===

I'm getting the following exception while trying to create a new instance of a class that heavily relies on generics:

    System.TypeLoadException

    GenericArguments[0], 'TOutPacket', on     
    'Library.Net.Relay`4[TInPacket,TOutPacket,TCryptograph,TEndian]' 
    violates the constraint of type parameter 'TInPacket'.

I'm puzzled as to why this happens. Aren't generic constraints checked at compile time? (and my code compiles)

My googling brought me to the conclusion that this has something to do with either of these causes, or (sometimes?) both:

- The order in which the generic constraints (`where`) are defined in the classes;
- The use of the self-referencing generic pattern (conter-intuitive but very legal, see [Eric Lippert's blog post][1])

One thing I am not ready to sacrifice is the self-referencing pattern. I absolutely need it for a specific purpose.

However, I'd like some help to point out where and why this problem occurs. As the library is massive and makes huge generic patterns, I think it would be best to progressively give code bits on request.