コード例 #1
0
        public void CreateTypeDefinition_LargeNumberOfProperties(int propertyCount)
        {
            var propertyNames = Enumerable.Range(0, propertyCount).Select(i => "Property" + i.ToString()).ToList();

            var genericTypeDefinition = AnonymousTypeUtils.CreateGenericTypeDefinition(propertyNames);

            Assert.AreEqual(propertyCount, genericTypeDefinition.GetProperties().Length);
        }
コード例 #2
0
        public void CreateGenericTypeDefinition_ParentClassWithConstructor()
        {
            var expectedException = Assert.Throws <ArgumentException>(() => AnonymousTypeUtils.CreateGenericTypeDefinition(
                                                                          propertyNames: new[] { "b", "a" },
                                                                          isMutable: false,
                                                                          parent: typeof(TestClassWithConstructor)
                                                                          ));

            StringAssert.Contains("default constructor", expectedException.Message);
        }
コード例 #3
0
        public void CreateGenericTypeDefinition_ParentClass()
        {
            var  parentType = typeof(TestClass);
            Type anonymousGenericTypeDefinition = AnonymousTypeUtils.CreateGenericTypeDefinition(
                propertyNames: new[] { "b", "a" },
                isMutable: false,
                parent: parentType
                );

            Assert.IsTrue(anonymousGenericTypeDefinition.IsSubclassOf(parentType));
        }
コード例 #4
0
        public void CreateGenericTypeDefinition(bool isMutable)
        {
            var propertyNames = new[] { "b", "a" };

            var anonymousGenericTypeDefinition = AnonymousTypeUtils.CreateGenericTypeDefinition(propertyNames, isMutable: isMutable);

            Assert.AreEqual(2, anonymousGenericTypeDefinition.GetProperties().Length);

            var propertyA = anonymousGenericTypeDefinition.GetProperty("a");

            Assert.IsNotNull(propertyA);
            Assert.IsTrue(propertyA.PropertyType.IsGenericParameter);
            Assert.IsTrue(propertyA.CanRead);
            Assert.AreEqual(isMutable, propertyA.CanWrite);

            var propertyB = anonymousGenericTypeDefinition.GetProperty("b");

            Assert.IsNotNull(propertyB);
            Assert.AreEqual("b", propertyB.Name);
            Assert.IsTrue(propertyB.PropertyType.IsGenericParameter);
            Assert.IsTrue(propertyB.CanRead);
            Assert.AreEqual(isMutable, propertyB.CanWrite);

            var constructors = anonymousGenericTypeDefinition.GetConstructors();

            Assert.AreEqual(1, constructors.Length);

            var constructorParameters = constructors.Single().GetParameters();

            Assert.AreEqual(2, constructorParameters.Length);

            Assert.AreEqual("b", constructorParameters.ElementAt(0).Name);
            Assert.AreEqual("a", constructorParameters.ElementAt(1).Name);

            StringAssert.StartsWith("<>f__LatticeUtilsAnonymousType", anonymousGenericTypeDefinition.Name);
            StringAssert.EndsWith("`2", anonymousGenericTypeDefinition.Name);
        }
コード例 #5
0
        /// <summary>
        /// Constructs an anonymous function that returns a DbExpression and expects an Anonymous Type.
        /// Supports the ability to dynamically create the following lambda:
        /// p => new {
        ///   Column1 = p.Property("l").Property("Column1")
        /// }
        /// </summary>
        /// <param name="fields">List of column names</param>
        /// <returns>Func<DbExpression,T></returns>
        public static LambdaExpression CreateLambdaExpression(Dictionary <string, string> fields)
        {
            Type type = Type.GetType("System.Func`2");

            Type[] anonymousTypeArguments = fields.Keys.Select(p => typeof(DbPropertyExpression)).ToArray();
            Type   anonymousType          = AnonymousTypeUtils.CreateGenericTypeDefinition(fields.Keys);

            anonymousType = anonymousType.MakeGenericType(anonymousTypeArguments);

            Type[] typeArguments = new[] { typeof(DbExpression), anonymousType };
            var    lambdaType    = type.MakeGenericType(typeArguments);

            var pParam = Expression.Parameter(typeof(DbExpression), "p");

            ParameterExpression[] parameters = { pParam };

            ConstructorInfo constructor = anonymousType.GetConstructors().SingleOrDefault();

            MethodInfo memberInfo = typeof(DbExpressionBuilder).GetMethods().FirstOrDefault(m => m.ToString() == "System.Data.Entity.Core.Common.CommandTrees.DbPropertyExpression Property(System.Data.Entity.Core.Common.CommandTrees.DbExpression, System.String)");

            List <Expression> argumentList = new List <Expression>();
            List <MemberInfo> membersList  = new List <MemberInfo>();

            foreach (string field in fields.Keys)
            {
                Expression[] leftTableExpressions = { pParam, Expression.Constant(fields[field]) };
                Expression   methodCallExpression = Expression.Call(null, memberInfo, leftTableExpressions);

                Expression[] secondPropertyExpressions = { methodCallExpression, Expression.Constant(field) };
                argumentList.Add(Expression.Call(null, memberInfo, secondPropertyExpressions));
                membersList.Add(anonymousType.GetMembers().FirstOrDefault(m => m.ToString() == string.Format("System.Data.Entity.Core.Common.CommandTrees.DbPropertyExpression {0}", field)));
            }

            var body = Expression.New(constructor, argumentList, membersList);

            return(Expression.Lambda(lambdaType, body, parameters));
        }