/// <summary> /// Tries to create an array value for the given type. /// </summary> /// <typeparam name="T">The type of the instance to create.</typeparam> /// <param name="options">Some create instance options.</param> /// <param name="memberInformation">The member to check.</param> /// <returns>Returns the created value, or null if the given type is not an array type.</returns> private static Object TryCreateArrayValue <T>(ICreateInstanceOptionsComplete <T> options, IMemberInformation memberInformation) where T : class { // Check if member is an array type if (!memberInformation.MemberType.IsArray) { return(null); } // Create the array var elementType = memberInformation.MemberType.GetElementType(); var array = Array.CreateInstance(elementType, GetCollectionItemCount(options)); // Add items var anonymousItemName = GetAnonymousItemName(options); for (var i = 0; i < array.Length; i++) { var currentMember = new MemberInformation { MemberType = elementType, MemberPath = $"{memberInformation.MemberPath}.{anonymousItemName}", MemberName = anonymousItemName }; // Get the value of the current array item. var arrayItemValue = GetValue(options, currentMember); currentMember.MemberObject = arrayItemValue; SetAllMembers(options, currentMember); array.SetValue(arrayItemValue, i); } return(array); }
/// <summary> /// Populates the given instance if it is a collection, based on the collection configuration. /// </summary> /// <typeparam name="T">The type of the instance to create.</typeparam> /// <param name="options">Some create instance options.</param> /// <param name="memberInformation">The member to check.</param> /// <param name="collectionInstance">The instance to populate.</param> /// <returns>Returns the populated or unmodified instance.</returns> private static Object TryPopulateCollection <T>(ICreateInstanceOptionsComplete <T> options, IMemberInformation memberInformation, Object collectionInstance) where T : class { // Check if collection should get populated or not if (!PopulateCollection(options) || collectionInstance == null) { return(collectionInstance); } // Check if type is collection type if (!memberInformation.MemberType.ImplementsICollectionT()) { return(collectionInstance); } // Get generic parameter type var genericArgumentTypes = memberInformation .MemberType .GetGenericTypeArguments() .ToArray(); // Get the add method var addMethod = memberInformation.MemberType .GetRuntimeMethod("Add", genericArgumentTypes); // Add items var anonymousItemName = GetAnonymousItemName(options); var collectionCount = GetCollectionItemCount(options); for (var i = 0; i < collectionCount; i++) { var addParameters = new List <Object>(); genericArgumentTypes .ForEach(x => { var currentMember = new MemberInformation { MemberType = x, MemberPath = $"{memberInformation.MemberPath}.{anonymousItemName}", MemberName = anonymousItemName }; // Get the value for the current collection item. var collectionItemValue = GetValue(options, currentMember); currentMember.MemberObject = collectionItemValue; SetAllMembers(options, currentMember); addParameters.Add(collectionItemValue); }); addMethod.Invoke(collectionInstance, addParameters.ToArray()); } return(collectionInstance); }
public static T CreateInstance <T>([NotNull] this ICreateInstanceOptionsComplete <T> options) where T : class { options.ThrowIfNull(nameof(options)); //Create instance var rootMemberInformation = new MemberInformation { MemberType = typeof(T), MemberPath = String.Empty, MemberName = String.Empty }; var instance = CreateRootMember(options, rootMemberInformation); rootMemberInformation.MemberObject = instance; //Set each member of the created instance. SetAllMembers(options, rootMemberInformation); return(instance); }