Example #1
0
        static string GetPropertyExpression(MemberExpression memberInfo)
        {
            // Handle the member:
            var property = memberInfo.Member as PropertyInfo;

            if (property == null)
            {
                return(null);
            }

            // Fix for overriden properties:
            try { property = memberInfo.Expression.Type.GetProperty(property.Name) ?? property; }
            catch { }

            if (CalculatedAttribute.IsCalculated(property))
            {
                return(null);
            }
            if (memberInfo.Expression.Type.IsNullable())
            {
                return(property.Name);
            }
            if (!property.DeclaringType.Implements <IEntity>())
            {
                return(null);
            }

            // Handle the "member owner" expression:
            if (IsSimpleParameter(memberInfo.Expression))
            {
                if (IsForeignKey(property))
                {
                    return(property.Name.TrimEnd(2));
                }

                return(property.Name);
            }
            else if (memberInfo.Expression is MemberExpression)
            {
                // The expression is itself a member of something.

                var parentProperty = GetPropertyExpression(memberInfo.Expression as MemberExpression);
                if (parentProperty == null)
                {
                    return(null);
                }
                else
                {
                    return($"{parentProperty}.{property.Name}");
                }
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Extracts the primitive properties of a specified type.
        /// </summary>
        static PropertyInfo[] ExtractPrimitiveProperties(Type type)
        {
            var result         = new List <PropertyInfo>();
            var primitiveTypes = new[] { typeof(string), typeof(int), typeof(int?), typeof(double), typeof(double?), typeof(DateTime), typeof(DateTime?) };

            foreach (var p in type.GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(p => p.CanRead).Where(p => primitiveTypes.Contains(p.PropertyType)))
            {
                if (p.Name == nameof(IsNew))
                {
                    continue;
                }
                if (p.PropertyType.Implements <IEntity>())
                {
                    continue;
                }
                if (CalculatedAttribute.IsCalculated(p))
                {
                    continue;
                }
                result.Add(p);
            }

            return(result.ToArray());
        }