public SettingDescriptor(Settings settingsInstance,
                                 string name,
                                 ConstraintChecker constraintCheck)
        {
            // We are a bit aggressive in our error checking here--
            // The most likely source of errors will be people that maintain the code in the future,
            // and we want to weed out as many newbie mistakes as possible.
            Contract.Requires(!string.IsNullOrEmpty(name),
                              context => "name cannot be null or empty.");
            Contract.Requires(settingsInstance != null,
                              context => String.Format("Null settingsInstance now allowed for {0}", name));
            Contract.Requires(settingsInstance.GetType().GetProperties().Any(s => s.Name == name),
                              context => string.Format("The settingsInstance does not contain a \"{0}\" property", name));

            ConstraintChecker = constraintCheck;
            Name             = name;
            SettingsInstance = settingsInstance;

            InstanceName = settingsInstance.GetType().Name;
            PropInfo     = settingsInstance.GetType().GetProperty(name);

            IsAccessDisallowed = DisallowedPropertyNames.Contains(name);

            var obsoleteAttribute = PropInfo.GetCustomAttributes(true).OfType <ObsoleteAttribute>().FirstOrDefault();

            IsObsolete = (obsoleteAttribute != null);

            ObsoleteMessage = IsObsolete ? obsoleteAttribute.Message : string.Empty;
        }
Esempio n. 2
0
        private static void FillData(OBJS.Data DataSet, Type BaseType, object Item)
        {
            List <PropertyInfo> Props = GetProprites(OBJS.Types.Prop, BaseType);

            List <OBJS.Data.DataValue> DataValues = new List <OBJS.Data.DataValue>();

            foreach (PropertyInfo Prop in Props)
            {
                object ItemObject = Prop.GetValue(Item, null);
                string ItemValue  = (ItemObject != null) ? ItemObject.ToString() : "";

                OBJS.Data.DataValue DataValue = new OBJS.Data.DataValue();
                DataValue.Name  = Prop.Name;
                DataValue.Value = ItemValue;

                DataValues.Add(DataValue);
            }


            List <PropertyInfo> AttributePop = GetProprites(OBJS.Types.Attribute, BaseType);

            foreach (PropertyInfo PropInfo in AttributePop)
            {
                OBJS.XMLDatabaseRetriveItem XMLDatabaseRetriveItem = (OBJS.XMLDatabaseRetriveItem)PropInfo.GetCustomAttributes(typeof(OBJS.XMLDatabaseRetriveItem), true).FirstOrDefault();
                object ItemObject = PropInfo.GetValue(Item, null);
                string ItemValue  = (ItemObject != null) ? ItemObject.ToString() : "";

                OBJS.Data.DataValue DataValue = DataValues.Find(A => A.Name == XMLDatabaseRetriveItem.NodeName);

                if (DataValue != null)
                {
                    DataValue.Attributes.Add(new OBJS.Data.DataValue()
                    {
                        Name  = XMLDatabaseRetriveItem.AttributeName,
                        Value = ItemValue
                    });
                }
                else
                {
                    DataValue      = new OBJS.Data.DataValue();
                    DataValue.Name = XMLDatabaseRetriveItem.NodeName;

                    DataValue.Attributes.Add(new OBJS.Data.DataValue()
                    {
                        Name  = XMLDatabaseRetriveItem.AttributeName,
                        Value = ItemValue
                    });
                    DataValues.Add(DataValue);
                }
            }

            DataValues.ForEach(T =>
            {
                DataSet.AddData(T);
            });
        }
        /// <summary>
        /// Map data from DataReader to an Object
        /// </summary>
        /// <typeparam name="T">Object to map</typeparam>
        /// <param name="DataRead">Datareader</param>
        /// <param name="UseElementMapper">Map maked with ElementMapper Attribute</param>
        /// <returns>List of Object with data contains in DataReader</returns>
        public static List <T> MapToList <T>(this DbDataReader DataRead, MapWith MappedWith = MapWith.Propertie) where T : new()
        {
            List <T> ListReturn = null;
            // Define object type
            var Entity = typeof(T);
            // Define dictionary
            var PropDict = new Dictionary <string, PropertyInfo>();
            var AttrDict = new Dictionary <string, PropertyInfo>();

            try
            {
                if (DataRead != null && DataRead.HasRows)
                {
                    ListReturn = new List <T>();
                    // Get each properties of the Object
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                    // Get each attribute of properites
                    foreach (PropertyInfo PropInfo in Props)
                    {
                        object[] AttrCollection = PropInfo.GetCustomAttributes(true);

                        foreach (object Attr in AttrCollection)
                        {
                            if (Attr is ElementMapper)
                            {
                                // Get the ElementMapper name
                                ElementMapper Element = Attr as ElementMapper;
                                if (Element != null)
                                {
                                    AttrDict.Add(Element.ElementName.ToUpper(), PropInfo);
                                }
                            }
                        }
                    }

                    while (DataRead.Read())
                    {
                        T newObject = new T();
                        for (int Index = 0; Index < DataRead.FieldCount; Index++)
                        {
                            if ((MappedWith == MapWith.Propertie) || (MappedWith == MapWith.All))
                            {
                                // Search reader name in Dictionary properties
                                if (PropDict.ContainsKey(DataRead.GetName(Index).ToUpper()))
                                {
                                    var Info = PropDict[DataRead.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        // Set value in Object
                                        var Val = DataRead.GetValue(Index);

                                        try
                                        {
                                            if (Info.PropertyType == Val.GetType())
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                            }
                                            else if (Info.PropertyType == typeof(string))
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val.ToString(), null);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            throw;
                                        }
                                    }
                                }
                            }

                            if ((MappedWith == MapWith.Attribute) || (MappedWith == MapWith.All))
                            {
                                // Search reader name in Dictionary attributes
                                if (AttrDict.ContainsKey(DataRead.GetName(Index).ToUpper()))
                                {
                                    var Info = AttrDict[DataRead.GetName(Index).ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        // Set value in Object
                                        var Val = DataRead.GetValue(Index);

                                        try
                                        {
                                            if (Info.PropertyType == Val.GetType())
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val, null);
                                            }
                                            else if (Info.PropertyType == typeof(string))
                                            {
                                                Info.SetValue(newObject, (Val == DBNull.Value) ? null : Val.ToString(), null);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            throw;
                                        }
                                    }
                                }
                            }
                        }
                        // Add current object in the list
                        ListReturn.Add(newObject);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataReaderExtension : " + ex.Message);
                throw;
            }

            return(ListReturn);
        }
        /// <summary>
        /// Map data from List Dictionnary to an Object
        /// </summary>
        /// <typeparam name="T">Object to map</typeparam>
        /// <param name="ListDict">List Dictionary</param>
        /// <param name="UseElementMapper">Map maked with ElementMapper Attribute</param>
        /// <returns>List of Object with data contains in List Dictionary</returns>
        public static List <T> MapToList <T>(this List <Dictionary <string, string> > ListDict, MapWith MappedWith = MapWith.Propertie) where T : new()
        {
            List <T> ListReturn = null;
            // Define object type
            var Entity = typeof(T);
            // Define dictionary
            var PropDict = new Dictionary <string, PropertyInfo>();
            var AttrDict = new Dictionary <string, PropertyInfo>();

            try
            {
                if (ListDict != null && ListDict.Count > 0)
                {
                    ListReturn = new List <T>();
                    // Get each properties of the Object
                    var Props = Entity.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    PropDict = Props.ToDictionary(p => p.Name.ToUpper(), p => p);
                    // Get each attribute of properites
                    foreach (PropertyInfo PropInfo in Props)
                    {
                        object[] AttrCollection = PropInfo.GetCustomAttributes(true);

                        foreach (object Attr in AttrCollection)
                        {
                            if (Attr is ElementMapper)
                            {
                                // Get the ElementMapper name
                                ElementMapper Element = Attr as ElementMapper;
                                if (Element != null)
                                {
                                    AttrDict.Add(Element.ElementName.ToUpper(), PropInfo);
                                }
                            }
                        }
                    }

                    foreach (Dictionary <string, string> Dict in ListDict)
                    {
                        T newObject = new T();

                        for (int Index = 0; Index < Dict.Count; Index++)
                        {
                            if ((MappedWith == MapWith.Propertie) || (MappedWith == MapWith.All))
                            {
                                if (PropDict.ContainsKey(Dict.ElementAt(Index).Key.ToUpper()))
                                {
                                    var Info = PropDict[Dict.ElementAt(Index).Key.ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = Dict.ElementAt(Index).Value;
                                        if (Info.PropertyType == Val.GetType())
                                        {
                                            Info.SetValue(newObject, Val);
                                        }
                                        else if (Info.PropertyType == typeof(string))
                                        {
                                            Info.SetValue(newObject, Val.ToString());
                                        }
                                    }
                                }
                            }

                            if ((MappedWith == MapWith.Attribute) || (MappedWith == MapWith.All))
                            {
                                if (AttrDict.ContainsKey(Dict.ElementAt(Index).Key.ToUpper()))
                                {
                                    var Info = AttrDict[Dict.ElementAt(Index).Key.ToUpper()];
                                    if ((Info != null) && Info.CanWrite)
                                    {
                                        var Val = Dict.ElementAt(Index).Value;
                                        if (Info.PropertyType == Val.GetType())
                                        {
                                            Info.SetValue(newObject, Val);
                                        }
                                        else if (Info.PropertyType == typeof(string))
                                        {
                                            Info.SetValue(newObject, Val.ToString());
                                        }
                                    }
                                }
                            }
                        }

                        ListReturn.Add(newObject);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataReaderExtension : " + ex.Message);
                throw;
            }

            return(ListReturn);
        }
Esempio n. 5
0
        public static object GetItemObject(Type ListType, List <OBJS.Data.DataValue> DataValues, object[] args = null)
        {
            object SubItem = Activator.CreateInstance(ListType, args);

            try
            {
                List <PropertyInfo> ListProp = GetProprites(OBJS.Types.Prop, ListType);

                foreach (PropertyInfo PropInfo in ListProp)
                {
                    OBJS.Data.DataValue Item = DataValues.FirstOrDefault(A => A.Name == PropInfo.Name);
                    if (Item == null)
                    {
                        continue;
                    }

                    OBJS.XMLDatabaseRetriveItem XMLDatabaseRetriveItem = (OBJS.XMLDatabaseRetriveItem)PropInfo.GetCustomAttributes(typeof(OBJS.XMLDatabaseRetriveItem), true).FirstOrDefault();

                    string DataValue = Item.Value;

                    object Value = GetValue(PropInfo.PropertyType, DataValue);
                    PropInfo.SetValue(SubItem, Value, null);
                    XMLDatabaseRetriveItem.Location = Item.Location;
                }

                List <PropertyInfo> AttributePop = GetProprites(OBJS.Types.Attribute, ListType);

                foreach (PropertyInfo PropInfo in AttributePop)
                {
                    OBJS.XMLDatabaseRetriveItem XMLDatabaseRetriveItem = (OBJS.XMLDatabaseRetriveItem)PropInfo.GetCustomAttributes(typeof(OBJS.XMLDatabaseRetriveItem), true).FirstOrDefault();

                    OBJS.Data.DataValue Item = DataValues.FirstOrDefault(A => A.Name == XMLDatabaseRetriveItem.NodeName);
                    if (Item == null)
                    {
                        continue;
                    }

                    OBJS.Data.DataValue DataValueItem = Item.Attributes.FirstOrDefault(B => B.Name == XMLDatabaseRetriveItem.AttributeName);

                    if (DataValueItem == null)
                    {
                        continue;
                    }

                    string DataValue = DataValueItem.Value;

                    object Value = GetValue(PropInfo.PropertyType, DataValue);
                    PropInfo.SetValue(SubItem, Value, null);
                    XMLDatabaseRetriveItem.Location = Item.Location;
                }
            }
            catch (Exception e)
            {
                e.ToString();
            }

            return(SubItem);
        }