static void ReferenceType()
        {
            var refType = new ReferenceTypeConstraint <string>();

            var refType1 = new ReferenceTypeConstraint <Product>();
        }
Пример #2
0
        public GenericConstraint()
        {
            #region 1. Base class constraint.

            var baseClassConstraint1 = new BaseClassConstraint <B1>();
            var baseClassConstraint2 = new BaseClassConstraint <BaseClass>();

            #endregion

            #region 2. Interface constraint.

            var interfaceConstraint1 = new InterfaceConstraint <B2>();
            var interfaceConstraint2 = new InterfaceConstraint <IInterface1>();

            #endregion

            #region 3. Reference-type constraint.

            // 3.1. Class
            var referenceTypeConstraintClass = new ReferenceTypeConstraint <BaseClass>();

            // 3.2. Interface
            var referenceTypeConstraintInterface = new ReferenceTypeConstraint <IInterface1>();

            // 3.3. Array type.
            var referenceTypeConstraintArray = new ReferenceTypeConstraint <Array>();

            // 3.4. Delegate
            var referenceTypeConstraintDelegate = new ReferenceTypeConstraint <Delegate1>();

            #endregion

            #region 4. Value-type constraint.

            var structConstraint1 = new StructConstraint <Struct1>();
            var structConstraint2 = new StructConstraint <int>();

            #endregion

            #region 5. Parameterless constructor constraint.

            var parameterlessConstructorConstraint1 = new ParameterlessConstructorConstraint <BaseClass>();

            // Vì tất cả value type đều có parameterless constructor có thể truy cập nên có thể áp dụng cho new() constraint.
            var parameterlessConstructorConstraint2 = new ParameterlessConstructorConstraint <int>();
            //var parameterlessConstructorConstraint3 = new ParameterlessConstructorConstraint<AbstractClass1>();

            #endregion

            #region 6. Unmanaged constraint.

            //var unmanagedConstraint = new UnmanagedConstraint<Struct2>();

            #endregion

            #region 7. Naked type constraint.

            var nakedTypeConstraint = new NakedTypeConstraint <BaseClass>();
            var text = nakedTypeConstraint.Method1 <BaseClass>();

            #endregion
        }