Ejemplo n.º 1
0
        public static List <PropertyRoute> GenerateRoutes(Type type)
        {
            PropertyRoute        root   = PropertyRoute.Root(type);
            List <PropertyRoute> result = new List <PropertyRoute>();

            result.Add(root.Add(piId));

            foreach (PropertyInfo pi in Reflector.PublicInstancePropertiesInOrder(type))
            {
                PropertyRoute route = root.Add(pi);
                result.Add(route);

                if (Reflector.IsEmbeddedEntity(pi.PropertyType))
                {
                    result.AddRange(GenerateEmbeddedProperties(route));
                }

                if (Reflector.IsMList(pi.PropertyType))
                {
                    Type colType = pi.PropertyType.ElementType();
                    if (Reflector.IsEmbeddedEntity(colType))
                    {
                        result.AddRange(GenerateEmbeddedProperties(route.Add("Item")));
                    }
                }
            }

            foreach (var t in MixinDeclarations.GetMixinDeclarations(type))
            {
                result.AddRange(GenerateEmbeddedProperties(root.Add(t)));
            }

            return(result);
        }
Ejemplo n.º 2
0
        MemberInfo GetMember(string fieldOrProperty)
        {
            if (fieldOrProperty.StartsWith("["))
            {
                var mixinName = ExtractMixin(fieldOrProperty);

                return(MixinDeclarations.GetMixinDeclarations(Type).FirstOrDefault(t => t.Name == mixinName) ??
                       throw new InvalidOperationException("{0}{1} does not exist".FormatWith(this, fieldOrProperty)));
            }
            else
            {
                var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

                return((MemberInfo?)Type.GetProperty(fieldOrProperty, flags, null, null, IsCollection() ? new[] { typeof(int) } : new Type[0], null) ??
                       (MemberInfo?)Type.GetField(fieldOrProperty, flags) ??
                       throw new InvalidOperationException("{0}.{1} does not exist".FormatWith(this, fieldOrProperty)));
            }
        }
Ejemplo n.º 3
0
        MemberInfo GetMember(string fieldOrProperty)
        {
            MemberInfo mi = (MemberInfo)Type.GetProperty(fieldOrProperty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, null, IsCollection() ? new[] { typeof(int) } : new Type[0], null) ??
                            (MemberInfo)Type.GetField(fieldOrProperty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (mi == null && Type.IsEntity())
            {
                string?name = ExtractMixin(fieldOrProperty);

                mi = MixinDeclarations.GetMixinDeclarations(Type).FirstOrDefault(t => t.Name == name);
            }

            if (mi == null)
            {
                throw new InvalidOperationException("{0}.{1} does not exist".FormatWith(this, fieldOrProperty));
            }

            return(mi);
        }
Ejemplo n.º 4
0
 public Entity()
 {
     mixin = MixinDeclarations.CreateMixins(this);
 }
Ejemplo n.º 5
0
 internal ModifiableEntity()
 {
     mixin = MixinDeclarations.CreateMixins(this);
 }
Ejemplo n.º 6
0
        void SetParentAndProperty(PropertyRoute parent, MemberInfo fieldOrProperty)
        {
            if (fieldOrProperty == null)
            {
                throw new ArgumentNullException("fieldOrProperty");
            }

            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            this.Parent = parent;

            if (parent.Type.IsIEntity() && parent.PropertyRouteType != PropertyRouteType.Root)
            {
                throw new ArgumentException("Parent can not be a non-root Identifiable");
            }

            if (fieldOrProperty is PropertyInfo && Reflector.IsMList(parent.Type))
            {
                if (fieldOrProperty.Name != "Item")
                {
                    throw new NotSupportedException("PropertyInfo {0} is not supported".FormatWith(fieldOrProperty.Name));
                }

                PropertyInfo      = (PropertyInfo)fieldOrProperty;
                PropertyRouteType = PropertyRouteType.MListItems;
            }
            else if (fieldOrProperty is PropertyInfo && parent.Type.IsLite())
            {
                if (fieldOrProperty.Name != "Entity" && fieldOrProperty.Name != "EntityOrNull")
                {
                    throw new NotSupportedException("PropertyInfo {0} is not supported".FormatWith(fieldOrProperty.Name));
                }

                PropertyInfo      = (PropertyInfo)fieldOrProperty;
                PropertyRouteType = PropertyRouteType.LiteEntity;
            }
            else if (typeof(Entity).IsAssignableFrom(parent.type) && fieldOrProperty is Type)
            {
                MixinDeclarations.AssertDeclared(parent.type, (Type)fieldOrProperty);

                type = (Type)fieldOrProperty;
                PropertyRouteType = PropertyRouteType.Mixin;
            }
            else if (typeof(ModifiableEntity).IsAssignableFrom(parent.Type) || typeof(IRootEntity).IsAssignableFrom(parent.Type))
            {
                PropertyRouteType = PropertyRouteType.FieldOrProperty;
                if (fieldOrProperty is PropertyInfo)
                {
                    if (!parent.Type.Follow(a => a.BaseType).Contains(fieldOrProperty.DeclaringType))
                    {
                        var pi = (PropertyInfo)fieldOrProperty;

                        if (!parent.Type.GetInterfaces().Contains(fieldOrProperty.DeclaringType))
                        {
                            throw new ArgumentException("PropertyInfo {0} not found on {1}".FormatWith(pi.PropertyName(), parent.Type));
                        }

                        var otherProperty = parent.Type.Follow(a => a.BaseType)
                                            .Select(a => a.GetProperty(fieldOrProperty.Name, BindingFlags.Public | BindingFlags.Instance, null, null, new Type[0], null)).NotNull().FirstEx();

                        if (otherProperty == null)
                        {
                            throw new ArgumentException("PropertyInfo {0} not found on {1}".FormatWith(pi.PropertyName(), parent.Type));
                        }

                        fieldOrProperty = otherProperty;
                    }

                    PropertyInfo = (PropertyInfo)fieldOrProperty;
                    FieldInfo    = Reflector.TryFindFieldInfo(Parent.Type, PropertyInfo);
                }
                else if (fieldOrProperty is MethodInfo && ((MethodInfo)fieldOrProperty).Name == "ToString")
                {
                    FieldInfo    = (FieldInfo)fiToStr;
                    PropertyInfo = null;
                }
                else
                {
                    FieldInfo    = (FieldInfo)fieldOrProperty;
                    PropertyInfo = Reflector.TryFindPropertyInfo(FieldInfo);
                }
            }
            else
            {
                throw new NotSupportedException("Properties of {0} not supported".FormatWith(parent.Type));
            }
        }