Beispiel #1
0
        /// <summary>
        /// 设置属性的自定义特性。
        /// </summary>
        /// <param name="property"></param>
        /// <param name="dyPropertyBuilder"></param>
        private static void SetPropertyCustomAttributes(PropertyInfo property, DynamicPropertyBuilder dyPropertyBuilder)
        {
            var broAttr = property.GetCustomAttributes <BrowsableAttribute>().FirstOrDefault();
            var catAttr = property.GetCustomAttributes <CategoryAttribute>().FirstOrDefault();
            var desAttr = property.GetCustomAttributes <DescriptionAttribute>().FirstOrDefault();
            var defAttr = property.GetCustomAttributes <DefaultValueAttribute>().FirstOrDefault();
            var disAttr = property.GetCustomAttributes <DisplayNameAttribute>().FirstOrDefault();

            if (broAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <BrowsableAttribute>(broAttr.Browsable);
            }

            if (catAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <CategoryAttribute>(catAttr.Category);
            }

            if (desAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DescriptionAttribute>(desAttr.Description);
            }

            if (defAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DefaultValueAttribute>(defAttr.Value);
            }

            if (disAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DisplayNameAttribute>(disAttr.DisplayName);
            }
        }
        /// <summary>
        /// 设置属性的自定义特性。
        /// </summary>
        /// <param name="property"></param>
        /// <param name="dyPropertyBuilder"></param>
        private static void SetPropertyCustomAttributes(PropertyInfo property, DynamicPropertyBuilder dyPropertyBuilder)
        {
            var broAttr = property.GetCustomAttributes <BrowsableAttribute>().FirstOrDefault();
            var catAttr = property.GetCustomAttributes <CategoryAttribute>().FirstOrDefault();
            var desAttr = property.GetCustomAttributes <DescriptionAttribute>().FirstOrDefault();
            var defAttr = property.GetCustomAttributes <DefaultValueAttribute>().FirstOrDefault();
            var disAttr = property.GetCustomAttributes <DisplayNameAttribute>().FirstOrDefault();
            var ediAttr = property.GetCustomAttributes <EditorAttribute>().FirstOrDefault();

            if (broAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <BrowsableAttribute>(broAttr.Browsable);
            }

            if (catAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <CategoryAttribute>(catAttr.Category);
            }

            if (desAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DescriptionAttribute>(desAttr.Description);
            }

            if (defAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DefaultValueAttribute>(defAttr.Value);
            }

            if (disAttr != null)
            {
                dyPropertyBuilder.SetCustomAttribute <DisplayNameAttribute>(disAttr.DisplayName);
            }

            if (ediAttr != null)
            {
                var editorType = Type.GetType(ediAttr.EditorTypeName);
                if (editorType == null)
                {
                    editorType = compiledTypes.FirstOrDefault(s => s.AssemblyQualifiedName == ediAttr.EditorTypeName);
                }

                dyPropertyBuilder.SetCustomAttribute <EditorAttribute>(editorType, Type.GetType(ediAttr.EditorBaseTypeName));
            }
        }
        public static Type CompileDbContextType(Type dbContextType)
        {
            TypeBuilder        tb          = GetDbConntextTypeBuilder(dbContextType);
            ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);

            // NOTE: assuming your list contains Field objects with fields FieldName(string) and FieldType(Type)
            if (DynamicAssembly.Entities != null)
            {
                //Create Main   Entites
                Type myGeneric = typeof(DbSet <>);
                foreach (Model.DynamicEntity entity in DynamicAssembly.Entities)
                {
                    Type constructedClass = myGeneric.MakeGenericType(entity.EntityType);
                    DynamicPropertyBuilder.CreateProperty(tb, entity.Name, constructedClass);
                }
            }

            Type objectType = tb.CreateType();

            return(objectType);
        }