Exemple #1
0
        private IList CreateaArrayInstance(Type propertyType, Type targetType = null)
        {
            //Get the underlying type used int he array
            //var elementType = propertyType.GetElementType(); //Works only for arrays
            var elementType = propertyType.IsGenericType ? propertyType.GetGenericArguments()[0] : propertyType.GetElementType(); //Works for IList<T> / IEnumerable<T>

            //Get a number of elements we want to create
            //Note: (between 1 and 10 for now)
            var elementCount = Numbers.Int(1, 10);

            //Create an instance of our target array
            IList arrayInstance = null;

            //If we're working with a generic list or any other sort of collection
            if (propertyType.IsGenericTypeDefinition)
            {
                arrayInstance = (IList)GenericHelper.CreateGeneric(propertyType, elementType);
            }
            else
            {
                arrayInstance = (IList)GenericHelper.CreateGeneric(typeof(List <>), elementType);
            }

            //Determine if there's a selector available for this type
            var           hasSelector = TypeMap.CountSelectors(elementType) > 0;
            ITypeSelector selector    = null;

            //So we have a type available for this selector..
            if (hasSelector)
            {
                selector = TypeMap.GetBaseSelector(elementType);
            }

            //If the element in the array isn't the same type as the parent object (recursive objects, like trees)
            if (elementType != targetType)
            {
                for (var i = 0; i < elementCount; i++)
                {
                    //Create a new element instance
                    var element = SafeObjectCreate(elementType);

                    if (hasSelector)
                    {
                        selector.Generate(ref element);
                    }

                    //If the element type is a class populate it recursively
                    else if (elementType.IsClass)
                    {
                        var subProperties = elementType.GetProperties();

                        //Populate all of the properties on this object
                        ProcessProperties(subProperties, element);
                    }

                    arrayInstance.Add(element);
                }
            }
            return(arrayInstance);
        }
Exemple #2
0
        /// <summary>
        /// Protected method used to implement our selector-matching strategy. Uses a greedy approach.
        /// </summary>
        /// <param name="property">The meta-data about the property for which we will be finding a match</param>
        /// <param name="targetObject">The object which will receive the property injection</param>
        protected virtual void ProcessProperty(PropertyInfo property, object targetObject)
        {
            //Get the type of the property
            var propertyType = property.PropertyType;

            //Determine if we have a selector-on-hand for this data type
            var selectorCount = TypeMap.CountSelectors(propertyType);

            //We have some matching selectors, so we'll evaluate and return the best match
            if (selectorCount > 0)
            {
                //Evaluate all of the possible selectors and find the first available match
                var selector = EvaluateSelectors(property, TypeMap.GetSelectors(propertyType));

                //We found a matching selector
                if (!(selector is MissingSelector))
                {
                    selector.Generate(targetObject, property); //Bind the property
                    return;                                    //Exit
                }
            }

            //Check to see if the type is a class and has a default constructor
            if (propertyType.IsClass && propertyType.GetConstructor(Type.EmptyTypes) != null)
            {
                var subProperties = propertyType.GetProperties();

                //Create an instance of the underlying subclass
                var subClassInstance = Activator.CreateInstance(propertyType);

                //Match all of the properties on the subclass
                ProcessProperties(subProperties, subClassInstance);

                //Bind the sub-class back onto the original target object
                property.SetValue(targetObject, subClassInstance, null);

                return; //Exit
            }

            //Check to see if the type is an array or any other sort of collection
            if (typeof(IList).IsAssignableFrom(propertyType))
            {
                //Get the underlying type used int he array
                var elementType = propertyType.GetElementType();

                //Get a number of elements we want to create
                //Note: (between 0 and 10 for now)
                var elementCount = Numbers.Int(0, 10);

                //Create an instance of our target array
                IList arrayInstance = null;

                //If we're working with a generic list or any other sort of collection
                if (propertyType.IsGenericType)
                {
                    arrayInstance = (IList)GenericHelper.CreateGeneric(propertyType, elementType);
                }
                else
                {
                    arrayInstance = (IList)GenericHelper.CreateGeneric(typeof(List <>), elementType);
                }

                //Determine if there's a selector available for this type
                var           hasSelector = TypeMap.CountSelectors(elementType) > 0;
                ITypeSelector selector    = null;

                if (hasSelector)
                {
                    //selector = EvaluateSelectors(ele)
                }

                for (var i = 0; i < elementCount; i++)
                {
                    //Create a new element instance
                    var element = Activator.CreateInstance(elementType);

                    if (hasSelector)
                    {
                    }

                    //If the element type is a sub-class, then populate it recursively
                    if (elementType.IsClass)
                    {
                        var subProperties = propertyType.GetProperties();

                        //Populate all of the properties on this object
                        ProcessProperties(subProperties, element);
                    }

                    arrayInstance.Add(element);
                }
            }
        }