コード例 #1
0
        object GenerateValueOrEmpty(Type propertyType, bool isEmpty)
        {
            object value = null;

            if (propertyType.IsEnum)
            {
                value = isEmpty ? 0 : Pleasure.Generator.EnumAsInt(propertyType);
            }
            else if (propertyType.IsAnyEquals(typeof(string), typeof(object)))
            {
                value = isEmpty ? string.Empty : Pleasure.Generator.String();
            }
            else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                // ReSharper disable SimplifyConditionalTernaryExpression
                value = isEmpty ? false : Pleasure.Generator.Bool();
            }
            // ReSharper restore SimplifyConditionalTernaryExpression
            else if (propertyType.IsAnyEquals(typeof(int), typeof(int?)))
            {
                value = isEmpty ? default(int) : Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(long), typeof(long?)))
            {
                value = isEmpty ? default(long) : (long)Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(float), typeof(float?)))
            {
                value = isEmpty ? default(float) : Pleasure.Generator.PositiveFloating();
            }
            else if (propertyType.IsAnyEquals(typeof(decimal), typeof(decimal?)))
            {
                value = isEmpty ? default(decimal) : Pleasure.Generator.PositiveDecimal();
            }
            else if (propertyType.IsAnyEquals(typeof(double), typeof(double?)))
            {
                value = isEmpty ? default(double) : (double)Pleasure.Generator.PositiveFloating();
            }
            else if (propertyType == typeof(byte) || propertyType == typeof(byte?))
            {
                value = isEmpty ? default(byte) : (byte)Pleasure.Generator.PositiveNumber();
            }
            else if (propertyType == typeof(char) || propertyType == typeof(char?))
            {
                value = isEmpty ? default(char) : Pleasure.Generator.String()[0];
            }
            else if (propertyType == typeof(DateTime))
            {
                value = isEmpty ? new DateTime() : Pleasure.Generator.DateTime();
            }
            else if (propertyType == typeof(TimeSpan))
            {
                value = isEmpty ? new TimeSpan() : Pleasure.Generator.TimeSpan();
            }
            else if (propertyType.IsAnyEquals(typeof(Stream), typeof(MemoryStream)))
            {
                value = isEmpty ? Pleasure.Generator.Stream(0) : Pleasure.Generator.Stream();
            }
            else if (propertyType == typeof(byte[]))
            {
                value = isEmpty ? Pleasure.ToArray <byte>() : Pleasure.Generator.Bytes();
            }
            else if (propertyType == typeof(Guid) || propertyType == typeof(Guid?))
            {
                value = isEmpty ? Guid.Empty : Guid.NewGuid();
            }
            else if (propertyType == typeof(int[]))
            {
                value = isEmpty ? Pleasure.ToArray <int>() : Pleasure.ToArray(Pleasure.Generator.PositiveNumber(1));
            }
            else if (propertyType == typeof(string[]))
            {
                value = isEmpty ? Pleasure.ToArray <string>() : Pleasure.ToArray(Pleasure.Generator.String());
            }
            else if (propertyType.IsAnyEquals(typeof(HttpPostedFile), typeof(HttpPostedFileBase)))
            {
                value = isEmpty ? null : Pleasure.Generator.HttpPostedFile();
            }
            else if (propertyType == typeof(Dictionary <string, string>))
            {
                value = isEmpty ? new Dictionary <string, string>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() });
            }
            else if (propertyType == typeof(Dictionary <string, object>))
            {
                value = isEmpty ? new Dictionary <string, object>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }).ToDictionary(r => r.Key, r => (object)r.Value);
            }

            return(value);
        }
コード例 #2
0
        object GenerateValueOrEmpty(Type propertyType, bool isEmpty)
        {
            object value      = null;
            bool   isNullable = propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>);

            propertyType = isNullable ? propertyType.GetGenericArguments()[0] : propertyType;

            if (propertyType.IsEnum)
            {
                value = isEmpty ? 0 : Enum.Parse(propertyType, Pleasure.Generator.EnumAsInt(propertyType).ToString(), true);
            }
            else if (propertyType.IsAnyEquals(typeof(string), typeof(object)))
            {
                value = isEmpty ? string.Empty : Pleasure.Generator.String();
            }
            else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
            {
                // ReSharper disable SimplifyConditionalTernaryExpression
                value = isEmpty ? false : Pleasure.Generator.Bool();
            }
            // ReSharper restore SimplifyConditionalTernaryExpression
            else if (propertyType.IsAnyEquals(typeof(int)))
            {
                value = isEmpty ? default(int) : Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(long)))
            {
                value = isEmpty ? default(long) : (long)Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(short)))
            {
                value = isEmpty ? default(short) : (short)Pleasure.Generator.PositiveNumber(1);
            }
            else if (propertyType.IsAnyEquals(typeof(float)))
            {
                value = isEmpty ? default(float) : Pleasure.Generator.PositiveFloating();
            }
            else if (propertyType.IsAnyEquals(typeof(decimal)))
            {
                value = isEmpty ? default(decimal) : Pleasure.Generator.PositiveDecimal();
            }
            else if (propertyType.IsAnyEquals(typeof(double)))
            {
                value = isEmpty ? default(double) : Pleasure.Generator.PositiveDouble();
            }
            else if (propertyType.IsAnyEquals(typeof(byte), typeof(sbyte)))
            {
                value = isEmpty ? default(byte) : (byte)Pleasure.Generator.PositiveNumber();
            }
            else if (propertyType == typeof(char))
            {
                value = isEmpty ? default(char) : Pleasure.Generator.String()[0];
            }
            else if (propertyType.IsAnyEquals(typeof(DateTime)))
            {
                value = isEmpty ? new DateTime() : Pleasure.Generator.DateTime();
            }
            else if (propertyType.IsAnyEquals(typeof(TimeSpan)))
            {
                value = isEmpty ? new TimeSpan() : Pleasure.Generator.TimeSpan();
            }
            else if (propertyType.IsAnyEquals(typeof(Stream), typeof(MemoryStream)))
            {
                value = isEmpty ? Pleasure.Generator.Stream(0) : Pleasure.Generator.Stream();
            }
            else if (propertyType == typeof(byte[]))
            {
                value = isEmpty ? Pleasure.ToArray <byte>() : Pleasure.Generator.Bytes();
            }
            else if (propertyType == typeof(Guid))
            {
                value = isEmpty ? Guid.Empty : Guid.NewGuid();
            }
            else if (propertyType == typeof(int[]))
            {
                value = isEmpty ? Pleasure.ToArray <int>() : Pleasure.ToArray(Pleasure.Generator.PositiveNumber(1));
            }
            else if (propertyType == typeof(string[]))
            {
                value = isEmpty ? Pleasure.ToArray <string>() : Pleasure.ToArray(Pleasure.Generator.String());
            }
            else if (propertyType.IsAnyEquals(typeof(HttpPostedFile), typeof(HttpPostedFileBase)))
            {
                value = isEmpty ? null : Pleasure.Generator.HttpPostedFile();
            }
            else if (propertyType == typeof(Dictionary <string, string>))
            {
                value = isEmpty ? new Dictionary <string, string>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() });
            }
            else if (propertyType == typeof(Dictionary <string, object>))
            {
                value = isEmpty ? new Dictionary <string, object>() : Pleasure.ToDynamicDictionary <string>(new { key = Pleasure.Generator.String() }).ToDictionary(r => r.Key, r => (object)r.Value);
            }
            else if (propertyType == typeof(SqlConnection))
            {
                value = new SqlConnection(@"Data Source={0};Database={1};Integrated Security=true;".F(Pleasure.Generator.String(length: 5), Pleasure.Generator.String(length: 5)));
            }

            return(isNullable ? Activator.CreateInstance(typeof(Nullable <>).MakeGenericType(propertyType), value) : value);
        }