コード例 #1
0
 public void Non_Required_Value_Types_Are_Created_As_Nullable_Properties()
 {
     var attr = new XomNodeAttribute { Name = "attr1", Type = typeof(int), IsRequired = false};
     var type = XomAttributeTypeGenerator.GenerateType(new[] {attr}, "Test");
     var properties = type.GetProperties();
     Assert.IsTrue(properties[0].PropertyType == typeof(int?));
 }
コード例 #2
0
        public void Can_Specify_Name_For_Generated_Type()
        {
            var name = "TestName";
            var attr1 = new XomNodeAttribute { Name = "attr1", Type = typeof(string) };
            Type type = XomAttributeTypeGenerator.GenerateType(new[] {attr1}, name);

            Assert.AreEqual(name, type.Name, "Type's name was incorrect");
        }
コード例 #3
0
        public void Can_Generate_Class_Based_On_Xom_Attributes()
        {
            var attr1 = new XomNodeAttribute {Name = "attr1", Type = typeof (string)};
            var attr2 = new XomNodeAttribute {Name = "attr2", Type = typeof (int), IsRequired = true};

            Type type = XomAttributeTypeGenerator.GenerateType(new[] {attr1, attr2}, "TestName");

            Assert.IsNotNull(type, "Returned type was null");

            var properties = type.GetProperties();
            Assert.AreEqual(2, properties.Length, "Incorrect number of properties returned");
            Assert.IsTrue(properties.Any(x => x.PropertyType == typeof(string) && x.Name == attr1.Name), "No string property exists with the name attr1");
            Assert.IsTrue(properties.Any(x => x.PropertyType == typeof(int) && x.Name == attr2.Name), "No int property exists with the name attr2");
        }
コード例 #4
0
        private static void CreateProperty(TypeBuilder typeBuilder, XomNodeAttribute attribute)
        {
            var attributeType = attribute.Type;
            if (attributeType.IsValueType && !attribute.IsRequired)
                attributeType = typeof (Nullable<>).MakeGenericType(attributeType);

            var fieldBuilder = typeBuilder.DefineField("_" + attribute.Name, attributeType, FieldAttributes.Private);
            var propertyBuilder = typeBuilder.DefineProperty(attribute.Name, PropertyAttributes.HasDefault,
                                                             attributeType, null);

            var getPropMthdBldr = typeBuilder.DefineMethod("get_" + attribute.Name,
                                                           MethodAttributes.Public | MethodAttributes.SpecialName |
                                                           MethodAttributes.HideBySig, attributeType, Type.EmptyTypes);

            var getIl = getPropMthdBldr.GetILGenerator();

            getIl.Emit(OpCodes.Ldarg_0);
            getIl.Emit(OpCodes.Ldfld, fieldBuilder);
            getIl.Emit(OpCodes.Ret);

            MethodBuilder setPropMthdBldr =
                typeBuilder.DefineMethod("set_" + attribute.Name,
                  MethodAttributes.Public |
                  MethodAttributes.SpecialName |
                  MethodAttributes.HideBySig,
                  null, new[] { attributeType });

            ILGenerator setIl = setPropMthdBldr.GetILGenerator();
            Label modifyProperty = setIl.DefineLabel();
            Label exitSet = setIl.DefineLabel();

            setIl.MarkLabel(modifyProperty);
            setIl.Emit(OpCodes.Ldarg_0);
            setIl.Emit(OpCodes.Ldarg_1);
            setIl.Emit(OpCodes.Stfld, fieldBuilder);

            setIl.Emit(OpCodes.Nop);
            setIl.MarkLabel(exitSet);
            setIl.Emit(OpCodes.Ret);

            propertyBuilder.SetGetMethod(getPropMthdBldr);
            propertyBuilder.SetSetMethod(setPropMthdBldr);
        }
コード例 #5
0
 public void Null_Name_Creates_Guid_Type_Name()
 {
     var attr1 = new XomNodeAttribute { Name = "attr1", Type = typeof(string) };
     var type = XomAttributeTypeGenerator.GenerateType(new[] { attr1 }, null);
     Guid.Parse(type.Name);
 }