コード例 #1
0
        private static void PopulateFields(ICollection <IField> schema, Type template, string parentName, int currentDepth, int maxDepth)
        {
            if (currentDepth >= maxDepth)
            {
                return;
            }
            string concat(params string[] args) => string.Join(".", args).TrimStart('.');

            foreach (MemberInfo member in GetPublicFieldsAndPropertiesFrom(template))
            {
                string temp;
                IField field;
                Type   valueType = GetValueType(member);
#if DEBUG
                System.Diagnostics.Debug.WriteLine($"{concat(parentName, member.Name)}: <{valueType.Name}>");
#endif
                switch (GetKindOfType(valueType, out Type collectionElementType))
                {
                case KindOfType.Primitive:
                    field      = FieldFactory.CreateInstance(valueType, template);
                    field.Name = concat(parentName, member.Name);
                    schema.Add(field);
                    break;

                case KindOfType.CollectionOfPrimitives:
                    field      = FieldFactory.CreateInstance(valueType);
                    field.Name = concat(parentName, member.Name);
                    schema.Add(field);

                    field      = FieldFactory.CreateInstance(collectionElementType);
                    field.Name = concat(parentName, member.Name, Item);
                    schema.Add(field);
                    break;

                case KindOfType.Object:
                    temp = concat(parentName, member.Name);
                    PopulateFields(schema, valueType, temp, (currentDepth + 1), maxDepth);
                    break;

                case KindOfType.CollectionOfObjects:
                    field      = FieldFactory.CreateInstance(valueType);
                    field.Name = concat(parentName, member.Name);
                    schema.Add(field);

                    temp = concat(parentName, member.Name, Item);
                    PopulateFields(schema, collectionElementType, temp, (currentDepth + 1), maxDepth);
                    break;
                }
            }
        }
コード例 #2
0
ファイル: Schema.cs プロジェクト: Ackara/Mockaroo.NET
 /// <summary>
 /// Replace the <see cref="IField"/> object within this instance with a <see cref="IField"/>
 /// that is associated with the specified <see cref="DataType"/>.
 /// </summary>
 /// <param name="fieldName">Name of the field.</param>
 /// <param name="dataType">Type of the data.</param>
 public IField Replace(string fieldName, DataType dataType)
 {
     return(Replace(fieldName, FieldFactory.CreateInstance(dataType)));
 }