Exemple #1
0
        private Type CreateCustomEmitType(CustomWrapperModel customWrapperModel)
        {
            var dyClass = EmitHelper.CreateClass(customWrapperModel.ModelName,
                                                 MiCakeReflectionPredefined.DynamicAssembly,
                                                 MiCakeReflectionPredefined.DynamicModule,
                                                 baseType: typeof(BaseResultDataWrapper));

            foreach (var customerProperty in customWrapperModel.GetAllConfigProperties())
            {
                CheckValue.NotNullOrEmpty(customerProperty.Key, nameof(customerProperty.Key));
                EmitHelper.CreateProperty(dyClass, customerProperty.Key, typeof(object));
            }

            return(dyClass.CreateType());
        }
        public void CreatepProperty_Test()
        {
            var typeBuilder = EmitHelper.CreateClass("TestClass");

            EmitHelper.CreateProperty(typeBuilder, "Name", typeof(string));

            var type     = typeBuilder.CreateType();
            var instance = Activator.CreateInstance(type);

            var propertyInfo = instance.GetType().GetProperty("Name");

            Assert.NotNull(propertyInfo);

            propertyInfo.SetValue(instance, "MiCake");
            Assert.Equal("MiCake", propertyInfo.GetValue(instance));
        }
        public void CreateClass_Test()
        {
            var type = EmitHelper.CreateClass("TestClass").CreateType();

            Assert.Equal("TestClass", type.Name);
        }