/// <summary>Write an objects values to the file stream</summary>
        /// <param name="obj"></param>
        private bool WriteWithoutHeaders <T>(T obj, string context)
        {
            if (obj == null)
            {
                return(false);
            }

            // get all the properties in the class
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (props.Length < 1)
            {
                return(false);
            }

            // get all the fields marked with a destination, if there are NO destinations, then assume we want to write EVERY field
            List <string> destinations = RayPropertyAttribute.GetDestinations(typeof(T));

            StringBuilder sb = new StringBuilder();

            // get the property info
            foreach (PropertyInfo property in props)
            {
                if (!property.CanRead)
                {
                    continue;
                }

                object value = null;

                if (String.IsNullOrWhiteSpace(context))
                {
                    value = property.GetValue(obj);
                }
                else
                {
                    // get the properties FieldMap attribute
                    List <RayPropertyAttribute> dests = property.GetCustomAttributes <RayPropertyAttribute>(false).ToList();

                    // if there is not field map source, then this property is not read from the CSV
                    if (dests == null || dests.Count < 1)
                    {
                        continue;
                    }

                    RayPropertyAttribute map = dests.Where(s => s.Context.Equals(context, StringComparison.Ordinal)).FirstOrDefault();

                    if (map == null || String.IsNullOrWhiteSpace(map.Destination))
                    {
                        continue;
                    }

                    value = property.GetValue(obj);
                }

                // write to the file
                if (this.FieldQuoteChar == Char.MinValue)
                {
                    sb.AppendFormat("{0}{1}", (value == null) ? String.Empty : value.ToString(), this.ColDelimitor);
                }
                else
                {
                    sb.AppendFormat("{2}{0}{2}{1}", (value == null) ? String.Empty : value.ToString(), this.ColDelimitor, this.FieldQuoteChar);
                }
            }

            // trim
            sb.Length = sb.Length - this.ColDelimitor.Length;

            if (!String.IsNullOrWhiteSpace(this.RowDelimitor))
            {
                sb.Append(this.RowDelimitor);
            }

            this._osw.WriteLine(sb.ToString());

            return(true);
        }
        /// <summary>Write an objects values to the file stream using the headers to specify which ones and what order</summary>
        /// <param name="obj"></param>
        private bool WriteWithHeaders <T>(T obj, string context)
        {
            if (obj == null)
            {
                return(false);
            }

            // get all the properties in the class
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (props.Length < 1)
            {
                return(false);
            }

            StringBuilder sb = new StringBuilder();

            // loop through the headers and find the matching property destination
            foreach (string header in this._headers)
            {
                object value = null;

                // find a matching property with a FieldDestination value equal to this header
                foreach (PropertyInfo property in props)
                {
                    // can you even read this property
                    if (!property.CanRead)
                    {
                        continue;
                    }

                    // get the properties FieldDestination attribute
                    List <RayPropertyAttribute> dests = property.GetCustomAttributes <RayPropertyAttribute>(false).ToList();

                    // if there is not field dest, then this property is not written
                    if (dests == null || dests.Count < 1)
                    {
                        continue;
                    }

                    // if no specified context then use a Destination with NO Context
                    if (String.IsNullOrWhiteSpace(context))
                    {
                        RayPropertyAttribute map = dests.Where(s => String.IsNullOrWhiteSpace(s.Context) &&
                                                               !String.IsNullOrWhiteSpace(s.Destination) &&
                                                               s.Destination.Equals(header, StringComparison.InvariantCulture)).FirstOrDefault();

                        if (map == null)
                        {
                            continue;
                        }

                        value = property.GetValue(obj);
                    }
                    else                     // is a context
                    {
                        RayPropertyAttribute map = dests.Where(s => !String.IsNullOrWhiteSpace(s.Context) &&
                                                               !String.IsNullOrWhiteSpace(s.Destination) &&
                                                               s.Context.Equals(context, StringComparison.InvariantCulture) &&
                                                               s.Destination.Equals(header, StringComparison.InvariantCulture)).FirstOrDefault();

                        if (map == null)
                        {
                            continue;
                        }

                        value = property.GetValue(obj);
                    }
                }

                if (this.FieldQuoteChar == Char.MinValue)
                {
                    sb.AppendFormat("{0}{1}", (value == null) ? String.Empty : value.ToString(), this.ColDelimitor);
                }
                else
                {
                    sb.AppendFormat("{2}{0}{2}{1}", (value == null) ? String.Empty : value.ToString(), this.ColDelimitor, this.FieldQuoteChar);
                }
            }

            sb.Length = sb.Length - this.ColDelimitor.Length;

            if (!String.IsNullOrWhiteSpace(this.RowDelimitor))
            {
                sb.Append(this.RowDelimitor);
            }

            this._osw.WriteLine(sb.ToString());

            return(true);
        }
示例#3
0
        /// <summary>Takes a string, string dictionary of field/value pairs and converts them to the T source object</summary>
        /// <param name="values"></param>
        /// <param name="context">The context to use, if null then use every readable property</param>
        /// <returns>Returns new object of type T</returns>
        /// <remarks>Uses RayProperty to find the annotations
        /// Only used in DataFileReader for now
        /// </remarks>
        public static T SourceToObject <T>(Dictionary <string, string> values, string context) where T : class, new()
        {
            // get all the properties in the class
            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (props.Length < 1)
            {
                return(null);
            }

            // check for adornment
            bool marked = false;

            foreach (PropertyInfo prop in props)
            {
                if (prop.GetCustomAttributes <RayPropertyAttribute>(false).FirstOrDefault() != null)
                {
                    marked = true;
                    break;
                }
            }

            // cretae a new instance of T
            T obj = new T();

            // iterate each property in the type and see if it has a matching field in the source file
            foreach (PropertyInfo prop in props)
            {
                // can you publicly set the property
                if (!prop.CanWrite)
                {
                    continue;
                }

                string value = null;

                // case 1 - if there are NO RayProperties defined at all then use a straight property map
                if (!marked && String.IsNullOrWhiteSpace(context))
                {
                    // if the file contains the field
                    if (values.Keys.Contains(prop.Name))
                    {
                        value = (String.IsNullOrWhiteSpace(values[prop.Name])) ? String.Empty : values[prop.Name].Trim();
                    }
                }
                // case 2
                else if (marked && String.IsNullOrWhiteSpace(context))
                {
                    // get the properties FieldMap attribute
                    List <RayPropertyAttribute> sources = prop.GetCustomAttributes <RayPropertyAttribute>(false).ToList();

                    // if there is no field map source, then this property is not read from the CSV
                    if (sources == null || sources.Count < 1)
                    {
                        continue;
                    }

                    // get only null context sources
                    RayPropertyAttribute map = sources.Where(s => String.IsNullOrWhiteSpace(s.Context)).FirstOrDefault();

                    // no match or no source defined
                    if (map == null || String.IsNullOrWhiteSpace(map.Source))
                    {
                        continue;
                    }

                    // set the value field if the source file have this field
                    if (values.Keys.Contains(map.Source))
                    {
                        value = (String.IsNullOrWhiteSpace(values[map.Source])) ? String.Empty : values[map.Source].Trim();
                    }
                }
                // case 3
                else if (marked && !String.IsNullOrWhiteSpace(context))
                {
                    throw new System.Exception("Case not handled");

                    // get only null context sources
                    //RayPropertyAttribute map = sources.Where( s => s.Context.Equals( context, StringComparison.Ordinal ) ).FirstOrDefault();
                }

                // use the source field to get the value using string DataType Converters
                if (prop.PropertyType == typeof(string))
                {
                    prop.SetValue(obj, value);
                }
                else if (prop.PropertyType == typeof(DateTime))
                {
                    prop.SetValue(obj, value.GetDateTimeValue());
                }
                else if (prop.PropertyType == typeof(Nullable <DateTime>))
                {
                    prop.SetValue(obj, value.GetNullDateTimeValue());
                }
                else if (prop.PropertyType == typeof(int))
                {
                    prop.SetValue(obj, value.GetIntValue());
                }
                else if (prop.PropertyType == typeof(double))
                {
                    prop.SetValue(obj, value.GetDoubleValue());
                }
                else if (prop.PropertyType == typeof(Nullable <int>))
                {
                    prop.SetValue(obj, value.GetNullableIntValue());
                }
                else if (prop.PropertyType == typeof(long))
                {
                    prop.SetValue(obj, value.GetLongValue());
                }
                else if (prop.PropertyType == typeof(bool))
                {
                    prop.SetValue(obj, value.GetBooleanValue());
                }
                else if (prop.PropertyType.IsEnum)
                {
                    Type t = prop.PropertyType;

                    //prop.SetValue( obj, cursor.GetStringValue( key ).GetEnumValue<T.GetType()>() );

                    Object temp = Enum.Parse(t, value, true);
                    prop.SetValue(obj, temp);
                }
                else if (prop.PropertyType == typeof(ObjectId))
                {
                    if (String.IsNullOrWhiteSpace(value))
                    {
                        prop.SetValue(obj, ObjectId.Empty);
                    }
                    else
                    {
                        prop.SetValue(obj, new ObjectId(value));
                    }
                }
                else
                {
                    throw new System.Exception("Type not yet supported.");
                }
            }

            return(obj);
        }