public static Type GetEntityType(this PropertyInfo property, out EnclosedType enclosedType)
        {
            var type = property.PropertyType;

            enclosedType = EnclosedType.None;

            var typeInfo = type.GetTypeInfo();

            if (type.IsArray)
            {
                type         = type.GetElementType();
                enclosedType = EnclosedType.Array;
            }
            else if (typeInfo.IsGenericType && typeof(List <>).GetTypeInfo().IsAssignableFrom(type.GetGenericTypeDefinition().GetTypeInfo()))
            {
                type         = typeInfo.GenericTypeArguments[0];
                enclosedType = EnclosedType.List;
            }
            else if (typeInfo.IsGenericType && typeof(ObservableCollection <>).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo().GetGenericTypeDefinition().GetTypeInfo()))
            {
                type         = typeInfo.GenericTypeArguments[0];
                enclosedType = EnclosedType.ObservableCollection;
            }
            return(type);
        }
Exemple #2
0
        private static IEnumerable CreateEnclosedType(Type entityType, EnclosedType enclosedType, IList queryResults)
        {
            switch (enclosedType)
            {
            case EnclosedType.List:
            case EnclosedType.ObservableCollection:
                var collectionType = enclosedType == EnclosedType.List ? typeof(List <>) : typeof(ObservableCollection <>);

                var list = (IList)Activator.CreateInstance(collectionType.MakeGenericType(entityType));
                foreach (var result in queryResults)
                {
                    list.Add(result);
                }
                return(list);

            case EnclosedType.Array:
                var array = Array.CreateInstance(entityType, queryResults.Count);
                for (var i = 0; i < queryResults.Count; i++)
                {
                    array.SetValue(queryResults[i], i);
                }
                return(array);
            }

            return(null);
        }
Exemple #3
0
        public void TestEntityTypeArray()
        {
            var typeA        = typeof(DummyClassA);
            var property     = typeA.GetProperty("ManyToManyD");
            var expectedType = typeof(DummyClassD);
            const EnclosedType expectedContainerType = EnclosedType.Array;

            EnclosedType enclosedType;
            var          entityType = property.GetEntityType(out enclosedType);

            Assert.AreEqual(expectedType, entityType);
            Assert.AreEqual(expectedContainerType, enclosedType);
        }
Exemple #4
0
        public void TestEntityTypeObject()
        {
            var typeA        = typeof(DummyClassA);
            var property     = typeA.GetProperty("OneB");
            var expectedType = typeof(DummyClassB);
            const EnclosedType expectedContainerType = EnclosedType.None;

            EnclosedType enclosedType;
            var          entityType = property.GetEntityType(out enclosedType);

            Assert.AreEqual(expectedType, entityType);
            Assert.AreEqual(expectedContainerType, enclosedType);
        }
Exemple #5
0
        public void TestEntityTypeList()
        {
            var typeA        = typeof(DummyClassA);
            var property     = typeA.GetProperty("OneToManyC");
            var expectedType = typeof(DummyClassC);
            const EnclosedType expectedContainerType = EnclosedType.List;

            EnclosedType enclosedType;
            var          entityType = property.GetEntityType(out enclosedType);

            Assert.AreEqual(expectedType, entityType);
            Assert.AreEqual(expectedContainerType, enclosedType);
        }