public void AnonymousType_Nested()
        {
            var rtc = new RuntimeCompiler();

            var atb1 = rtc.GetNewAnonymousTypeBuilder();
            var atb2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineAnonymousType(atb1, new[]
            {
                new KeyValuePair <string, Type>("Qux", typeof(int)),
            });

            rtc.DefineAnonymousType(atb2, new[]
            {
                new KeyValuePair <string, Type>("Foo", atb1),
                new KeyValuePair <string, Type>("Baz", typeof(int)),
            }, new[] { "Foo" });

            var foo = atb1.CreateType();
            var bar = atb2.CreateType();

            var objFoo1 = Activator.CreateInstance(foo, new object[] { 42 });
            var objFoo2 = Activator.CreateInstance(foo, new object[] { 42 });

            var objBar1 = Activator.CreateInstance(bar, new object[] { objFoo1, 43 });
            var objBar2 = Activator.CreateInstance(bar, new object[] { objFoo2, 44 });

            Assert.AreEqual(objBar1, objBar2);
            Assert.AreEqual("{ Foo = { Qux = 42 }, Baz = 43 }", objBar1.ToString());
            Assert.AreEqual("{ Foo = { Qux = 42 }, Baz = 44 }", objBar2.ToString());
        }
Esempio n. 2
0
        public void AnonymousTypeTupletizer_RecursiveType()
        {
            var rtc = new RuntimeCompiler();

            var atb1 = rtc.GetNewAnonymousTypeBuilder();
            var atb2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineAnonymousType(atb1, new[]
            {
                new KeyValuePair <string, Type>("Qux", typeof(int)),
                new KeyValuePair <string, Type>("Bar", atb2),
            }, Array.Empty <string>());

            rtc.DefineAnonymousType(atb2, new[]
            {
                new KeyValuePair <string, Type>("Baz", typeof(int)),
                new KeyValuePair <string, Type>("Foo", atb1),
            }, Array.Empty <string>());

            var foo = atb1.CreateType();
            var bar = atb2.CreateType();

            var e = Expression.New(foo.GetConstructors().Single(), Expression.Constant(1), Expression.Constant(value: null, bar));

            Assert.ThrowsException <NotSupportedException>(() => AnonymousTypeTupletizer.Tupletize(e, Expression.Constant(value: null)));
        }
Esempio n. 3
0
        public void StructuralTypeEqualityComparer_ManOrBoy_Failure()
        {
            var rtc = new RuntimeCompiler();
            var rt1 = rtc.GetNewRecordTypeBuilder();
            var rt2 = rtc.GetNewRecordTypeBuilder();
            var at1 = rtc.GetNewAnonymousTypeBuilder();
            var at2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineRecordType(
                rt1,
                new Dictionary <string, Type>
            {
                { "Self", rt1 },
                { "Copy", rt2 },
                { "Other", typeof(int) }
            },
                true);

            rtc.DefineRecordType(
                rt2,
                new Dictionary <string, Type>
            {
                { "Self", rt2 },
                { "Copy", rt1 },
                { "Other", typeof(string) }
            },
                true);

            rtc.DefineAnonymousType(
                at1,
                new Dictionary <string, Type>
            {
                { "Self", at1 },
                { "Copy", at2 },
                { "Other", typeof(int) }
            },
                null);

            rtc.DefineAnonymousType(
                at2,
                new Dictionary <string, Type>
            {
                { "Self", at2 },
                { "Copy", at1 },
                { "Other", typeof(string) }
            },
                null);

            rt1.CreateType();
            rt2.CreateType();
            at1.CreateType();
            at2.CreateType();

            var eq = new StructuralTypeEqualityComparer();

            Assert.IsFalse(eq.Equals(rt1, rt2));
            Assert.IsFalse(eq.Equals(at1, at2));
        }
        public void AnonymousType_UnknownKey()
        {
            var rtc = new RuntimeCompiler();

            var atb = rtc.GetNewAnonymousTypeBuilder();

            AssertEx.ThrowsException <ArgumentException>(() => rtc.DefineAnonymousType(atb, new[] { new KeyValuePair <string, Type>("Bar", typeof(int)) }, "Baz"), ex => Assert.AreEqual("keys", ex.ParamName));
        }
        public void AnonymousType_Recursive()
        {
            var rtc = new RuntimeCompiler();

            var atb1 = rtc.GetNewAnonymousTypeBuilder();
            var atb2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineAnonymousType(atb1, new[]
            {
                new KeyValuePair <string, Type>("Qux", typeof(int)),
                new KeyValuePair <string, Type>("Bar", atb2),
            }, Array.Empty <string>());

            rtc.DefineAnonymousType(atb2, new[]
            {
                new KeyValuePair <string, Type>("Baz", typeof(int)),
                new KeyValuePair <string, Type>("Foo", atb1),
            }, Array.Empty <string>());

            var foo = atb1.CreateType();
            var bar = atb2.CreateType();

            var objFoo = Activator.CreateInstance(foo, new object[] { 42, null });
            var objBar = Activator.CreateInstance(bar, new object[] { 43, null });

            var fooBar = foo.GetProperty("Bar");

            fooBar.SetValue(objFoo, objBar);

            var barFoo = bar.GetProperty("Foo");

            barFoo.SetValue(objBar, objFoo);

            Assert.AreSame(fooBar.GetValue(objFoo), objBar);
            Assert.AreSame(barFoo.GetValue(objBar), objFoo);
        }
        public void AnonymousType_CSharp_StructuralFields()
        {
            AnonymousType_CSharp_Impl(() =>
            {
                var rtc = new RuntimeCompiler();

                var atb = rtc.GetNewAnonymousTypeBuilder();

                rtc.DefineAnonymousType(atb, new[]
                {
                    new StructuralFieldDeclaration("Name", typeof(string)),
                    new StructuralFieldDeclaration("Age", typeof(int)),
                });

                return(atb.CreateType());
            });
        }
        public void AnonymousType_CSharp()
        {
            AnonymousType_CSharp_Impl(() =>
            {
                var rtc = new RuntimeCompiler();

                var atb = rtc.GetNewAnonymousTypeBuilder();

                rtc.DefineAnonymousType(atb, new[]
                {
                    new KeyValuePair <string, Type>("Name", typeof(string)),
                    new KeyValuePair <string, Type>("Age", typeof(int)),
                });

                return(atb.CreateType());
            });
        }
        public void DefineAnonymousType_ArgumentChecks()
        {
            var rtc = new RuntimeCompiler();

            var atb = rtc.GetNewAnonymousTypeBuilder();

            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(anonymousTypeBuilder: null, Array.Empty <KeyValuePair <string, Type> >()), ex => Assert.AreEqual("anonymousTypeBuilder", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(atb, (KeyValuePair <string, Type>[])null), ex => Assert.AreEqual("properties", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(anonymousTypeBuilder: null, Array.Empty <KeyValuePair <string, Type> >(), Array.Empty <string>()), ex => Assert.AreEqual("anonymousTypeBuilder", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(atb, (KeyValuePair <string, Type>[])null, Array.Empty <string>()), ex => Assert.AreEqual("properties", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(anonymousTypeBuilder: null, Array.Empty <StructuralFieldDeclaration>()), ex => Assert.AreEqual("anonymousTypeBuilder", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(atb, (StructuralFieldDeclaration[])null), ex => Assert.AreEqual("properties", ex.ParamName));

            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(anonymousTypeBuilder: null, Array.Empty <StructuralFieldDeclaration>(), Array.Empty <string>()), ex => Assert.AreEqual("anonymousTypeBuilder", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => rtc.DefineAnonymousType(atb, (StructuralFieldDeclaration[])null, Array.Empty <string>()), ex => Assert.AreEqual("properties", ex.ParamName));
        }
        public Tests DataModelSerializerFactory_StructuralTypeCycle_Anonymous_Tests()
        {
            var rtc = new RuntimeCompiler();
            var atb = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineAnonymousType(atb, new Dictionary <string, Type>
            {
                { "Self", atb },
            });
            var at    = atb.CreateType();
            var inner = Activator.CreateInstance(at, new object[] { null });
            var outer = Activator.CreateInstance(at, new object[] { inner });

            var tests = new Tests
            {
                { "AnonymousTypeWithCycle", at, outer, DataTypeObjectEqualityComparer.Default },
            };

            return(tests);
        }
Esempio n. 10
0
        public void AnonymousType_NoKeys()
        {
            var rtc = new RuntimeCompiler();

            var atb = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineAnonymousType(atb, Array.Empty <KeyValuePair <string, Type> >());

            var ant = atb.CreateType();

            Assert.IsTrue(ant.IsAnonymousType());

            Assert.IsTrue(ant.IsDefined(typeof(CompilerGeneratedAttribute), inherit: false));

            var ctors = ant.GetConstructors();

            Assert.AreEqual(1, ctors.Length);

            var ctorParams = ctors[0].GetParameters();

            Assert.AreEqual(0, ctorParams.Length);

            var props = ant.GetProperties();

            Assert.AreEqual(0, props.Length);

            var foo = Activator.CreateInstance(ant);

            Assert.AreEqual("{ }", foo.ToString());

            var bar = Activator.CreateInstance(ant);

            Assert.IsTrue(foo.Equals(bar));
            Assert.IsTrue(bar.Equals(foo));
            Assert.AreEqual(bar.GetHashCode(), foo.GetHashCode());
        }
 /// <summary>
 /// Gets a type builder to later use to define a type.
 /// </summary>
 /// <param name="rtc">The runtime compiler to get the type builder from.</param>
 /// <returns>The type builder.</returns>
 protected override TypeBuilder GetTypeBuilder(RuntimeCompiler rtc) => rtc.GetNewAnonymousTypeBuilder();
Esempio n. 12
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); });
        }
Esempio n. 13
0
        public void StructuralTypeEqualityComparer_ManOrBoy1_Success()
        {
            var rtc = new RuntimeCompiler();
            var rt1 = rtc.GetNewRecordTypeBuilder();
            var rt2 = rtc.GetNewRecordTypeBuilder();
            var at1 = rtc.GetNewAnonymousTypeBuilder();
            var at2 = rtc.GetNewAnonymousTypeBuilder();

            rtc.DefineRecordType(
                rt1,
                new Dictionary <string, Type>
            {
                { "Self", rt1 },
                { "Copy", rt2 },
                { "Other", at1 }
            },
                true);

            rtc.DefineRecordType(
                rt2,
                new Dictionary <string, Type>
            {
                { "Self", rt2 },
                { "Copy", rt1 },
                { "Other", at2 }
            },
                true);

            rtc.DefineAnonymousType(
                at1,
                new Dictionary <string, Type>
            {
                { "Self", at1 },
                { "Copy", at2 },
                { "Other", rt1 }
            },
                null);

            rtc.DefineAnonymousType(
                at2,
                new Dictionary <string, Type>
            {
                { "Self", at2 },
                { "Copy", at1 },
                { "Other", rt2 }
            },
                null);

            var rt1c = rt1.CreateType();
            var rt2c = rt2.CreateType();
            var at1c = at1.CreateType();
            var at2c = at2.CreateType();

            var eq   = new StructuralTypeEqualityComparer();
            var orig = new TypeEqualityComparer();

            Assert.IsFalse(orig.Equals(rt1c, rt2c));
            Assert.IsTrue(eq.Equals(rt1c, rt2c));
            Assert.IsFalse(orig.Equals(at1c, at2c));
            Assert.IsTrue(eq.Equals(at1c, at2c));
        }