Пример #1
0
        public void Convert_StringToEnum_CorrectValue()
        {
            //Build the SUT
            var tc = new TypeConverter();

            //Call the method to test
            var actual = tc.Convert("Beta", typeof(NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration));

            //Assertion
            Assert.That(actual, Is.InstanceOf<NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration>());
            Assert.That(actual, Is.EqualTo(NBi.Testing.Unit.Core.Assemblies.Resource.Enumeration.Beta));
        }
Пример #2
0
        public void Convert_StringToString_CorrectValue()
        {
            //Build the SUT
            var tc = new TypeConverter();

            //Call the method to test
            var actual = tc.Convert("My God", typeof(string));

            //Assertion
            Assert.That(actual, Is.InstanceOf<string>());
            Assert.That(actual, Is.EqualTo("My God"));
        }
Пример #3
0
        public void Convert_StringToDecimal_CorrectValue()
        {
            //Build the SUT
            var tc = new TypeConverter();

            //Call the method to test
            var actual = tc.Convert("10", typeof(decimal));

            //Assertion
            Assert.That(actual, Is.InstanceOf<decimal>());
            Assert.That(actual, Is.EqualTo(10));
        }
Пример #4
0
        public void Convert_StringToDateTime_YMDHMCorrectValue()
        {
            //Build the SUT
            var tc = new TypeConverter();

            //Call the method to test
            var actual = tc.Convert("2012-05-10 10:15", typeof(DateTime));

            //Assertion
            Assert.That(actual, Is.InstanceOf<DateTime>());
            Assert.That(actual, Is.EqualTo(new DateTime(2012, 05, 10, 10, 15, 0)));
        }
Пример #5
0
        /// <summary>
        /// Execute the static method of the a type. Let you specify the value of the parameters of the method called.
        /// </summary>
        /// <param name="target">The type on which you want to apply a method</param>
        /// <param name="methodName">The name of the static method</param>
        /// <param name="parameters">The pair of names and values for each parameter of the method</param>
        /// <returns></returns>
        public object ExecuteStatic(Type type, string methodName, IDictionary<string, object> parameters)
        {
            var flags = BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;
            MethodInfo methodInfo = type.GetMethod(methodName, flags);
            if (methodInfo == null)
                throw new ArgumentException(string.Format("Method named '{0}' not found in type '{1}'", methodName, type), "methodName");

            var paramList = new List<object>();
            foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
            {
                var converter = new TypeConverter();
                var value = converter.Convert(parameters[paramInfo.Name], paramInfo.ParameterType);
                paramList.Add(value);
            }

            var result = methodInfo.Invoke(null, paramList.ToArray());
            return result;
        }
Пример #6
0
        protected internal ICustomCondition Instantiate(Type customConditionType, IReadOnlyDictionary <string, object> parameters)
        {
            var ctor = customConditionType.GetConstructors().FirstOrDefault(
                c => c.GetParameters().All(p => parameters.Keys.Contains(p.Name, StringComparer.InvariantCultureIgnoreCase)) &&
                c.GetParameters().Count() == parameters.Count()
                );

            if (ctor == null)
            {
                throw new NoConstructorFoundException();
            }

            var typeConverter = new TypeConverter();
            var ctorParams    = ctor.GetParameters().Select(
                p => typeConverter.Convert(
                    parameters.First(x => string.Compare(x.Key, p.Name, true) == 0).Value
                    , p.ParameterType)
                ).ToArray();
            var instance = ctor.Invoke(ctorParams) as ICustomCondition;

            return(instance);
        }
Пример #7
0
        /// <summary>
        /// Execute the method of the an object. Let you specify the value of the parameters of the method called.
        /// </summary>
        /// <param name="target">The object on which you want to apply a method</param>
        /// <param name="methodName">The name of the method</param>
        /// <param name="parameters">The pair of names and values for each parameter of the method</param>
        /// <returns></returns>
        public object Execute(object target, string methodName, IDictionary <string, object> parameters)
        {
            var        flags      = BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
            MethodInfo methodInfo = target.GetType().GetMethod(methodName, flags);

            if (methodInfo == null)
            {
                throw new ArgumentException(string.Format("Method named '{0}' not found in type '{1}'", methodName, target.GetType()), "methodName");
            }

            var paramList = new List <object>();

            foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
            {
                var converter = new TypeConverter();
                var value     = converter.Convert(parameters[paramInfo.Name], paramInfo.ParameterType);
                paramList.Add(value);
            }

            var result = methodInfo.Invoke(target, paramList.ToArray());

            return(result);
        }