しばらくの間、ほぼコンパイル時の制約について考えてきたので、これはコンセプトを立ち上げる絶好の機会です。
基本的な考え方は、チェックコンパイル時間を実行できない場合は、できるだけ早い時期に実行する必要があるということです。これは、基本的にアプリケーションが起動した瞬間です。すべてのチェックに問題がなければ、アプリケーションは実行されます。チェックが失敗した場合、アプリケーションは即座に失敗します。
動作
最良の結果は、制約が満たされない場合、プログラムがコンパイルされないことです。残念ながら、現在のC#実装ではそれは不可能です。
次善の策は、プログラムが起動した瞬間にクラッシュすることです。 
最後のオプションは、コードがヒットした瞬間にプログラムがクラッシュすることです。これは.NETのデフォルトの動作です。私にとって、これはまったく受け入れられません。
前提条件
制約メカニズムが必要なので、何もない場合は、属性を使用してみましょう。属性は、一般的な制約の上に表示され、条件に一致するかどうかを確認します。そうでない場合は、醜いエラーが発生します。
これにより、コードで次のようなことができるようになります。
public class Clas<[IsInterface] T> where T : class
(私はwhere T:class常に実行時チェックよりもコンパイル時チェックを好むので、ここを保持しました)
そのため、使用するすべての型が制約に一致するかどうかを確認するという1つの問題のみが残ります。それはどれほど難しいでしょうか?
それを分解しましょう
ジェネリック型は、常にクラス(/ struct / interface)またはメソッド上にあります。
制約をトリガーするには、次のいずれかを実行する必要があります。
- コンパイル時、型で型を使用する場合(継承、ジェネリック制約、クラスメンバー)
- メソッド本体で型を使用する場合のコンパイル時
- 実行時、リフレクションを使用して汎用基本クラスに基づいて何かを構築する場合。
- 実行時、リフレクションを使用してRTTIに基づいて何かを構築する場合。
この時点で、プログラムIMOで(4)を常に実行することは避けてください。とにかく、停止の問題を効果的に解決することになるので、これらのチェックはそれをサポートしません。
ケース1:タイプを使用する
例:
public class TestClass : SomeClass<IMyInterface> { ... } 
例2:
public class TestClass 
{ 
    SomeClass<IMyInterface> myMember; // or a property, method, etc.
} 
基本的に、これにはすべてのタイプ、継承、メンバー、パラメーターなどのスキャンが含まれます。タイプがジェネリックタイプであり、制約がある場合は、制約をチェックします。配列の場合は、要素タイプをチェックします。
この時点で、デフォルトで.NETがタイプ「レイジー」をロードするという事実を打破することを付け加えなければなりません。すべてのタイプをスキャンすることにより、.NETランタイムにそれらすべてを強制的にロードさせます。ほとんどのプログラムでは、これは問題になりません。それでも、コードで静的イニシャライザを使用すると、このアプローチで問題が発生する可能性があります...とは言っても、とにかくこれを行うことは誰にもお勧めしません(このようなものを除いて:-)。あなたには多くの問題があります。
ケース2:メソッドでの型の使用
例:
void Test() {
    new SomeClass<ISomeInterface>();
}
これをチェックするには、1つのオプションしかありません。クラスを逆コンパイルし、使用されているすべてのメンバートークンをチェックし、そのうちの1つがジェネリック型であるかどうか-引数をチェックします。
ケース3:リフレクション、ランタイムのジェネリック構造
例:
typeof(CtorTest<>).MakeGenericType(typeof(IMyInterface))
ケース(2)と同じようなトリックでこれをチェックすることは理論的には可能だと思いますが、その実装ははるかに困難です(MakeGenericTypeコードパスで呼び出されるかどうかをチェックする必要があります)。ここでは詳しく説明しません...
ケース4:リフレクション、ランタイムRTTI
例:
Type t = Type.GetType("CtorTest`1[IMyInterface]");
これは最悪のシナリオであり、前に説明したように、一般的に悪い考えである私見です。いずれにしても、チェックを使用してこれを理解する実用的な方法はありません。
ロットのテスト
ケース(1)と(2)をテストするプログラムを作成すると、次のような結果になります。
[AttributeUsage(AttributeTargets.GenericParameter)]
public class IsInterface : ConstraintAttribute
{
    public override bool Check(Type genericType)
    {
        return genericType.IsInterface;
    }
    public override string ToString()
    {
        return "Generic type is not an interface";
    }
}
public abstract class ConstraintAttribute : Attribute
{
    public ConstraintAttribute() {}
    public abstract bool Check(Type generic);
}
internal class BigEndianByteReader
{
    public BigEndianByteReader(byte[] data)
    {
        this.data = data;
        this.position = 0;
    }
    private byte[] data;
    private int position;
    public int Position
    {
        get { return position; }
    }
    public bool Eof
    {
        get { return position >= data.Length; }
    }
    public sbyte ReadSByte()
    {
        return (sbyte)data[position++];
    }
    public byte ReadByte()
    {
        return (byte)data[position++];
    }
    public int ReadInt16()
    {
        return ((data[position++] | (data[position++] << 8)));
    }
    public ushort ReadUInt16()
    {
        return (ushort)((data[position++] | (data[position++] << 8)));
    }
    public int ReadInt32()
    {
        return (((data[position++] | (data[position++] << 8)) | (data[position++] << 0x10)) | (data[position++] << 0x18));
    }
    public ulong ReadInt64()
    {
        return (ulong)(((data[position++] | (data[position++] << 8)) | (data[position++] << 0x10)) | (data[position++] << 0x18) | 
                        (data[position++] << 0x20) | (data[position++] << 0x28) | (data[position++] << 0x30) | (data[position++] << 0x38));
    }
    public double ReadDouble()
    {
        var result = BitConverter.ToDouble(data, position);
        position += 8;
        return result;
    }
    public float ReadSingle()
    {
        var result = BitConverter.ToSingle(data, position);
        position += 4;
        return result;
    }
}
internal class ILDecompiler
{
    static ILDecompiler()
    {
        // Initialize our cheat tables
        singleByteOpcodes = new OpCode[0x100];
        multiByteOpcodes = new OpCode[0x100];
        FieldInfo[] infoArray1 = typeof(OpCodes).GetFields();
        for (int num1 = 0; num1 < infoArray1.Length; num1++)
        {
            FieldInfo info1 = infoArray1[num1];
            if (info1.FieldType == typeof(OpCode))
            {
                OpCode code1 = (OpCode)info1.GetValue(null);
                ushort num2 = (ushort)code1.Value;
                if (num2 < 0x100)
                {
                    singleByteOpcodes[(int)num2] = code1;
                }
                else
                {
                    if ((num2 & 0xff00) != 0xfe00)
                    {
                        throw new Exception("Invalid opcode: " + num2.ToString());
                    }
                    multiByteOpcodes[num2 & 0xff] = code1;
                }
            }
        }
    }
    private ILDecompiler() { }
    private static OpCode[] singleByteOpcodes;
    private static OpCode[] multiByteOpcodes;
    public static IEnumerable<ILInstruction> Decompile(MethodBase mi, byte[] ildata)
    {
        Module module = mi.Module;
        BigEndianByteReader reader = new BigEndianByteReader(ildata);
        while (!reader.Eof)
        {
            OpCode code = OpCodes.Nop;
            int offset = reader.Position;
            ushort b = reader.ReadByte();
            if (b != 0xfe)
            {
                code = singleByteOpcodes[b];
            }
            else
            {
                b = reader.ReadByte();
                code = multiByteOpcodes[b];
                b |= (ushort)(0xfe00);
            }
            object operand = null;
            switch (code.OperandType)
            {
                case OperandType.InlineBrTarget:
                    operand = reader.ReadInt32() + reader.Position;
                    break;
                case OperandType.InlineField:
                    if (mi is ConstructorInfo)
                    {
                        operand = module.ResolveField(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
                    }
                    else
                    {
                        operand = module.ResolveField(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
                    }
                    break;
                case OperandType.InlineI:
                    operand = reader.ReadInt32();
                    break;
                case OperandType.InlineI8:
                    operand = reader.ReadInt64();
                    break;
                case OperandType.InlineMethod:
                    try
                    {
                        if (mi is ConstructorInfo)
                        {
                            operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
                        }
                        else
                        {
                            operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
                        }
                    }
                    catch
                    {
                        operand = null;
                    }
                    break;
                case OperandType.InlineNone:
                    break;
                case OperandType.InlineR:
                    operand = reader.ReadDouble();
                    break;
                case OperandType.InlineSig:
                    operand = module.ResolveSignature(reader.ReadInt32());
                    break;
                case OperandType.InlineString:
                    operand = module.ResolveString(reader.ReadInt32());
                    break;
                case OperandType.InlineSwitch:
                    int count = reader.ReadInt32();
                    int[] targetOffsets = new int[count];
                    for (int i = 0; i < count; ++i)
                    {
                        targetOffsets[i] = reader.ReadInt32();
                    }
                    int pos = reader.Position;
                    for (int i = 0; i < count; ++i)
                    {
                        targetOffsets[i] += pos;
                    }
                    operand = targetOffsets;
                    break;
                case OperandType.InlineTok:
                case OperandType.InlineType:
                    try
                    {
                        if (mi is ConstructorInfo)
                        {
                            operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), Type.EmptyTypes);
                        }
                        else
                        {
                            operand = module.ResolveMember(reader.ReadInt32(), mi.DeclaringType.GetGenericArguments(), mi.GetGenericArguments());
                        }
                    }
                    catch
                    {
                        operand = null;
                    }
                    break;
                case OperandType.InlineVar:
                    operand = reader.ReadUInt16();
                    break;
                case OperandType.ShortInlineBrTarget:
                    operand = reader.ReadSByte() + reader.Position;
                    break;
                case OperandType.ShortInlineI:
                    operand = reader.ReadSByte();
                    break;
                case OperandType.ShortInlineR:
                    operand = reader.ReadSingle();
                    break;
                case OperandType.ShortInlineVar:
                    operand = reader.ReadByte();
                    break;
                default:
                    throw new Exception("Unknown instruction operand; cannot continue. Operand type: " + code.OperandType);
            }
            yield return new ILInstruction(offset, code, operand);
        }
    }
}
public class ILInstruction
{
    public ILInstruction(int offset, OpCode code, object operand)
    {
        this.Offset = offset;
        this.Code = code;
        this.Operand = operand;
    }
    public int Offset { get; private set; }
    public OpCode Code { get; private set; }
    public object Operand { get; private set; }
}
public class IncorrectConstraintException : Exception
{
    public IncorrectConstraintException(string msg, params object[] arg) : base(string.Format(msg, arg)) { }
}
public class ConstraintFailedException : Exception
{
    public ConstraintFailedException(string msg) : base(msg) { }
    public ConstraintFailedException(string msg, params object[] arg) : base(string.Format(msg, arg)) { }
}
public class NCTChecks
{
    public NCTChecks(Type startpoint)
        : this(startpoint.Assembly)
    { }
    public NCTChecks(params Assembly[] ass)
    {
        foreach (var assembly in ass)
        {
            assemblies.Add(assembly);
            foreach (var type in assembly.GetTypes())
            {
                EnsureType(type);
            }
        }
        while (typesToCheck.Count > 0)
        {
            var t = typesToCheck.Pop();
            GatherTypesFrom(t);
            PerformRuntimeCheck(t);
        }
    }
    private HashSet<Assembly> assemblies = new HashSet<Assembly>();
    private Stack<Type> typesToCheck = new Stack<Type>();
    private HashSet<Type> typesKnown = new HashSet<Type>();
    private void EnsureType(Type t)
    {
        // Don't check for assembly here; we can pass f.ex. System.Lazy<Our.T<MyClass>>
        if (t != null && !t.IsGenericTypeDefinition && typesKnown.Add(t))
        {
            typesToCheck.Push(t);
            if (t.IsGenericType)
            {
                foreach (var par in t.GetGenericArguments())
                {
                    EnsureType(par);
                }
            }
            if (t.IsArray)
            {
                EnsureType(t.GetElementType());
            }
        }
    }
    private void PerformRuntimeCheck(Type t)
    {
        if (t.IsGenericType && !t.IsGenericTypeDefinition)
        {
            // Only check the assemblies we explicitly asked for:
            if (this.assemblies.Contains(t.Assembly))
            {
                // Gather the generics data:
                var def = t.GetGenericTypeDefinition();
                var par = def.GetGenericArguments();
                var args = t.GetGenericArguments();
                // Perform checks:
                for (int i = 0; i < args.Length; ++i)
                {
                    foreach (var check in par[i].GetCustomAttributes(typeof(ConstraintAttribute), true).Cast<ConstraintAttribute>())
                    {
                        if (!check.Check(args[i]))
                        {
                            string error = "Runtime type check failed for type " + t.ToString() + ": " + check.ToString();
                            Debugger.Break();
                            throw new ConstraintFailedException(error);
                        }
                    }
                }
            }
        }
    }
    // Phase 1: all types that are referenced in some way
    private void GatherTypesFrom(Type t)
    {
        EnsureType(t.BaseType);
        foreach (var intf in t.GetInterfaces())
        {
            EnsureType(intf);
        }
        foreach (var nested in t.GetNestedTypes())
        {
            EnsureType(nested);
        }
        var all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
        foreach (var field in t.GetFields(all))
        {
            EnsureType(field.FieldType);
        }
        foreach (var property in t.GetProperties(all))
        {
            EnsureType(property.PropertyType);
        }
        foreach (var evt in t.GetEvents(all))
        {
            EnsureType(evt.EventHandlerType);
        }
        foreach (var ctor in t.GetConstructors(all))
        {
            foreach (var par in ctor.GetParameters())
            {
                EnsureType(par.ParameterType);
            }
            // Phase 2: all types that are used in a body
            GatherTypesFrom(ctor);
        }
        foreach (var method in t.GetMethods(all))
        {
            if (method.ReturnType != typeof(void))
            {
                EnsureType(method.ReturnType);
            }
            foreach (var par in method.GetParameters())
            {
                EnsureType(par.ParameterType);
            }
            // Phase 2: all types that are used in a body
            GatherTypesFrom(method);
        }
    }
    private void GatherTypesFrom(MethodBase method)
    {
        if (this.assemblies.Contains(method.DeclaringType.Assembly)) // only consider methods we've build ourselves
        {
            MethodBody methodBody = method.GetMethodBody();
            if (methodBody != null)
            {
                // Handle local variables
                foreach (var local in methodBody.LocalVariables)
                {
                    EnsureType(local.LocalType);
                }
                // Handle method body
                var il = methodBody.GetILAsByteArray();
                if (il != null)
                {
                    foreach (var oper in ILDecompiler.Decompile(method, il))
                    {
                        if (oper.Operand is MemberInfo)
                        {
                            foreach (var type in HandleMember((MemberInfo)oper.Operand))
                            {
                                EnsureType(type);
                            }
                        }
                    }
                }
            }
        }
    }
    private static IEnumerable<Type> HandleMember(MemberInfo info)
    {
        // Event, Field, Method, Constructor or Property.
        yield return info.DeclaringType;
        if (info is EventInfo)
        {
            yield return ((EventInfo)info).EventHandlerType;
        }
        else if (info is FieldInfo)
        {
            yield return ((FieldInfo)info).FieldType;
        }
        else if (info is PropertyInfo)
        {
            yield return ((PropertyInfo)info).PropertyType;
        }
        else if (info is ConstructorInfo)
        {
            foreach (var par in ((ConstructorInfo)info).GetParameters())
            {
                yield return par.ParameterType;
            }
        }
        else if (info is MethodInfo)
        {
            foreach (var par in ((MethodInfo)info).GetParameters())
            {
                yield return par.ParameterType;
            }
        }
        else if (info is Type)
        {
            yield return (Type)info;
        }
        else
        {
            throw new NotSupportedException("Incorrect unsupported member type: " + info.GetType().Name);
        }
    }
}
コードを使用する
まあ、それは簡単な部分です:-)
// Create something illegal
public class Bar2 : IMyInterface
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}
// Our fancy check
public class Foo<[IsInterface] T>
{
}
class Program
{
    static Program()
    {
        // Perform all runtime checks
        new NCTChecks(typeof(Program));
    }
    static void Main(string[] args)
    {
        // Normal operation
        Console.WriteLine("Foo");
        Console.ReadLine();
    }
}