mardi 15 décembre 2015

Determine if type is instantiable or usable

In the following example, the comment is meant to check if a type is instantiable or usable:

class TypeContract
{
    Type indicator;

    public TypeContract(Type type)
    {
        // if (!type.IsInstantiable)
        // {
        //    throw new ArgumentException();
        // }

        indicator = type;
    }
}

For reference, System.String is instantiable, but System.Convert, IDictionary<,> and TKey are not. Note that constructor accessibility is not a concern in terms of classes.

How does the C# compiler check this? Is there a reliable type attribute? Perhabs it uses a manual check like:

!((type.IsAbstract && type.IsSealed) || type.IsGenericParameter || type.ContainsGenericParameters)





Aucun commentaire:

Enregistrer un commentaire