public bool PosTest1()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P001";
        string       c_TEST_DESC = "PosTest1: initialize an instance of type TargetParameterCountException using an emtpy string message";
        string       errorDesc;

        string message = string.Empty;

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message);
            if (null == e || e.Message != message)
            {
                errorDesc  = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is emtpy string";
                TestLibrary.TestFramework.LogError("001" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc  = "Unexpected exception: " + e;
            errorDesc += "\nInput message is emtpy string";
            TestLibrary.TestFramework.LogError("002" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest2()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P002";
        string       c_TEST_DESC = "PosTest2: initialize an instance of type TargetParameterCountException using a string containing special character";
        string       errorDesc;

        string message = "Not supported exception occurs here \n\r\0\t\v";

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message);
            if (null == e || e.Message != message)
            {
                errorDesc  = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is \"" + message + "\"";
                TestLibrary.TestFramework.LogError("003" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc  = "Unexpected exception: " + e;
            errorDesc += "\nInput message is \"" + message + "\"";
            TestLibrary.TestFramework.LogError("004" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest3()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P003";
        string       c_TEST_DESC = "PosTest3: initialize an instance of type TargetParameterCountException using a null reference";
        string       errorDesc;

        string    message        = null;
        Exception innerException = new Exception();

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message, innerException);
            if (null == e)
            {
                errorDesc  = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is a null reference.";
                errorDesc += "\nInner exception is " + innerException;
                TestLibrary.TestFramework.LogError("005" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc  = "Unexpected exception: " + e;
            errorDesc += "\nInput message is a null reference.";
            errorDesc += "\nInner exception is " + innerException;
            TestLibrary.TestFramework.LogError("006" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest4()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P004";
        string       c_TEST_DESC = "PosTest4: message is a string containing special character and inner exception is a null reference";
        string       errorDesc;

        string    message        = "Not supported exception occurs here";
        Exception innerException = null;

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message, innerException);
            if (null == e || e.Message != message || e.InnerException != innerException)
            {
                errorDesc  = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is \"" + message + "\"";
                errorDesc += "\nInner exception is a null reference.";
                TestLibrary.TestFramework.LogError("007" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc  = "Unexpected exception: " + e;
            errorDesc += "\nInput message is \"" + message + "\"";
            errorDesc += "\nInner exception is a null reference.";
            TestLibrary.TestFramework.LogError("008" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest1()
    {
        bool retVal = true;

        const string c_TEST_ID = "P001";
        string c_TEST_DESC = "PosTest1: initialize an instance of type TargetParameterCountException via default constructor";
        string errorDesc;

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException();
            if (null == e)
            {
                errorDesc = "Failed to initialize an instance of type TargetParameterCountException via default constructor.";
                TestLibrary.TestFramework.LogError("001" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            TestLibrary.TestFramework.LogError("002" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return retVal;
    }
    public bool PosTest1()
    {
        bool retVal = true;

        const string c_TEST_ID = "P001";
        string c_TEST_DESC = "PosTest1: initialize an instance of type TargetParameterCountException using an emtpy string message";
        string errorDesc;

        string message = string.Empty;
        Exception innerException = new ArgumentNullException();

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message, innerException);
            if (null == e || e.Message != message || e.InnerException != innerException)
            {
                errorDesc = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is emtpy string";
                errorDesc += "\nInner exception is " + innerException;
                TestLibrary.TestFramework.LogError("001" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            errorDesc += "\nInput message is emtpy string";
            errorDesc += "\nInner exception is " + innerException;
            TestLibrary.TestFramework.LogError("002" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return retVal;
    }
예제 #7
0
        public static object GetMemberValue(MemberInfo member, object target)
        {
            object value;

            ValidationUtils.ArgumentNotNull(member, "member");
            ValidationUtils.ArgumentNotNull(target, "target");
            MemberTypes memberType = member.MemberType();

            if (memberType == MemberTypes.Field)
            {
                return(((FieldInfo)member).GetValue(target));
            }
            if (memberType != MemberTypes.Property)
            {
                throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, member.Name), "member");
            }
            try
            {
                value = ((PropertyInfo)member).GetValue(target, null);
            }
            catch (TargetParameterCountException targetParameterCountException1)
            {
                TargetParameterCountException targetParameterCountException = targetParameterCountException1;
                throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(CultureInfo.InvariantCulture, member.Name), targetParameterCountException);
            }
            return(value);
        }
    public bool PosTest1()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P001";
        string       c_TEST_DESC = "PosTest1: initialize an instance of type TargetParameterCountException via default constructor";
        string       errorDesc;

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException();
            if (null == e)
            {
                errorDesc = "Failed to initialize an instance of type TargetParameterCountException via default constructor.";
                TestLibrary.TestFramework.LogError("001" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            TestLibrary.TestFramework.LogError("002" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
예제 #9
0
        public static void TargetParameterCountException()
        {
            TargetParameterCountException ex = new TargetParameterCountException();

            Assert.NotEmpty(ex.Message);
            Assert.Null(ex.InnerException);
            Assert.Equal(COR_E_TARGETPARAMCOUNT, ex.HResult);
        }
예제 #10
0
        public static void TargetParameterCountException_Message()
        {
            string message = "message";
            TargetParameterCountException ex = new TargetParameterCountException(message);

            Assert.Equal(message, ex.Message);
            Assert.Null(ex.InnerException);
            Assert.Equal(COR_E_TARGETPARAMCOUNT, ex.HResult);
        }
예제 #11
0
        private static Exception TranslateException(TargetParameterCountException e)
        {
            //
            // The parameters array does not have the correct number of
            // arguments.
            //

            return(new InvocationException(e.Message, e));
        }
예제 #12
0
        public static void TargetParameterCountException_Message_InnerException()
        {
            string    message = "message";
            Exception inner   = new Exception();
            TargetParameterCountException ex = new TargetParameterCountException(message, inner);

            Assert.Equal(message, ex.Message);
            Assert.Same(inner, ex.InnerException);
            Assert.Equal(COR_E_TARGETPARAMCOUNT, ex.HResult);
        }
예제 #13
0
        public static void ResolverThrows()
        {
            var resolver = new ResolverThrows();

            using (MetadataLoadContext lc = new MetadataLoadContext(resolver, "EmptyCore"))
            {
                Assert.Null(resolver.Context);
                Assert.Equal(0, resolver.CallCount);

                Assembly derived = lc.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);
                Type     t       = derived.GetType("Derived1", throwOnError: true);
                TargetParameterCountException e = Assert.Throws <TargetParameterCountException>(() => t.BaseType);

                Assert.Same(lc, resolver.Context);
                Assert.Equal(1, resolver.CallCount);
                Assert.Equal("Hi!", e.Message);
            }
        }
예제 #14
0
        public static void ResolveEventThrows()
        {
            using (TypeLoader tl = new TypeLoader())
            {
                bool resolveHandlerCalled = false;

                tl.Resolving +=
                    delegate(TypeLoader sender, AssemblyName name)
                {
                    resolveHandlerCalled = true;
                    throw new TargetParameterCountException("Hi!");
                };

                Assembly derived = tl.LoadFromByteArray(TestData.s_DerivedClassWithVariationsOnFooImage);
                Type     t       = derived.GetType("Derived1", throwOnError: true);
                TargetParameterCountException e = Assert.Throws <TargetParameterCountException>(() => t.BaseType);
                Assert.True(resolveHandlerCalled);
                Assert.Equal("Hi!", e.Message);
            }
        }
    public bool PosTest3()
    {
        bool retVal = true;

        const string c_TEST_ID = "P003";
        string c_TEST_DESC = "PosTest3: initialize an instance of type TargetParameterCountException using a null reference";
        string errorDesc;

        string message = null;
        Exception innerException = new Exception();

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message, innerException);
            if (null == e)
            {
                errorDesc = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is a null reference.";
                errorDesc += "\nInner exception is " + innerException;
                TestLibrary.TestFramework.LogError("005" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            errorDesc += "\nInput message is a null reference.";
            errorDesc += "\nInner exception is " + innerException;
            TestLibrary.TestFramework.LogError("006" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return retVal;
    }
예제 #16
0
        /// <summary>
        /// 以指定的属性为基础,定义以自动属性模式实现并触发属性更改事件的重写属性,并添加到当前类型。
        /// </summary>
        /// <param name="type">要定义属性的 <see cref="TypeBuilder"/> 对象。</param>
        /// <param name="baseProperty">作为基础的属性。</param>
        /// <param name="onPropertyChangedMethod">
        /// <see cref="INotifyPropertyChanged.PropertyChanged"/> 事件的触发方法。</param>
        /// <param name="explicitOverride">指定是否以显式方式重写。</param>
        /// <returns>定义的属性更改通知自动属性及其对应的字段,
        /// 在属性更改时触发 <see cref="INotifyPropertyChanged.PropertyChanged"/> 事件。</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="baseProperty"/> 是索引属性或无法在程序集外部重写。</exception>
        /// <exception cref="ArgumentNullException">存在为 <see langword="null"/> 的参数。</exception>
        internal static KeyValuePair <PropertyBuilder, FieldBuilder> DefineObservableAutoPropertyOverride(
            this TypeBuilder type, PropertyInfo baseProperty,
            MethodInfo onPropertyChangedMethod, bool explicitOverride = false)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (baseProperty is null)
            {
                throw new ArgumentNullException(nameof(baseProperty));
            }
            if (onPropertyChangedMethod is null)
            {
                throw new ArgumentNullException(nameof(onPropertyChangedMethod));
            }
            if (baseProperty.GetIndexParameters().Length != 0)
            {
                var inner = new TargetParameterCountException();
                throw new ArgumentException(inner.Message, nameof(baseProperty), inner);
            }
            if (!baseProperty.GetAccessors().All(accessor => accessor.IsOverridable()))
            {
                var inner = new MemberAccessException();
                throw new ArgumentException(inner.Message, nameof(baseProperty), inner);
            }

            var rPropertyNames = baseProperty.GetCustomAttribute <
                RelatedPropertiesAttribute>()?.PropertyNames ?? Array.Empty <string>();

            var propertyName = baseProperty.Name;

            if (explicitOverride)
            {
                var baseHandle = baseProperty.GetAccessors().First().MethodHandle;
                propertyName += $"#{baseHandle.Value.ToString()}";
            }

            var property = type.DefineProperty(
                propertyName, baseProperty.Attributes, baseProperty.PropertyType,
                Array.ConvertAll(baseProperty.GetIndexParameters(), param => param.ParameterType));

            var field = type.DefineField(
                propertyName, baseProperty.PropertyType, FieldAttributes.Private);

            if (baseProperty.CanRead)
            {
                var baseMethod = baseProperty.GetMethod !;

                var method = type.DefineMethodOverride(baseMethod, explicitOverride);

                var il = method.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, field);
                il.Emit(OpCodes.Ret);

                property.SetGetMethod(method);
            }

            if (baseProperty.CanWrite)
            {
                var baseMethod = baseProperty.SetMethod !;

                var method = type.DefineMethodOverride(baseMethod, explicitOverride);

                var il = method.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Stfld, field);
                var eventArgsConstructor =
                    typeof(PropertyChangedEventArgs).GetConstructor(new[] { typeof(string) }) !;
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldstr, baseProperty.Name);
                il.Emit(OpCodes.Newobj, eventArgsConstructor);
                il.Emit(OpCodes.Callvirt, onPropertyChangedMethod);
                foreach (var rPropertyName in rPropertyNames)
                {
                    il.Emit(OpCodes.Ldarg_0);
                    il.Emit(OpCodes.Ldstr, rPropertyName);
                    il.Emit(OpCodes.Newobj, eventArgsConstructor);
                    il.Emit(OpCodes.Callvirt, onPropertyChangedMethod);
                }
                il.Emit(OpCodes.Ret);

                property.SetSetMethod(method);
            }

            return(new KeyValuePair <PropertyBuilder, FieldBuilder>(property, field));
        }
    public bool PosTest2()
    {
        bool retVal = true;

        const string c_TEST_ID = "P002";
        string c_TEST_DESC = "PosTest2: initialize an instance of type TargetParameterCountException using a string containing special character";
        string errorDesc;

        string message = "Not supported exception occurs here \n\r\0\t\v";
        Exception innerException = new Exception();

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            TargetParameterCountException e = new TargetParameterCountException(message, innerException);
            if (null == e || e.Message != message || e.InnerException != innerException)
            {
                errorDesc = "Failed to initialize an instance of type TargetParameterCountException.";
                errorDesc += "\nInput message is \"" + message + "\"";
                errorDesc += "\nInner exception is " + innerException;
                TestLibrary.TestFramework.LogError("003" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            errorDesc += "\nInput message is \"" + message + "\"";
            errorDesc += "\nInner exception is " + innerException;
            TestLibrary.TestFramework.LogError("004" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return retVal;
    }
예제 #18
0
        /// <summary>
        /// 以指定的属性为基础,定义以自动属性模式实现的重写属性,并添加到当前类型。
        /// </summary>
        /// <param name="type">要定义属性的 <see cref="TypeBuilder"/> 对象。</param>
        /// <param name="baseProperty">作为基础的属性。</param>
        /// <param name="explicitOverride">指定是否以显式方式重写。</param>
        /// <returns>定义的自动属性与其对应的字段。</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="baseProperty"/> 是索引属性或无法在程序集外部重写。</exception>
        /// <exception cref="ArgumentNullException">存在为 <see langword="null"/> 的参数。</exception>
        public static KeyValuePair <PropertyBuilder, FieldBuilder> DefineAutoPropertyOverride(
            this TypeBuilder type, PropertyInfo baseProperty, bool explicitOverride = false)
        {
            if (type is null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (baseProperty is null)
            {
                throw new ArgumentNullException(nameof(baseProperty));
            }
            if (baseProperty.GetIndexParameters().Length != 0)
            {
                var inner = new TargetParameterCountException();
                throw new ArgumentException(inner.Message, nameof(baseProperty), inner);
            }
            if (!baseProperty.GetAccessors().All(accessor => accessor.IsOverridable()))
            {
                var inner = new MemberAccessException();
                throw new ArgumentException(inner.Message, nameof(baseProperty), inner);
            }

            var propertyName = baseProperty.Name;

            if (explicitOverride)
            {
                var baseHandle = baseProperty.GetAccessors().First().MethodHandle;
                propertyName += $"#{baseHandle.Value.ToString()}";
            }

            var property = type.DefineProperty(
                propertyName, baseProperty.Attributes, baseProperty.PropertyType,
                Array.ConvertAll(baseProperty.GetIndexParameters(), param => param.ParameterType));

            var field = type.DefineField(
                propertyName, baseProperty.PropertyType, FieldAttributes.Private);

            if (baseProperty.CanRead)
            {
                var baseMethod = baseProperty.GetMethod !;

                var method = type.DefineMethodOverride(baseMethod, explicitOverride);

                var il = method.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldfld, field);
                il.Emit(OpCodes.Ret);

                property.SetGetMethod(method);
            }

            if (baseProperty.CanWrite)
            {
                var baseMethod = baseProperty.SetMethod !;

                var method = type.DefineMethodOverride(baseMethod, explicitOverride);

                var il = method.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                il.Emit(OpCodes.Stfld, field);
                il.Emit(OpCodes.Ret);

                property.SetSetMethod(method);
            }

            return(new KeyValuePair <PropertyBuilder, FieldBuilder>(property, field));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="InvalidHandleEventException"/> class.
 /// </summary>
 /// <param name="message">Message.</param>
 /// <param name="targetParameterCountException">Target parameter count exception.</param>
 public InvalidHandleEventException(string message, TargetParameterCountException targetParameterCountException)
     : base(message, targetParameterCountException)
 {
 }
예제 #20
0
        /// <summary>
        /// 以指定的泛型参数列表为基础,设定当前泛型参数列表的泛型约束。
        /// </summary>
        /// <param name="genericParams">
        /// 要设定泛型约束的 <see cref="GenericTypeParameterBuilder"/> 数组。</param>
        /// <param name="baseGenericParams">作为基础的泛型参数列表。</param>
        /// <param name="baseTypeGenericArgs">定义基础泛型参数列表的方法的构造泛型类型的泛型参数列表。</param>
        /// <exception cref="ArgumentException"><paramref name="baseGenericParams"/>
        /// 不为泛型参数列表,或与 <paramref name="genericParams"/> 的长度不等。</exception>
        /// <exception cref="ArgumentNullException">存在为 <see langword="null"/> 的参数。</exception>
        internal static void SetGenericConstraintsLike(
            this GenericTypeParameterBuilder[] genericParams,
            Type[] baseGenericParams, Type[] baseTypeGenericArgs)
        {
            if (genericParams is null)
            {
                throw new ArgumentNullException(nameof(genericParams));
            }
            if (baseGenericParams is null)
            {
                throw new ArgumentNullException(nameof(baseGenericParams));
            }
            if (baseTypeGenericArgs is null)
            {
                throw new ArgumentNullException(nameof(baseTypeGenericArgs));
            }
            if (baseGenericParams.Length != genericParams.Length)
            {
                var inner = new TargetParameterCountException();
                throw new ArgumentException(inner.Message, nameof(baseGenericParams), inner);
            }
            if (baseGenericParams.Any(type => !type.IsGenericParameter))
            {
                var inner = new InvalidOperationException();
                throw new ArgumentException(inner.Message, nameof(baseGenericParams), inner);
            }

            Type MakeConstraint(Type constraintType)
            {
                if (constraintType.IsGenericParameter)
                {
                    var position = constraintType.GenericParameterPosition;
                    var isInBase = Array.IndexOf(baseGenericParams, constraintType) != -1;
                    return(isInBase ? genericParams[position] : baseTypeGenericArgs[position]);
                }
                else if (constraintType.IsGenericType)
                {
                    var typeGenericDefinition = constraintType.GetGenericTypeDefinition();
                    var typeGenericArguments  = constraintType.GetGenericArguments();
                    for (int index = 0; index < typeGenericArguments.Length; index++)
                    {
                        typeGenericArguments[index] = MakeConstraint(typeGenericArguments[index]);
                    }
                    return(typeGenericDefinition.MakeGenericType(typeGenericArguments));
                }
                else
                {
                    return(constraintType);
                }
            }

            for (int index = 0; index < genericParams.Length; index++)
            {
                var genericParam     = genericParams[index];
                var baseGenericParam = baseGenericParams[index];

                var baseGenericConstraints = baseGenericParam.GetGenericParameterConstraints();

                baseGenericConstraints = Array.ConvertAll(baseGenericConstraints, MakeConstraint);

                var baseTypeConstraint = baseGenericConstraints.Where(
                    genericConstraint => genericConstraint.IsClass).SingleOrDefault();
                var interfaceConstraints = baseGenericConstraints.Where(
                    genericConstraint => genericConstraint.IsInterface).ToArray();

                genericParam.SetGenericParameterAttributes(
                    baseGenericParam.GenericParameterAttributes);
                if (baseTypeConstraint is not null)
                {
                    genericParam.SetBaseTypeConstraint(baseTypeConstraint);
                }
                if (interfaceConstraints.Length != 0)
                {
                    genericParam.SetInterfaceConstraints(interfaceConstraints);
                }
            }
        }