Ejemplo n.º 1
0
        /// <summary>
        ///     Iterates through the properties in an object and creates a Key / Value set to allow setting properties via reflection in another object.
        /// </summary>
        /// <param name="toReflect">
        ///     Object to reflect
        /// </param>
        /// <param name="bindingFlags">
        ///     BindingFlags in GetProperties Command Defaults to Public
        /// </param>
        /// <returns>
        ///     ReflectionInfo Key / Value Set
        /// </returns>
        public static ReflectionInfo Funnel(
            this object toReflect, BindingFlags bindingFlags = DefaultBindings)
        {
            var reflectType = toReflect.GetType();
            var props       = reflectType.GetProperties(bindingFlags);

            var rInfo = new ReflectionInfo {
                SourceType = reflectType
            };

            foreach (var prop in props)
            {
                rInfo.Add(prop.Name, prop.GetValue(toReflect, null));
            }

            return(rInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Maps 2D Enumerable of paired values to a Key / Value to allow setting properties via reflection in another object.
        /// </summary>
        /// <typeparam name="TReflected">
        ///     Enumerable of Enumerable of value pairs
        /// </typeparam>
        /// <typeparam name="TKey">
        ///     The Property Name selector type
        /// </typeparam>
        /// <typeparam name="TValue">
        ///     The Value selector type
        /// </typeparam>
        /// <param name="toMap">
        ///     Enumerable of value pairs
        /// </param>
        /// <param name="keySelector">
        ///     Selector for Property Name
        /// </param>
        /// <param name="valueSelector">
        ///     Selector for Value
        /// </param>
        /// <returns>
        ///     The Mapping.Extensions+ReflectionInfo.
        /// </returns>
        public static ReflectionInfo FunnelUsingSelector <TReflected, TKey, TValue>(
            this IEnumerable <TReflected> toMap,
            Func <TReflected, TKey> keySelector,
            Func <TReflected, TValue> valueSelector)
        {
            var reflectInfo = new ReflectionInfo
            {
                SourceType = typeof(TReflected)
            };

            foreach (TReflected mapValue in toMap)
            {
                reflectInfo.Add(keySelector(mapValue), valueSelector(mapValue));
            }

            return(reflectInfo);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Converts some reflection info into a Dynamic object.
        /// </summary>
        /// <param name="reflected">The Reflection Info object to convert to a Dynamic object.</param>
        /// <returns>A dynamic object with properties corresponding to the reflection info.</returns>
        public static dynamic IntoDynamic(this ReflectionInfo reflected)
        {
            var dynamicObject = new DynamicEntity();

            foreach (var keyValuePair in reflected)
            {
                KeyValuePair <object, object> tempReflected = keyValuePair;
                // Needs to happen or it could produce race conditions.
                KeyValuePair <object, List <MappedColumn> > explicitMapping =
                    reflected.MappedColumns.FirstOrDefault(x => (string)x.Key == tempReflected.Key.ToString());

                if (explicitMapping.Value != null)
                {
                    foreach (MappedColumn eMapping in explicitMapping.Value)
                    {
                        if (eMapping.Converter != null)
                        {
                            if (typeof(IMappingConverter).IsAssignableFrom(eMapping.Converter))
                            {
                                var converter = (IMappingConverter)Activator.CreateInstance(eMapping.Converter);
                                dynamicObject[eMapping.Target] =
                                    converter.ConversionMethod(keyValuePair.Value.ToString());
                            }
                            else
                            {
                                throw new Exception("Converter Type not a valid IMappingConverter");
                            }
                        }
                        else
                        {
                            dynamicObject[eMapping.Target] = keyValuePair.Value;
                        }
                    }
                }
                else
                {
                    dynamicObject[keyValuePair.Key.ToString()] = keyValuePair.Value;
                }
            }
            return(dynamicObject);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Maps a 2d array using a given header array. Order determines column position.
        /// </summary>
        /// <param name="csvArray">The csv array</param>
        /// <param name="headers">Array of header strings</param>
        /// <returns></returns>
        public static IEnumerable <ReflectionInfo> Funnel2DArray(this IEnumerable <IEnumerable <string> > csvArray, params string[] headers)
        {
            var csvData = csvArray.ToList();
            var header  = headers;
            var rInfos  = new List <ReflectionInfo>();

            foreach (var item in csvData.Skip(1))
            {
                var itemA = item.ToArray();

                var rInfo = new ReflectionInfo {
                    SourceType = item.GetType()
                };

                for (var i = 0; i < (header.Count() > itemA.Count() ? itemA.Count() : header.Count()); i++)
                {
                    rInfo[header[i]] = itemA[i];
                }

                rInfos.Add(rInfo);
            }
            return(rInfos.AsEnumerable());
        }
Ejemplo n.º 5
0
 /// <summary>
 ///     Creates a dictionary the keys set to the property/column names.
 /// </summary>
 /// <param name="reflected">The Reflection Info object to convert to a Dynamic object.</param>
 /// <returns>Dictionary</returns>
 public static Dictionary <string, object> IntoDictionary(this ReflectionInfo reflected)
 {
     return(reflected.ToDictionary(x => x.Key.ToString(), x => x.Value));
 }
Ejemplo n.º 6
0
        /// <summary>
        ///     Uses ReflectionInfo to generate an instance of the set type populating the data using reflection.
        /// </summary>
        /// <typeparam name="T">
        ///     Object Type to create and populate
        /// </typeparam>
        /// <param name="reflectedArray">
        ///     ReflectionInfo Key/Value Set
        /// </param>
        /// <param name="toUpdate">Object whose properties will be updated</param>
        /// <param name="ignoreCase">Ignore case on name matching if true</param>
        /// <param name="throwException">If populating a field match with a value fails throw an exception if true</param>
        /// <param name="removeSourceUnderscores">Remove underscores from property names in source if true</param>
        /// <param name="bindingFlags">
        ///     BindingFlags in GetProperties Command Defaults to Public
        /// </param>
        /// <returns>
        ///     New instance of T
        /// </returns>
        public static T IntoExisting <T>(
            this ReflectionInfo reflectedArray, T toUpdate, bool ignoreCase = false, bool throwException = true,
            bool removeSourceUnderscores = false,
            BindingFlags bindingFlags    = DefaultBindings)
        {
            var props = typeof(T).GetProperties(bindingFlags).ToDictionary(x => ignoreCase ? x.Name.ToLower() : x.Name);

            var empty = toUpdate;

            foreach (var reflected in reflectedArray)
            {
                PropertyInfo        prop;
                List <MappedColumn> explicitMapping;


                if (reflectedArray.MappedColumns.TryGetValue(reflected.Key.ToString(), out explicitMapping))
                {
                    foreach (MappedColumn eMapping in explicitMapping)
                    {
                        props.TryGetValue(ignoreCase ? eMapping.Target.ToLower() : eMapping.Target, out prop);
                        if (eMapping.Converter != null && prop != null)
                        {
                            if (typeof(IMappingConverter).IsAssignableFrom(eMapping.Converter))
                            {
                                var converter = Activator.CreateInstance(eMapping.Converter) as IMappingConverter;
                                if (converter != null)
                                {
                                    prop.SetValue(empty, converter.ConversionMethod(reflected.Value), null);
                                }
                            }
                            else
                            {
                                throw new Exception("Converter Type not a valid IMappingConverter");
                            }
                        }
                        else
                        {
                            ParseSetValue(reflected, prop, empty, throwException);
                        }
                    }
                }
                else
                {
                    var keyName = reflected.Key.ToString().Replace(" ", "").Trim();

                    if (removeSourceUnderscores)
                    {
                        keyName = keyName.Replace("_", "");
                    }

                    props.TryGetValue(ignoreCase ? keyName.ToLower() : keyName, out prop);

                    if (prop != null)
                    {
                        ParseSetValue(reflected, prop, empty, throwException);
                    }
                }
            }

            return(empty);
        }