Ejemplo n.º 1
0
        protected virtual object GenerateAnonymousData(object entity, Type propertyType, string propertyName, int hierarchyDepth)
        {
            if (propertyType == typeof(string))
            {
                return(AnonymousData.String(propertyName));
            }

            if (propertyType == typeof(sbyte) || propertyType == typeof(byte) || propertyType == typeof(Byte) || propertyType == typeof(SByte))
            {
                return(AnonymousData.Byte());
            }

            if (propertyType == typeof(short) || propertyType == typeof(ushort) || propertyType == typeof(Int16) || propertyType == typeof(UInt16))
            {
                return(AnonymousData.Short());
            }

            if (propertyType == typeof(int) || propertyType == typeof(uint) || propertyType == typeof(Int32) || propertyType == typeof(UInt32))
            {
                return(AnonymousData.Int(3, false));
            }

            if (propertyType == typeof(long) || propertyType == typeof(ulong) || propertyType == typeof(Int64) || propertyType == typeof(UInt64))
            {
                return(AnonymousData.Long());
            }

            if (propertyType == typeof(double) || propertyType == typeof(Double))
            {
                return(AnonymousData.Double());
            }

            if (propertyType == typeof(decimal) || propertyType == typeof(Decimal))
            {
                return(AnonymousData.Decimal());
            }

            if (propertyType == typeof(float) || propertyType == typeof(Single))
            {
                return(AnonymousData.Float());
            }

            if (propertyType == typeof(char) || propertyType == typeof(Char))
            {
                return(AnonymousData.Char());
            }

            if (propertyType == typeof(DateTime))
            {
                return(AnonymousData.DateTime());
            }

            if (propertyType == typeof(TimeSpan))
            {
                return(AnonymousData.TimeSpan());
            }

            if (propertyType.IsValueType)
            {
                return(Activator.CreateInstance(propertyType));
            }

            if (hierarchyDepth > 0 && propertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(propertyType))
            {
                var genericTypeArgument = propertyType.GenericTypeArguments.FirstOrDefault();
                if (!genericTypeArgument.IsClass || genericTypeArgument.IsGenericType)
                {
                    return(null);
                }
                var listOfChildEntities = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericTypeArgument));

                try
                {
                    for (var i = 1; i <= NumberOfNestedEntitiesInCollections; i++)
                    {
                        var childEntity = GenerateAnonymousDateForChildEntityObject(null, genericTypeArgument, propertyName, hierarchyDepth);
                        listOfChildEntities.Add(childEntity);
                    }
                    return(listOfChildEntities);
                }
                catch (Exception)
                {
                    return((IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(genericTypeArgument)));
                }
            }

            if (hierarchyDepth > 0 && propertyType.IsClass && !propertyType.IsGenericType)
            {
                return(GenerateAnonymousDateForChildEntityObject(entity, propertyType, propertyName, hierarchyDepth));
            }

            return(null);
        }