Ejemplo n.º 1
0
        protected virtual object Deserialize(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value.GetType().IsCollectionType())
            {
                if (targetType != typeof(object) && !targetType.IsSimple() && !typeof(ICollection).IsAssignableFrom(targetType))
                {
                    IList coll     = value as IList;
                    var   itemType = value.GetType().GetItemType();
                    if (itemType == typeof(object) || itemType.IsSimple())
                    {
                        value = ChoActivator.CreateInstance(targetType);
                        foreach (var p in ChoTypeDescriptor.GetProperties <ChoArrayIndexAttribute>(targetType).Select(pd => new { pd, a = ChoTypeDescriptor.GetPropetyAttribute <ChoArrayIndexAttribute>(pd) })
                                 .GroupBy(g => g.a.Position).Select(g => g.First()).Where(g => g.a.Position >= 0).OrderBy(g => g.a.Position))
                        {
                            if (p.a.Position < coll.Count)
                            {
                                ChoType.ConvertNSetPropertyValue(value, p.pd.Name, coll[p.a.Position], culture);
                            }
                        }
                    }
                }
            }

            return(value);
        }
Ejemplo n.º 2
0
        public static void Initialize(this object target)
        {
            if (target == null)
            {
                return;
            }

            object defaultValue = null;

            foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties <DefaultValueAttribute>(target.GetType()))
            {
                try
                {
                    defaultValue = ChoTypeDescriptor.GetPropetyAttribute <DefaultValueAttribute>(pd).Value;
                    if (defaultValue != null)
                    {
                        ChoType.ConvertNSetMemberValue(target, pd.Name, defaultValue);
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.TraceSwitch.TraceError, "Error while assigning default value '{0}' to '{1}' member. {2}".FormatString(defaultValue, ChoType.GetMemberName(pd), ex.Message));
                }
            }

            ChoETLFramework.InitializeObject(target);

            if (target is IChoInitializable)
            {
                ((IChoInitializable)target).Initialize();
            }
        }
Ejemplo n.º 3
0
        private void CheckColumnsStrict(object rec)
        {
            if (Configuration.IsDynamicObject)
            {
                var eoDict = rec.ToDynamicObject() as IDictionary <string, Object>;

                if (eoDict.Count != Configuration.FixedLengthRecordFieldConfigurations.Count)
                {
                    throw new ChoParserException("Incorrect number of fields found in record object. Expected [{0}] fields. Found [{1}] fields.".FormatString(Configuration.FixedLengthRecordFieldConfigurations.Count, eoDict.Count));
                }

                string[] missingColumns = Configuration.FixedLengthRecordFieldConfigurations.Select(v => v.Name).Except(eoDict.Keys, Configuration.FileHeaderConfiguration.StringComparer).ToArray();
                if (missingColumns.Length > 0)
                {
                    throw new ChoParserException("[{0}] fields are not found in record object.".FormatString(String.Join(",", missingColumns)));
                }
            }
            else
            {
                PropertyDescriptor[] pds = ChoTypeDescriptor.GetProperties <ChoFixedLengthRecordFieldAttribute>(rec.GetType()).ToArray();

                if (pds.Length != Configuration.FixedLengthRecordFieldConfigurations.Count)
                {
                    throw new ChoParserException("Incorrect number of fields found in record object. Expected [{0}] fields. Found [{1}] fields.".FormatString(Configuration.FixedLengthRecordFieldConfigurations.Count, pds.Length));
                }

                string[] missingColumns = Configuration.FixedLengthRecordFieldConfigurations.Select(v => v.Name).Except(pds.Select(pd => pd.Name), Configuration.FileHeaderConfiguration.StringComparer).ToArray();
                if (missingColumns.Length > 0)
                {
                    throw new ChoParserException("[{0}] fields are not found in record object.".FormatString(String.Join(",", missingColumns)));
                }
            }
        }
Ejemplo n.º 4
0
        private void DiscoverRecordFields(Type recordType)
        {
            if (!IsDynamicObject) //recordType != typeof(ExpandoObject))
            {
                CSVRecordFieldConfigurations.Clear();

                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoCSVRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoCSVRecordFieldAttribute>().Any()))
                    {
                        //if (!pd.PropertyType.IsSimple())
                        //    throw new ChoRecordConfigurationException("Property '{0}' is not a simple type.".FormatString(pd.Name));

                        var obj = new ChoCSVRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoCSVRecordFieldAttribute>().First());
                        obj.FieldType = pd.PropertyType;
                        CSVRecordFieldConfigurations.Add(obj);
                    }
                }
                else
                {
                    int position = 0;
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        //if (!pd.PropertyType.IsSimple())
                        //    throw new ChoRecordConfigurationException("Property '{0}' is not a simple type.".FormatString(pd.Name));

                        var obj = new ChoCSVRecordFieldConfiguration(pd.Name, ++position);
                        obj.FieldType = pd.PropertyType;
                        CSVRecordFieldConfigurations.Add(obj);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private static void PopulateParams(SQLiteCommand cmd, object target, Dictionary <string, PropertyInfo> PIDict)
        {
            if (target.GetType().IsDynamicType())
            {
                var eo    = target as IDictionary <string, Object>;
                int count = 0;
                foreach (var kvp in eo)
                {
                    cmd.Parameters[count].Value = kvp.Value == null ? DBNull.Value : kvp.Value;
                    count++;
                }
                //foreach (KeyValuePair<string, object> kvp in eo)
                //{
                //    cmd.Parameters[$"@{kvp.Key}"].Value = kvp.Value == null ? DBNull.Value : kvp.Value;
                //}
            }
            else
            {
                object pv    = null;
                int    count = 0;
                foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType()))
                {
                    pv = PIDict[pd.Name].GetValue(target);
                    cmd.Parameters[count].Value = pv == null ? DBNull.Value : pv;
                    count++;
                }

                //foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType()))
                //{
                //    pv = PIDict[pd.Name].GetValue(target);
                //    cmd.Parameters[$"@{pd.Name}"].Value = pv == null ? DBNull.Value : pv;
                //}
            }
        }
Ejemplo n.º 6
0
        private string[] GetFields(object record)
        {
            string[] fieldNames = null;
            Type     recordType = ElementType == null?record.GetType() : ElementType;

            Configuration.RecordType      = recordType.ResolveType();
            Configuration.IsDynamicObject = recordType.IsDynamicType();
            if (!Configuration.IsDynamicObject)
            {
                if (Configuration.FixedLengthRecordFieldConfigurations.Count == 0)
                {
                    Configuration.MapRecordFields(Configuration.RecordType);
                }
            }

            if (Configuration.IsDynamicObject)
            {
                var dictKeys = new List <string>();
                var dict     = record.ToDynamicObject() as IDictionary <string, Object>;
                fieldNames = dict.Flatten().ToDictionary().Keys.ToArray();
            }
            else
            {
                fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray();
                if (fieldNames.Length == 0)
                {
                    fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray();
                }
            }
            return(fieldNames);
        }
Ejemplo n.º 7
0
 private void DiscoverRecordFields(Type recordType, bool clear = true)
 {
     if (clear)
         JSONRecordFieldConfigurations.Clear();
     DiscoverRecordFields(recordType, null,
         ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType<ChoJSONRecordFieldAttribute>().Any()).Any());
 }
Ejemplo n.º 8
0
        public static void ScanAndDefineKeyToEntity(this Type type)
        {
            bool hasIdProperty = ChoTypeDescriptor.GetProperties(type).Where(p => String.Compare(p.Name, "id", true) == 0).Any();

            if (hasIdProperty)
            {
                return;
            }

            bool hasKeyDefined = ChoTypeDescriptor.GetProperties(type).Where(pd => pd.Attributes.OfType <KeyAttribute>().Any()).Any();

            if (hasKeyDefined)
            {
                return;
            }

            PropertyDescriptor firstPd = ChoTypeDescriptor.GetProperties(type).FirstOrDefault();

            if (firstPd == null)
            {
                return;
            }
            PropertyDescriptor      pd2 = TypeDescriptor.CreateProperty(type, firstPd, new KeyAttribute());
            ChoCustomTypeDescriptor ctd = new ChoCustomTypeDescriptor(TypeDescriptor.GetProvider(type).GetTypeDescriptor(type));

            ctd.OverrideProperty(pd2);
            TypeDescriptor.AddProvider(new ChoTypeDescriptionProvider(ctd), type);
        }
Ejemplo n.º 9
0
        public override bool CanConvert(Type objectType)
        {
            if (objectType == null)
            {
                return(false);
            }

            bool isKVPObject = false;

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                isKVPObject = true;
            }
            else
            {
                var isKVPAttrDefined = ChoTypeDescriptor.GetTypeAttribute <ChoKeyValueTypeAttribute>(objectType) != null;
                if (isKVPAttrDefined)
                {
                    var kP = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                    var vP = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();
                    if (kP != null && vP != null)
                    {
                        isKVPObject = true;
                    }
                }
            }

            return(isKVPObject);
        }
Ejemplo n.º 10
0
        public static object CreateInstanceAndDefaultToMembers(this Type type, IDictionary <string, ChoRecordFieldConfiguration> fcs)
        {
            var    obj          = ChoActivator.CreateInstance(type);
            object defaultValue = null;

            foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(type))
            {
                try
                {
                    if (!fcs.ContainsKey(pd.Name) || !fcs[pd.Name].IsDefaultValueSpecified)
                    {
                        continue;
                    }

                    defaultValue = fcs[pd.Name].DefaultValue;
                    if (defaultValue != null)
                    {
                        ChoType.ConvertNSetPropertyValue(obj, pd.Name, defaultValue);
                    }
                }
                catch (Exception ex)
                {
                    ChoETLFramework.WriteLog(ChoETLFramework.TraceSwitch.TraceError, "Error while assigning default value '{0}' to '{1}' member. {2}".FormatString(defaultValue, ChoType.GetMemberName(pd), ex.Message));
                }
            }
            return(obj);
        }
Ejemplo n.º 11
0
        private void DiscoverRecordFields(Type recordType, bool clear = true,
                                          List <ChoJSONRecordFieldConfiguration> recordFieldConfigurations = null)
        {
            if (recordType == null)
            {
                return;
            }

            if (RecordMapType == null)
            {
                RecordMapType = recordType;
            }

            if (recordFieldConfigurations == null)
            {
                recordFieldConfigurations = JSONRecordFieldConfigurations;
            }

            if (clear && recordFieldConfigurations != null)
            {
                recordFieldConfigurations.Clear();
            }

            DiscoverRecordFields(recordType, null,
                                 ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any(), recordFieldConfigurations);
        }
Ejemplo n.º 12
0
        private void DiscoverRecordFields(Type recordType)
        {
            if (!IsDynamicObject) // recordType != typeof(ExpandoObject))
            {
                JSONRecordFieldConfigurations.Clear();

                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()))
                    {
                        var obj = new ChoJSONRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().First());
                        obj.FieldType = pd.PropertyType;
                        JSONRecordFieldConfigurations.Add(obj);
                    }
                }
                else
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        var obj = new ChoJSONRecordFieldConfiguration(pd.Name, (string)null);
                        obj.FieldType = pd.PropertyType;
                        JSONRecordFieldConfigurations.Add(obj);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        private Type DiscoverRecordFields(Type recordType, bool clear = true,
                                          List <ChoYamlRecordFieldConfiguration> recordFieldConfigurations = null, bool isTop = false)
        {
            if (recordType == null)
            {
                return(recordType);
            }

            if (RecordMapType == null)
            {
                RecordMapType = recordType;
            }

            if (recordFieldConfigurations == null)
            {
                recordFieldConfigurations = YamlRecordFieldConfigurations;
            }

            if (clear && recordFieldConfigurations != null)
            {
                recordFieldConfigurations.Clear();
            }

            return(DiscoverRecordFields(recordType, null,
                                        ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoYamlRecordFieldAttribute>().Any()).Any(), recordFieldConfigurations, isTop));
        }
Ejemplo n.º 14
0
        public ChoCSVWriter <T> Index <TField>(Expression <Func <T, TField> > field, int minumum, int maximum)
        {
            Type recordType = field.GetPropertyType().GetUnderlyingType();
            var  fqn        = field.GetFullyQualifiedMemberName();

            if (typeof(IList).IsAssignableFrom(recordType) && !typeof(ArrayList).IsAssignableFrom(recordType) &&
                minumum >= 0 && maximum >= 0 && minumum <= maximum)
            {
                recordType = recordType.GetItemType().GetUnderlyingType();
                if (recordType.IsSimple())
                {
                }
                else
                {
                    //Remove any unused config
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        var fcs = Configuration.CSVRecordFieldConfigurations.Where(o => o.DeclaringMember == "{0}.{1}".FormatString(field.GetFullyQualifiedMemberName(), pd.Name) &&
                                                                                   o.ArrayIndex != null && (o.ArrayIndex <minumum || o.ArrayIndex> maximum)).ToArray();

                        foreach (var fc in fcs)
                        {
                            Configuration.CSVRecordFieldConfigurations.Remove(fc);
                        }
                    }

                    for (int index = minumum; index <= maximum; index++)
                    {
                        foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                        {
                            var fc = Configuration.CSVRecordFieldConfigurations.Where(o => o.DeclaringMember == "{0}.{1}".FormatString(field.GetFullyQualifiedMemberName(), pd.Name) &&
                                                                                      o.ArrayIndex != null && o.ArrayIndex == index).FirstOrDefault();

                            if (fc != null)
                            {
                                continue;
                            }

                            Type pt = pd.PropertyType.GetUnderlyingType();
                            if (pt != typeof(object) && !pt.IsSimple())
                            {
                            }
                            else
                            {
                                int fieldPosition = 0;
                                fieldPosition = Configuration.CSVRecordFieldConfigurations.Count > 0 ? Configuration.CSVRecordFieldConfigurations.Max(f => f.FieldPosition) : 0;
                                fieldPosition++;
                                ChoCSVRecordFieldConfiguration obj = Configuration.NewFieldConfiguration(ref fieldPosition, field.GetFullyQualifiedMemberName(), pd, index, field.GetPropertyDescriptor().GetDisplayName());

                                //if (!CSVRecordFieldConfigurations.Any(c => c.Name == (declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name))))
                                Configuration.CSVRecordFieldConfigurations.Add(obj);
                            }
                        }
                    }
                }
            }
            return(this);
        }
Ejemplo n.º 15
0
        private void DiscoverRecordFields(Type recordType, bool clear = true)
        {
            if (clear)
            {
                //SupportsMultiRecordTypes = false;
                FixedLengthRecordFieldConfigurations.Clear();
            }
            //else
            //    SupportsMultiRecordTypes = true;

            DiscoverRecordFields(recordType, null,
                                 ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().Any()).Any());
        }
        private void DiscoverRecordFields(Type recordType)
        {
            if (!IsDynamicObject)
            {
                FixedLengthRecordFieldConfigurations.Clear();

                foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().Any()))
                {
                    //if (!pd.PropertyType.IsSimple())
                    //    throw new ChoRecordConfigurationException("Property '{0}' is not a simple type.".FormatString(pd.Name));
                    var obj = new ChoFixedLengthRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoFixedLengthRecordFieldAttribute>().First());
                    obj.FieldType = pd.PropertyType;
                    FixedLengthRecordFieldConfigurations.Add(obj);
                }
            }
        }
Ejemplo n.º 17
0
        protected virtual object Serialize(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            IList result = ChoActivator.CreateInstance(typeof(IList <>).MakeGenericType(targetType)) as IList;

            if (value != null && !value.GetType().IsCollectionType())
            {
                if (targetType == typeof(object) || targetType.IsSimple())
                {
                    foreach (var p in ChoTypeDescriptor.GetProperties(value.GetType()).Where(pd => ChoTypeDescriptor.GetPropetyAttribute <ChoIgnoreMemberAttribute>(pd) == null))
                    {
                        result.Add(ChoConvert.ConvertTo(ChoType.GetPropertyValue(value, p.Name), targetType, culture));
                    }
                }
            }

            return(result.OfType <object>().ToArray());
        }
Ejemplo n.º 18
0
        public static Dictionary <string, object> ToDictionary(this object target)
        {
            ChoGuard.ArgumentNotNull(target, "Target");

            if (target is IDictionary <string, object> )
            {
                return((Dictionary <string, object>)target);
            }

            Dictionary <string, object> dict = new Dictionary <string, object>();

            foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType()))
            {
                dict.Add(pd.Name, ChoType.GetPropertyValue(target, pd.Name));
            }

            return(dict);
        }
        private void DiscoverRecordFields(Type recordType, ref int pos, bool clear = true)
        {
            if (recordType == null)
            {
                return;
            }

            if (clear)
            {
                //SupportsMultiRecordTypes = false;
                CSVRecordFieldConfigurations.Clear();
            }
            //else
            //SupportsMultiRecordTypes = true;

            DiscoverRecordFields(recordType, ref pos, null,
                                 ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoCSVRecordFieldAttribute>().Any()).Any());
        }
Ejemplo n.º 20
0
        public static string CreateTableScript(this object target, Dictionary <Type, string> columnDataMapper = null, string tableName = null, string keyColumns = null)
        {
            ChoGuard.ArgumentNotNull(target, "Target");
            Type objectType = target is Type ? target as Type : target.GetType();

            columnDataMapper = columnDataMapper ?? ColumnDataMapper.Value;

            StringBuilder script = new StringBuilder();

            if (target is IDictionary <string, object> )
            {
                tableName = tableName.IsNullOrWhiteSpace() ? "Table" : tableName;
                var eo = target as IDictionary <string, Object>;

                return(CreateTableScript(tableName, eo.ToDictionary(kvp => kvp.Key, kvp1 => kvp1.Value.GetNType()), keyColumns.SplitNTrim(), columnDataMapper));
            }
            else
            {
                if (tableName.IsNullOrWhiteSpace())
                {
                    TableAttribute attr = TypeDescriptor.GetAttributes(objectType).OfType <TableAttribute>().FirstOrDefault();
                    if (attr != null && !attr.Name.IsNullOrWhiteSpace())
                    {
                        tableName = attr.Name;
                    }
                    else
                    {
                        tableName = objectType.Name;
                    }
                }

                string[] keyColumnArray = null;
                if (keyColumns.IsNullOrEmpty())
                {
                    keyColumnArray = ChoTypeDescriptor.GetProperties(objectType).Where(pd => pd.Attributes.OfType <KeyAttribute>().Any()).Select(p => p.Name).ToArray();
                }
                else
                {
                    keyColumnArray = keyColumns.SplitNTrim();
                }

                return(CreateTableScript(tableName, ChoTypeDescriptor.GetProperties(objectType).ToDictionary(pd => pd.Name, pd => pd.PropertyType, null), keyColumnArray, columnDataMapper));
            }
        }
        private void DiscoverRecordFields(Type recordType, bool clear = true)
        {
            if (recordType == null)
            {
                return;
            }

            if (RecordMapType == null)
            {
                RecordMapType = recordType;
            }

            if (clear)
            {
                XmlRecordFieldConfigurations.Clear();
            }
            DiscoverRecordFields(recordType, null,
                                 ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()).Any());
        }
Ejemplo n.º 22
0
        private void RegisterYamlTagMapForType(Type recordType)
        {
            if (_refDict.Contains(recordType))
            {
                return;
            }
            else
            {
                _refDict.Add(recordType);
            }

            if (recordType.IsDynamicType() || recordType.IsSpecialCollectionType())
            {
                return;
            }

            var tagMapAttrs = ChoTypeDescriptor.GetTypeAttributes <ChoYamlTagMapAttribute>(recordType).ToArray();

            if (tagMapAttrs.Length > 0)
            {
                foreach (var tagMapAttr in tagMapAttrs)
                {
                    if (tagMapAttr != null && !tagMapAttr.TagMap.IsNullOrWhiteSpace())
                    {
                        WithTagMapping(tagMapAttr.TagMap, recordType, tagMapAttr.Alias);
                    }
                }
            }
            else
            {
                WithTagMapping("!", recordType, false);
            }

            foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
            {
                if (pd.PropertyType.IsSimple())
                {
                    continue;
                }

                RegisterYamlTagMapForType(pd.PropertyType);
            }
        }
        private string[] GetFields(List <object> records)
        {
            string[] fieldNames = null;
            Type     recordType = ElementType == null?records.First().GetType() : ElementType;

            Configuration.RecordType = recordType.ResolveType();

            Configuration.IsDynamicObject = recordType.IsDynamicType();
            if (!Configuration.IsDynamicObject)
            {
                if (Configuration.FixedLengthRecordFieldConfigurations.Count == 0)
                {
                    Configuration.MapRecordFields(Configuration.RecordType);
                }
            }

            if (Configuration.IsDynamicObject)
            {
                var record = new Dictionary <string, object>();
                foreach (var r in records.Select(r => (IDictionary <string, Object>)r.ToDynamicObject()))
                {
                    record.Merge(r);
                }

                if (Configuration.UseNestedKeyFormat)
                {
                    fieldNames = record.Flatten(Configuration.NestedColumnSeparator).ToDictionary().Keys.ToArray();
                }
                else
                {
                    fieldNames = record.Keys.ToArray();
                }
            }
            else
            {
                fieldNames = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(Configuration.RecordType).Select(pd => pd.Name).ToArray();
                if (fieldNames.Length == 0)
                {
                    fieldNames = ChoType.GetProperties(Configuration.RecordType).Select(p => p.Name).ToArray();
                }
            }
            return(fieldNames);
        }
Ejemplo n.º 24
0
        private void CheckColumnOrderStrict(object rec)
        {
            if (Configuration.IsDynamicObject)
            {
                var eoDict = rec.ToDynamicObject() as IDictionary <string, Object>;

                if (!Enumerable.SequenceEqual(Configuration.CSVRecordFieldConfigurations.OrderBy(v => v.FieldPosition).Select(v => v.Name), eoDict.Keys))
                {
                    throw new ChoParserException("Incorrect column order found.");
                }
            }
            else
            {
                PropertyDescriptor[] pds = ChoTypeDescriptor.GetProperties <ChoCSVRecordFieldAttribute>(rec.GetType()).ToArray();
                if (!Enumerable.SequenceEqual(Configuration.CSVRecordFieldConfigurations.OrderBy(v => v.FieldPosition).Select(v => v.Name), pds.Select(pd => pd.Name)))
                {
                    throw new ChoParserException("Incorrect column order found.");
                }
            }
        }
Ejemplo n.º 25
0
 private static void PopulateParams(SqlCommand cmd, object target, Dictionary <string, PropertyInfo> PIDict)
 {
     if (target is ExpandoObject)
     {
         var eo = target as IDictionary <string, Object>;
         foreach (KeyValuePair <string, object> kvp in eo)
         {
             cmd.Parameters["@{0}".FormatString(kvp.Key)].Value = kvp.Value == null ? DBNull.Value : kvp.Value;
         }
     }
     else
     {
         object pv = null;
         foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType()))
         {
             pv = PIDict[pd.Name].GetValue(target);
             cmd.Parameters["@{0}".FormatString(pd.Name)].Value = pv == null ? DBNull.Value : pv;
         }
     }
 }
Ejemplo n.º 26
0
        public static Dictionary <string, object> ToDictionary(this object target)
        {
            ChoGuard.ArgumentNotNull(target, "Target");

            if (target is IDictionary <string, object> )
            {
                return((Dictionary <string, object>)target);
            }
            if (target is IDictionary)
            {
                Dictionary <string, object> dict1 = new Dictionary <string, object>();
                foreach (var kvp in ((IDictionary)target).Keys)
                {
                    dict1.Add(kvp.ToNString(), ((IDictionary)target)[kvp]);
                }
                return(dict1);
            }
            if (target is IEnumerable <KeyValuePair <string, object> > )
            {
                return(new List <KeyValuePair <string, object> >(target as IEnumerable <KeyValuePair <string, object> >).ToDictionary(x => x.Key, x => x.Value));
            }
            if (target is IEnumerable <Tuple <string, object> > )
            {
                return(new List <Tuple <string, object> >(target as IEnumerable <Tuple <string, object> >).ToDictionary(x => x.Item1, x => x.Item2));
            }
            if (target is IList)
            {
                return(((IList)(target)).OfType <object>().Select((item, index) => new KeyValuePair <string, object>("Column_{0}".FormatString(index), item)).ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
            }

            Dictionary <string, object> dict = new Dictionary <string, object>();

            foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(target.GetType()))
            {
                dict.Add(pd.Name, ChoType.GetPropertyValue(target, pd.Name));
            }

            return(dict);
        }
        private void DiscoverRecordFields(Type recordType)
        {
            if (!IsDynamicObject)
            {
                XmlRecordFieldConfigurations.Clear();

                if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()).Any())
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()))
                    {
                        //if (!pd.PropertyType.IsSimple())
                        //    throw new ChoRecordConfigurationException("Property '{0}' is not a simple type.".FormatString(pd.Name));

                        var obj = new ChoXmlRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().First());
                        if (obj.XPath.IsNullOrWhiteSpace())
                        {
                            obj.XPath = $"//{obj.FieldName}|//@{obj.FieldName}";
                        }

                        obj.FieldType = pd.PropertyType;
                        XmlRecordFieldConfigurations.Add(obj);
                    }
                }
                else
                {
                    foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType))
                    {
                        //if (!pd.PropertyType.IsSimple())
                        //    throw new ChoRecordConfigurationException("Property '{0}' is not a simple type.".FormatString(pd.Name));

                        var obj = new ChoXmlRecordFieldConfiguration(pd.Name, $"//{pd.Name}|//@{pd.Name}");
                        obj.FieldType = pd.PropertyType;
                        XmlRecordFieldConfigurations.Add(obj);
                    }
                }
            }
        }
Ejemplo n.º 28
0
        public static bool IsKeyValueType(this Type type)
        {
            bool isKVPObject = false;

            if (typeof(IChoKeyValueType).IsAssignableFrom(type))
            {
                isKVPObject = true;
            }
            else
            {
                var isKVPAttrDefined = ChoTypeDescriptor.GetTypeAttribute <ChoKeyValueTypeAttribute>(type) != null;
                if (isKVPAttrDefined)
                {
                    var kP = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(type).FirstOrDefault();
                    var vP = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(type).FirstOrDefault();
                    if (kP != null && vP != null)
                    {
                        isKVPObject = true;
                    }
                }
            }

            return(isKVPObject);
        }
Ejemplo n.º 29
0
        public override void WriteJson(JsonWriter writer, object value,
                                       JsonSerializer serializer)
        {
            if (value == null)
            {
                return;
            }
            Type objectType = value.GetType();

            var dict = new Dictionary <string, string>();

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                IChoKeyValueType kvp = value as IChoKeyValueType;
                var propName         = kvp.Key.ToNString();
                var propValue        = kvp.Value.ToNString();
                if (!propName.IsNullOrWhiteSpace())
                {
                    dict.Add(propName, propValue);
                }
            }
            else
            {
                var kP        = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                var vP        = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();
                var propName  = ChoType.GetPropertyValue(value, kP.Name).ToNString();
                var propValue = ChoType.GetPropertyValue(value, vP.Name).ToNString();

                if (!propName.IsNullOrWhiteSpace())
                {
                    dict.Add(propName, propValue);
                }
            }

            serializer.Serialize(writer, dict);
        }
Ejemplo n.º 30
0
        public override object ReadJson(JsonReader reader, Type objectType,
                                        object existingValue, JsonSerializer serializer)
        {
            var dict = serializer.Deserialize <Dictionary <string, string> >(reader);
            var item = dict.First();

            var rec = ChoActivator.CreateInstance(objectType);

            if (typeof(IChoKeyValueType).IsAssignableFrom(objectType))
            {
                IChoKeyValueType kvp = rec as IChoKeyValueType;
                kvp.Key   = item.Key;
                kvp.Value = item.Value;
            }
            else
            {
                var kP = ChoTypeDescriptor.GetProperties <ChoKeyAttribute>(objectType).FirstOrDefault();
                var vP = ChoTypeDescriptor.GetProperties <ChoValueAttribute>(objectType).FirstOrDefault();

                ChoType.SetPropertyValue(rec, kP.Name, item.Key);
                ChoType.SetPropertyValue(rec, vP.Name, item.Value);
            }
            return(rec);
        }