Beispiel #1
0
        } // ToDictionary()

        public virtual void Populate(Dictionary <String, String> row)
        {
            DataDefinition d = GetDefinition();

            foreach (KeyValuePair <String, String> pair in row)
            {
                if (d.Maps.ContainsKey(pair.Key))
                {
                    IReference property = d.Maps[pair.Key];
                    var        thisType = GetType();
                    Type       t        = property.PropertyType;

                    if (t.IsGenericType && t.GetGenericArguments().Length > 0)
                    {
                        t = t.GetGenericArguments()[0];
                    }

                    if (t == null)
                    {
                        var propertyType = thisType.GetProperty(pair.Key);
                        var fieldType    = thisType.GetField(pair.Key);

                        if (propertyType != null)
                        {
                            t = propertyType.PropertyType;
                        }
                        else if (fieldType != null)
                        {
                            t = fieldType.FieldType;
                        }
                        else
                        {
                            t = typeof(Object);
                        }
                    }

                    if (!String.IsNullOrEmpty(pair.Value))
                    {
                        if (t == typeof(Int16))
                        {
                            property.Set(Convert.ToInt16(pair.Value));
                        }
                        else if (t == typeof(UInt16))
                        {
                            property.Set(Convert.ToUInt16(pair.Value));
                        }
                        else if (t == typeof(Int32))
                        {
                            property.Set(Convert.ToInt32(pair.Value));
                        }
                        else if (t == typeof(UInt32))
                        {
                            property.Set(Convert.ToUInt32(pair.Value));
                        }
                        else if (t == typeof(Int64))
                        {
                            property.Set(Convert.ToInt64(pair.Value));
                        }
                        else if (t == typeof(Single))
                        {
                            property.Set(Convert.ToSingle(pair.Value));
                        }
                        else if (t == typeof(Double))
                        {
                            property.Set(Convert.ToDouble(pair.Value));
                        }
                        else if (t == typeof(Decimal))
                        {
                            property.Set(Convert.ToDecimal(pair.Value));
                        }
                        else if (t == typeof(DateTime))
                        {
                            property.Set(Convert.ToDateTime(pair.Value));
                        }
                        else if (t == typeof(Guid))
                        {
                            Guid g = Guid.Empty;
                            try
                            {
                                g = new Guid(pair.Value);
                            }
                            catch
                            {
                                // do nothing.
                            }
                            property.Set(g);
                        }
                        else if (t == typeof(Nullable <Boolean>) || t == typeof(Boolean))
                        {
                            if (pair.Value != null)
                            {
                                Boolean v = false;
                                switch (pair.Value.ToLower())
                                {
                                case "1":
                                case "true":
                                    v = true;
                                    break;

                                default:
                                    break;
                                }
                                property.Set(v);
                            }
                        }
                        else if (t == typeof(String))
                        {
                            property.Set(Convert.ToString(pair.Value));
                        }
                        else if (t == typeof(Byte[]))
                        {
                            property.Set(System.Text.Encoding.UTF8.GetBytes(pair.Value));
                        }
                        else
                        {
                            property.Set(pair.Value);
                        }
                    }
                }
            }
        } // Populate(Dictionary<String,String>)