Exemplo n.º 1
0
        public void CreateAnonymousType_ArgumentChecks()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => RuntimeCompiler.CreateAnonymousType((KeyValuePair <string, Type>[])null), ex => Assert.AreEqual("properties", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => RuntimeCompiler.CreateAnonymousType((KeyValuePair <string, Type>[])null, Array.Empty <string>()), ex => Assert.AreEqual("properties", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => RuntimeCompiler.CreateAnonymousType((StructuralFieldDeclaration[])null), ex => Assert.AreEqual("properties", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => RuntimeCompiler.CreateAnonymousType((StructuralFieldDeclaration[])null, Array.Empty <string>()), ex => Assert.AreEqual("properties", ex.ParamName));
        }
Exemplo n.º 2
0
        public void AnonymousType_PropertyAttribute_NoMatchingConstructor()
        {
            var props = new[] { new StructuralFieldDeclaration("bar", typeof(string), new List <CustomAttributeDeclaration> {
                    new CustomAttributeDeclaration(typeof(FooAttribute), new List <object> {
                        0
                    }.AsReadOnly())
                }.AsReadOnly()) };

            Assert.ThrowsException <InvalidOperationException>(() => RuntimeCompiler.CreateAnonymousType(props));
        }
Exemplo n.º 3
0
 public void AnonymousType_VisualBasic_Static()
 {
     AnonymousType_VisualBasic_Impl(() =>
     {
         return(RuntimeCompiler.CreateAnonymousType(new[]
         {
             new KeyValuePair <string, Type>("Name", typeof(string)),
             new KeyValuePair <string, Type>("Age", typeof(int)),
         }, new[] { "Name" }));
     });
 }
Exemplo n.º 4
0
 public void AnonymousType_CSharp_StructuralFields_Static_WithKeys()
 {
     AnonymousType_CSharp_Impl(() =>
     {
         return(RuntimeCompiler.CreateAnonymousType(new[]
         {
             new StructuralFieldDeclaration("Name", typeof(string)),
             new StructuralFieldDeclaration("Age", typeof(int)),
         }, new[] { "Name", "Age" }));
     });
 }
Exemplo n.º 5
0
        public void TypeSubstitutionExpressionVisitor_Anonymize()
        {
            var pers  = typeof(Person);
            var query = (Expression <Func <IEnumerable <Person>, IEnumerable <string> > >)(xs => from x in xs where x.Age > 10 let name = x.Name where name.StartsWith("B") select name.ToUpper() + " is " + x.Age);

            var check1 = new TypeErasureChecker(new[] { typeof(Person) });

            Assert.ThrowsException <InvalidOperationException>(() => check1.Visit(query));


            var anon = RuntimeCompiler.CreateAnonymousType(new[]
            {
                new KeyValuePair <string, Type>("Name", typeof(string)),
                new KeyValuePair <string, Type>("Age", typeof(int)),
            });

            var subst1 = new TypeSubstitutionExpressionVisitor(new Dictionary <Type, Type>
            {
                { pers, anon }
            });

            var res1 = subst1.Apply(query);

            check1.Visit(res1);

            var check2 = new TypeErasureChecker(new[] { anon });

            Assert.ThrowsException <InvalidOperationException>(() => check2.Visit(res1));


            var f = ((LambdaExpression)res1).Compile();

            var cast       = ((MethodInfo)ReflectionHelpers.InfoOf(() => Enumerable.Cast <int>(null))).GetGenericMethodDefinition().MakeGenericMethod(anon);
            var peopleObj  = new[] { Activator.CreateInstance(anon, new object[] { "Bart", 10 }), Activator.CreateInstance(anon, new object[] { "Lisa", 8 }), Activator.CreateInstance(anon, new object[] { "Bart", 21 }) };
            var peopleAnon = cast.Invoke(obj: null, new object[] { peopleObj });
            var qres       = (IEnumerable <string>)f.DynamicInvoke(peopleAnon);

            Assert.IsTrue(new[] { "BART is 21" }.SequenceEqual(qres));

            var subst2 = new TypeSubstitutionExpressionVisitor(new Dictionary <Type, Type>
            {
                { anon, pers }
            });

            var res2 = subst2.Apply(res1);

            check2.Visit(res2);

            Assert.ThrowsException <InvalidOperationException>(() => check1.Visit(res2));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(query, res2));
        }
Exemplo n.º 6
0
        public void StructuralTypeEqualityComparer_StructuralNotEquals()
        {
            var rt1 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
            },
                                                       true);

            var rt2 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) },
            },
                                                       true);

            var rt3 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(int) }
            },
                                                       true);

            var at1 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
            },
                                                          null);

            var at2 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                                                          null);

            var at3 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(int) }
            },
                                                          null);

            var eq = new StructuralTypeEqualityComparer();

            Assert.IsFalse(eq.Equals(rt1, rt2));
            Assert.IsFalse(eq.Equals(rt2, rt3));
            Assert.IsFalse(eq.Equals(at1, at2));
            Assert.IsFalse(eq.Equals(at2, at3));
        }
        public void TypeSlimDerivationVisitor_AnonymousType_KeysReconstructed()
        {
            var anon = RuntimeCompiler.CreateAnonymousType(new[]
            {
                new KeyValuePair <string, Type>("foo", typeof(int)),
                new KeyValuePair <string, Type>("bar", typeof(int))
            }, new string[] { "bar" });

            Assert.IsNotNull(anon.GetProperty("bar"));
            Assert.IsNotNull(anon.GetProperty("foo"));

            var m = Expression.New(anon.GetConstructors().Single(), Expression.Constant(1), Expression.Constant(2));

            RoundtripAndAssertStructural(m);
        }
Exemplo n.º 8
0
        public void AnonymousType_PropertiesAttributes()
        {
            var expectedAttributeValue = "bing://foo";
            var customAttribute        = new CustomAttributeDeclaration(typeof(FooAttribute), new List <object> {
                expectedAttributeValue
            }.AsReadOnly());
            var props = new[] { new StructuralFieldDeclaration("Foo", typeof(int), new List <CustomAttributeDeclaration> {
                    customAttribute
                }.AsReadOnly()) };
            var type            = RuntimeCompiler.CreateAnonymousType(props);
            var property        = type.GetProperty("Foo");
            var actualAttribute = property.GetCustomAttribute <FooAttribute>(inherit: false);

            Assert.IsNotNull(actualAttribute);
            Assert.AreEqual(expectedAttributeValue, actualAttribute.Uri);
        }
Exemplo n.º 9
0
        public void AnonymousType_Visibility()
        {
            var props  = new[] { new KeyValuePair <string, Type>("bar", typeof(Bar)) };
            var props2 = new[] { new StructuralFieldDeclaration("bar", typeof(string), new List <CustomAttributeDeclaration> {
                    new CustomAttributeDeclaration(typeof(BarAttribute), new List <object>().AsReadOnly())
                }.AsReadOnly()) };
            var props3 = new[] { new StructuralFieldDeclaration("bar", typeof(Bar)) };

            Assert.ThrowsException <InvalidOperationException>(() => RuntimeCompiler.CreateAnonymousType(props));
            Assert.ThrowsException <InvalidOperationException>(() => { var rtc = new RuntimeCompiler(); var atb = rtc.GetNewAnonymousTypeBuilder(); rtc.DefineAnonymousType(atb, props); });

            Assert.ThrowsException <InvalidOperationException>(() => RuntimeCompiler.CreateAnonymousType(props2));
            Assert.ThrowsException <InvalidOperationException>(() => { var rtc = new RuntimeCompiler(); var atb = rtc.GetNewAnonymousTypeBuilder(); rtc.DefineAnonymousType(atb, props2); });

            Assert.ThrowsException <InvalidOperationException>(() => RuntimeCompiler.CreateAnonymousType(props3));
            Assert.ThrowsException <InvalidOperationException>(() => { var rtc = new RuntimeCompiler(); var atb = rtc.GetNewAnonymousTypeBuilder(); rtc.DefineAnonymousType(atb, props3); });
        }
        public void StructuralTypeSubstitutionExpressionVisitor_RecordToAnonymous_ThrowsInvalidOperation()
        {
            var type1 = RuntimeCompiler.CreateRecordType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) }
            }, valueEquality: true);

            var type2 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) }
            });

            var expr = Expression.Constant(Activator.CreateInstance(type1));

            var visitor = new StructuralTypeSubstitutionExpressionVisitor(new Dictionary <Type, Type> {
                { type1, type2 }
            });

            Assert.ThrowsException <InvalidOperationException>(() => visitor.Visit(expr));
        }
        public void StructuralTypeSubstitutionExpressionVisitor_NullValue_Success()
        {
            var type1 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) }
            });

            var type2 = RuntimeCompiler.CreateAnonymousType(new Dictionary <string, Type>
            {
                { "Foo", typeof(int) }
            });

            var expr = Expression.Constant(null, type1);

            var visitor = new StructuralTypeSubstitutionExpressionVisitor(new Dictionary <Type, Type> {
                { type1, type2 }
            });

            var result = visitor.Visit(expr);

            Assert.AreEqual(type2, result.Type);
        }
Exemplo n.º 12
0
 protected override Type MakeStructural(StructuralDataType type, ReadOnlyCollection <Tuple <string, Type> > properties)
 {
     return(RuntimeCompiler.CreateAnonymousType(properties.Select(p => new KeyValuePair <string, Type>(p.Item1, p.Item2))));
 }