コード例 #1
0
        public void ShouldGenerateTypeFromData()
        {
            Dictionary <string, Type> typeDef = new Dictionary <string, Type>
            {
                { "StringProp", typeof(String) },
                { "IntProp", typeof(int) },
                { "DateProp", typeof(DateTime) },
                { "DoubleNullableProp", typeof(double?) }
            };

            Type t = TypeGenerator.MakeType("NewTypeA", typeDef);

            Assert.That("NewTypeA", Is.EqualTo(t.Name));

            var pi = t.GetField("StringProp");

            Assert.That(typeof(string), Is.EqualTo(pi.FieldType));

            pi = t.GetField("IntProp");
            Assert.That(typeof(int), Is.EqualTo(pi.FieldType));

            pi = t.GetField("DateProp");
            Assert.That(typeof(DateTime), Is.EqualTo(pi.FieldType));

            pi = t.GetField("DoubleNullableProp");
            Assert.That(typeof(double?), Is.EqualTo(pi.FieldType));

            IObjectAccessor a = (IObjectAccessor)Activator.CreateInstance(t);

            a["StringProp"] = "test";
            string s = (string)a["StringProp"];

            Assert.That(s, Is.EqualTo("test"));
        }
コード例 #2
0
        public void ShouldGenerateUniqueTypeIfNameEmpty()
        {
            Dictionary <string, Type> typeDef = new Dictionary <string, Type>
            {
                { "StringProp", typeof(String) },
                { "IntProp", typeof(int) },
                { "DateProp", typeof(DateTime) },
                { "DoubleNullableProp", typeof(double?) }
            };

            var t1 = TypeGenerator.MakeType(null, typeDef);
            var t2 = TypeGenerator.MakeType(null, typeDef);

            Assert.That(t1.Name, Is.Not.EqualTo(t2.Name));
        }
コード例 #3
0
        public void ShouldCheckIfTypeAlreadyExists()
        {
            Dictionary <string, Type> typeDef = new Dictionary <string, Type>
            {
                { "StringProp", typeof(String) },
                { "IntProp", typeof(int) },
                { "DateProp", typeof(DateTime) },
                { "DoubleNullableProp", typeof(double?) }
            };

            Type t = TypeGenerator.MakeType("TestClassA", typeDef);

            Assert.That(t, Is.Not.Null);

            Type t2 = TypeGenerator.GetType("TestClassA");

            Assert.That(t2, Is.Not.Null);
            Assert.Throws <ArgumentException>(() => TypeGenerator.MakeType("TestClassA", typeDef));

            Assert.That(t2, Is.EqualTo(t));
        }