Example #1
0
        public static IList <T> GetExchangeInfo <T> (Type type) where T : ExchangePropertyInfo, new ()
        {
            List <T> props = new List <T> ();

            foreach (PropertyInfo property in type.GetProperties())
            {
                DbColumnAttribute         boAttribute = null;
                ExchangePropertyAttribute exAttribute = null;
                foreach (object attrib in property.GetCustomAttributes(true))
                {
                    if (boAttribute == null && attrib is DbColumnAttribute)
                    {
                        boAttribute = (DbColumnAttribute)attrib;
                    }

                    if (exAttribute == null && attrib is ExchangePropertyAttribute)
                    {
                        exAttribute = (ExchangePropertyAttribute)attrib;
                    }

                    if (boAttribute != null && exAttribute != null)
                    {
                        break;
                    }
                }

                if (exAttribute == null)
                {
                    continue;
                }

                string exchangeName;
                if (boAttribute != null)
                {
                    exchangeName = Translator.GetExchangeFieldName(boAttribute.DbField);
                }
                else if (exAttribute.FieldName != DataField.NotSet)
                {
                    exchangeName = Translator.GetExchangeFieldName(exAttribute.FieldName);
                }
                else
                {
                    exchangeName = exAttribute.DefaultExchangeName;
                }

                props.Add(new T
                {
                    Name           = exchangeName,
                    IsRequired     = exAttribute.Required,
                    DefaultMapping = exAttribute.DefaultExchangeName,
                    PropertyInfo   = property
                });
            }

            props.Sort((x, y) => string.Compare(x.Name, y.Name));
            return(props);
        }
        public DataTable ToDataTable(bool stringValues)
        {
            SortedList <string, PropertyInfo> props = new SortedList <string, PropertyInfo> ();

            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                ExchangePropertyAttribute exAttribute = null;
                foreach (object attrib in property.GetCustomAttributes(true))
                {
                    exAttribute = attrib as ExchangePropertyAttribute;
                    if (exAttribute != null)
                    {
                        break;
                    }
                }

                if (exAttribute == null)
                {
                    continue;
                }

                props.Add(exAttribute.DefaultExchangeName, property);
            }

            DataTable ret = new DataTable();

            foreach (KeyValuePair <string, PropertyInfo> pair in props)
            {
                ret.Columns.Add(pair.Key, stringValues ? typeof(string) : pair.Value.PropertyType);
            }

            foreach (T obj in this)
            {
                ret.Rows.Add(props.Select(prop => prop.Value.GetValue(obj, null)).ToArray());
            }

            return(ret);
        }