示例#1
0
        public static XElement FieldValuesToXml(object objectWithValue)
        {
            XElement res     = new XElement(@"ConstsUniverse");
            Type     objType = objectWithValue.GetType();

            foreach (var fieldInfo in objType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(IFormattable).IsAssignableFrom(fieldInfo.FieldType) || typeof(bool).IsAssignableFrom(fieldInfo.FieldType))
                {
                    Type   fieldType           = fieldInfo.FieldType;
                    object fieldValue          = fieldInfo.GetValue(objectWithValue);
                    NumericValuesAttribute atr = fieldInfo.GetCustomAttribute <NumericValuesAttribute>();

                    XElement xelLoc = new XElement(fieldInfo.Name);
                    xelLoc.Add(
                        Convert.ToString(fieldValue)
                        );
                    res.Add(xelLoc);
                }
            }
            return(res);
        }
示例#2
0
        public static void FieldValuesFromXml(object objectToBeInit, XElement xel)
        {
            Type objType = objectToBeInit.GetType();
            Dictionary <string, object> convertedValuesDictionary = new Dictionary <string, object>();

            foreach (var fieldInfo in objType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(IFormattable).IsAssignableFrom(fieldInfo.FieldType) || typeof(bool).IsAssignableFrom(fieldInfo.FieldType))
                {
                    Type   fieldType         = fieldInfo.FieldType;
                    object fieldValueFromXml = xel.Element(fieldInfo.Name).Value;
                    object convertedFieldValue;
                    NumericValuesAttribute atr = fieldInfo.GetCustomAttribute <NumericValuesAttribute>();
                    if (fieldType.GetCustomAttribute <ParsebleAttribute>() != null)
                    {
                        convertedFieldValue = ParsebleAttribute.Parse(fieldValueFromXml.ToString(), fieldType);
                    }
                    else
                    {
                        convertedFieldValue = Convert.ChangeType(fieldValueFromXml, fieldType);
                    }
                    if (convertedFieldValue.GetType() != fieldType)
                    {
                        throw new Exception(string.Format("Wrong xml value for field {0}.", fieldInfo.Name));
                    }
                    convertedValuesDictionary.Add(fieldInfo.Name, convertedFieldValue);
                }
            }
            foreach (var fieldInfo in objType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
            {
                if (typeof(IFormattable).IsAssignableFrom(fieldInfo.FieldType) || typeof(bool).IsAssignableFrom(fieldInfo.FieldType))
                {
                    fieldInfo.SetValue(objectToBeInit, convertedValuesDictionary[fieldInfo.Name]);
                }
            }
        }