Example #1
0
        public void InterfaceIsInstance()
        {
            object e = new NewException();

            Assert.IsTrue(e is IExInterface);

            object f = new EException();

            Assert.IsTrue(!(f is IExInterface));
        }
Example #2
0
        public virtual TypeBuilder DraftType(ModuleBuilder module, Type constraint, string name, TypeAttributes attributes,
                                             Func <MethodInfo, string> explicitInterfaceMethodNameTranslator = null)
        {
            #region ensure arguments

            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            #region check argument constraint

            if (constraint == null)
            {
                throw new ArgumentNullException(nameof(constraint));
            }

            var @base      = constraint.BaseType;
            var interfaces = constraint.GetInterfaces();
            if (!constraint.IsGenericParameter)
            {
                if (!constraint.IsInterface)
                {
                    throw NewException.ForInvalidArgument(nameof(constraint));
                }

                var tmp = new Type[interfaces.Length + 1];
                tmp[0] = constraint;
                interfaces.CopyTo(tmp, 1);
                interfaces = tmp;
            }
            else
            {
                var gpAttrs = constraint.GenericParameterAttributes;
                if ((gpAttrs & (DefaultConstructorConstraint | NotNullableValueTypeConstraint)) == DefaultConstructorConstraint)
                {
                    throw NewException.ForInvalidArgument(nameof(constraint));
                }

                if (interfaces.Length == 0)
                {
                    throw NewException.ForInvalidArgument(nameof(constraint));
                }
            }

            if (@base != null && @base != TypeObject && @base != TypeValueType)
            {
                throw NewException.ForInvalidArgument(nameof(constraint));
            }

            #endregion

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw NewException.ForArgument(nameof(name));
            }

            #endregion

            var type = module.DefineType(name, attributes, @base, interfaces.ToArray());

            var input = type.DefineField("@", ThisType, FieldAttributes.Private | FieldAttributes.InitOnly);
            var ctor  = type.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new[] { ThisType });
            var ilGen = ctor.GetILGenerator();
            ilGen.Emit(OpCodes.Ldarg_0);
            ilGen.Emit(OpCodes.Ldarg_1);
            ilGen.Emit(OpCodes.Stfld, input);
            ilGen.Emit(OpCodes.Ret);

            FillInMethods(type, input, explicitInterfaceMethodNameTranslator);

            return(type);
        }
Example #3
0
        public void BaseClassIsInstance()
        {
            var e = new NewException();

            Assert.IsTrue(e is Exception);
        }