Esempio n. 1
0
 public int GetNextInt()
 {
     return(Flags.HasFlag(AutoFillFlags.RandomInt) ? RngUtils.GetRandomInt() : IntValue++);
 }
Esempio n. 2
0
        private static object GetValueForType(Type type, AutoFillContext context, string propertyName)
        {
            if (context.IgnoredMembers.Contains(string.Join(".", context.Levels)))
            {
                return(null);
            }
            if (type == typeof(int))
            {
                return(context.GetNextInt());
            }
            if (type == typeof(char))
            {
                return((char)context.GetNextInt());
            }
            if (type == typeof(byte))
            {
                return((byte)context.GetNextInt());
            }
            if (type == typeof(short))
            {
                return((short)context.GetNextInt());
            }
            if (type == typeof(long))
            {
                return((long)context.GetNextInt());
            }
            if (type == typeof(float))
            {
                return((float)context.GetNextInt());
            }
            if (type == typeof(double))
            {
                return((double)context.GetNextInt());
            }
            if (type == typeof(decimal))
            {
                return((decimal)context.GetNextInt());
            }
            if (type == typeof(bool))
            {
                return(true);
            }
            if (type == typeof(string))
            {
                var value = GenerateStringValue(propertyName);
                if (context.Flags.HasFlag(AutoFillFlags.RandomizeString))
                {
                    value.Append("-").Append(RngUtils.GetRandomHexString(8));
                }
                return(value.ToString());
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                return(GetValueForType(type.GetGenericArguments()[0], context, propertyName));
            }

            foreach (var interfaceType in type.GetInterfaces())
            {
                if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    var elementListType = interfaceType.GenericTypeArguments[0];
                    var listType        = typeof(List <>).MakeGenericType(elementListType);
                    var list            = (IList)Activator.CreateInstance(listType);
                    for (int i = 0; i < 3; i++)
                    {
                        list.Add(GetValueForType(elementListType, context, propertyName + i));
                    }

                    return(list);
                }
            }

            return(CreateElement(type, context));
        }