示例#1
0
        private KeyValuePair <Models.Relationship, PropertyBuilder> CreateOneToManyProperty(Models.Relationship oneToMany,
                                                                                            TypeBuilder typeBuilder,
                                                                                            IEnumerable <Type> knowEntities)
        {
            var type           = knowEntities.Where(c => c.Name == oneToMany.Many.Name).Single();
            var collectionType = typeof(ICollection <>).MakeGenericType(type);

            var fieldInfo = FieldHelper.CreatePrivateField(typeBuilder, collectionType,
                                                           $"__navProp_{oneToMany.Many.Name}");

            var propertyBuilder = PropertyHelper.CreateProperty(typeBuilder, oneToMany.ManyNavigationName,
                                                                collectionType, PropertyHelper.PropertyGetSet.Both);

            var hashSetType = typeof(HashSet <>).MakeGenericType(type);

            var ctors = typeof(HashSet <>).GetConstructor(Type.EmptyTypes);
            var hashSetConstructor = TypeBuilder.GetConstructor(hashSetType, ctors);

            GenerateOneToManyGetPropertyBody(propertyBuilder.GetMethod as MethodBuilder, fieldInfo, hashSetConstructor);
            GenerateOneToManySetPropertyBody(propertyBuilder.SetMethod as MethodBuilder, fieldInfo);

            // Apply InverseProperty Attribute
            var inverseProxyAttrConstruct = typeof(InversePropertyAttribute).GetConstructor(new Type[] { typeof(string) });
            var inverseProxyBuilder       = new CustomAttributeBuilder(inverseProxyAttrConstruct, new string[] { oneToMany.OneNavigationName });

            propertyBuilder.SetCustomAttribute(inverseProxyBuilder);

            return(new KeyValuePair <Models.Relationship, PropertyBuilder>(oneToMany, propertyBuilder));
        }
示例#2
0
        private KeyValuePair <Models.Relationship, PropertyBuilder> CreateManyToOneProperty(Models.Relationship manyToOne,
                                                                                            TypeBuilder typeBuilder,
                                                                                            IEnumerable <Type> knowEntities)
        {
            Type            type            = knowEntities.Where(c => c.Name == manyToOne.One.Name).Single();
            PropertyBuilder propertyBuilder = PropertyHelper.CreateAutoProperty(typeBuilder, manyToOne.OneNavigationName,
                                                                                type, PropertyHelper.PropertyGetSet.Both);

            return(new KeyValuePair <Models.Relationship, PropertyBuilder>(manyToOne, propertyBuilder));
        }