Ejemplo n.º 1
0
        public void CanAccessPropertiesValues()
        {
            var dict = new ReflectionBasedDictionaryAdapter(new Customer(1, "name"));

            Assert.AreEqual(1, dict["id"]);
            Assert.AreEqual("name", dict["name"]);
        }
Ejemplo n.º 2
0
        public static ComponentRegistration <T> WithEndpoints <T>(this ComponentRegistration <T> r, object endpoints)
            where T : class
        {
            var dictionary = new ReflectionBasedDictionaryAdapter(endpoints);

            var explicitEndpoints = new Dictionary <string, Endpoint>();
            var endpointNames     = new Dictionary <string, string>();

            foreach (var key in dictionary.Keys)
            {
                var dependencyName = key.ToString();
                if (dictionary[key] is Endpoint)
                {
                    var endpoint = (Endpoint)dictionary[key];
                    explicitEndpoints[dependencyName] = endpoint;
                }
                if (dictionary[key] is string)
                {
                    var endpointName = (string)dictionary[key];
                    endpointNames[dependencyName] = endpointName;
                }
            }

            return(r.AddDescriptor(new CustomDependencyDescriptor(explicitEndpoints)).ExtendedProperties(new { endpointNames }));
        }
Ejemplo n.º 3
0
        public void ShouldNotAccessInexistingProperties()
        {
            var dict = new ReflectionBasedDictionaryAdapter(new Customer(1, "name"));

            Assert.IsFalse(dict.Contains("Age"), "Age property found when it should not be");
            Assert.IsFalse(dict.Contains("Address"), "Address property found when it should not be");
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts the route params.
        /// </summary>
        /// <param name="routeParams">The route params.</param>
        /// <returns></returns>
        protected virtual IDictionary ConvertRouteParams(object routeParams)
        {
            IDictionary parameters;

            if (routeParams != null)
            {
                if (typeof(IDictionary).IsAssignableFrom(routeParams.GetType()))
                {
                    parameters = (IDictionary)routeParams;
                }
                else if (typeof(string) == routeParams.GetType())
                {
                    throw new Exception("Route parameters cannot be a string, we expect a dictionary (IDictionary), " +
                                        "an anonymous type or a class from which we can extract the parameters. You have specified '" + routeParams + "'");
                }
                else
                {
                    parameters = new ReflectionBasedDictionaryAdapter(routeParams);
                }

                // Forces copying entries to a non readonly dictionary, preserving the original one
                parameters = new Hashtable(parameters, StringComparer.InvariantCultureIgnoreCase);
            }
            else
            {
                parameters = new HybridDictionary(true);
            }

            return(parameters);
        }
        public static ComponentRegistration <T> WithEndpoints <T>(this ComponentRegistration <T> r, object endpoints)
            where T : class
        {
            var dictionary = new ReflectionBasedDictionaryAdapter(endpoints);

            return(r.WithEndpoints(dictionary));
        }
Ejemplo n.º 6
0
        public static IEnumerable <TModel> GetByQuery <TModel>(this IDao <TModel> extended, string queryString, object argumentsAsAnonymousType)
        {
            ISession             session             = GetSession(extended);
            IDictionary          parameterDictionary = new ReflectionBasedDictionaryAdapter(argumentsAsAnonymousType);
            IEnumerable <TModel> result = extended.GetByQuery(session, queryString, parameterDictionary);

            return(result);
        }
Ejemplo n.º 7
0
        public void CanAccessExistingPropertiesInACaseInsensitiveFashion()
        {
            var dict = new ReflectionBasedDictionaryAdapter(new Customer(1, "name"));

            Assert.IsTrue(dict.Contains("id"));
            Assert.IsTrue(dict.Contains("ID"));
            Assert.IsTrue(dict.Contains("Id"));
            Assert.IsTrue(dict.Contains("name"));
            Assert.IsTrue(dict.Contains("Name"));
            Assert.IsTrue(dict.Contains("NAME"));
        }
Ejemplo n.º 8
0
 public void ShouldNotAccessWriteOnlyProperties()
 {
     try
     {
         var dict = new ReflectionBasedDictionaryAdapter(new Customer(1, "name", true));
         Assert.IsTrue((bool)dict["IsWriteOnly"]);
     }
     catch (ArgumentException)
     {
         Assert.Fail("Attempted to read a write-only property");
     }
 }
Ejemplo n.º 9
0
        public void Using_anonymous_types_works_without_exception()
        {
            var target = new { foo = 1, name = "john", age = 25 };

            Assert.IsFalse(target.GetType().GetTypeInfo().IsPublic);
            var dict = new ReflectionBasedDictionaryAdapter(target);

            Assert.AreEqual(3, dict.Count);

            Assert.AreEqual(1, dict["foo"]);
            Assert.AreEqual("john", dict["name"]);
            Assert.AreEqual(25, dict["age"]);
        }
Ejemplo n.º 10
0
        public void EnumeratorIteration()
        {
            var dict = new ReflectionBasedDictionaryAdapter(new { foo = 1, name = "jonh", age = 25 });

            Assert.AreEqual(3, dict.Count);

            var enumerator = (IDictionaryEnumerator)dict.GetEnumerator();

            while (enumerator.MoveNext())
            {
                Assert.IsNotNull(enumerator.Key);
                Assert.IsNotNull(enumerator.Value);
            }
        }
Ejemplo n.º 11
0
        public void InexistingPropertiesReturnsNull()
        {
            var dict = new ReflectionBasedDictionaryAdapter(new Customer(1, "name"));

            Assert.IsNull(dict["age"]);
        }