예제 #1
0
        public void CanConvertTypesToString()
        {
            bool   falseVal  = false;
            object converted = ReflectionTools.ConvertEx(typeof(string), falseVal);

            Assert.AreEqual("False", converted, "Invalid value!");

            // We may add additional cases as we see fit...
        }
        public void CanConvertGuidToString()
        {
            Guid   g = Guid.NewGuid();
            string s = g.ToString();

            string comp = ReflectionTools.ConvertEx <string>(g);

            Assert.AreEqual(comp, s, "Invalid string value for GUID");
        }
예제 #3
0
        // --------------------------------------------------------------------------------------------------------------------------
        // TODO: Put this with some XML tools?
        public static T ResolveAttribute <T>(XElement elem, string attrName, T fallback = default(T))
        {
            XAttribute attr = elem.Attribute(attrName);

            if (attr == null)
            {
                return(fallback);
            }

            return(ReflectionTools.ConvertEx <T>(attr.Value));
        }
예제 #4
0
        // --------------------------------------------------------------------------------------------------------------------------
        public static void CopyProperty(string name, Type tFrom, Type tTo, object from, object to)
        {
            PropertyInfo pFrom = ReflectionTools.GetPropertyInfo(tFrom, name, false, false);
            PropertyInfo pTo   = ReflectionTools.GetPropertyInfo(tTo, name, false, false);

            if (pFrom == null | pTo == null)
            {
                return;
            }

            pTo.SetValue(to, ReflectionTools.ConvertEx(pTo.PropertyType, pFrom.GetValue(from, null)), null);
        }
예제 #5
0
        public void CanConvertCompatibleValueToNullableType()
        {
            Type   expectedType = typeof(bool?);
            bool   actualVal    = true;
            object converted    = ReflectionTools.ConvertEx(expectedType, actualVal);

            Assert.IsNotNull(converted, "Converted value should not be null!");

            // NOTE: Because we have a value that we are trying to convert (instead of null) we will actually get back
            // its type, instead of a nullable version.  This is what the framework will output when we use the nullable constructor
            // with a value arg.  It may seem odd that we don't get the *? that we expect, but that is just how it works.
            Assert.AreEqual(converted.GetType(), actualVal.GetType());
        }
예제 #6
0
        // --------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Deserialize the given XElement into the corresponding type.
        /// </summary>
        /// <remarks>
        /// At this time, this function will only deserialize attributes defined on the fragment.  Child elements
        /// will be ignored.
        /// </remarks>
        public static T DeserializeFragment <T>(XElement src)
            where T : new()
        {
            T    res = new T();
            Type t   = typeof(T);

            var props = ReflectionTools.GetPropertiesWithAttribute <T, XmlAttributeAttribute>().ToList();

            foreach (var p in props)
            {
                // string name = a.Name;
                string     attrName = ReflectionTools.GetAttribute <XmlAttributeAttribute>(p).AttributeName;
                XAttribute xattr    = src.Attribute(attrName);
                if (xattr != null)
                {
                    object val = ReflectionTools.ConvertEx(p.PropertyType, xattr.Value);
                    p.SetValue(res, val, null);
                }
            }


            return(res);
        }
        public void CanConvertNullableDateTime()
        {
            DateTime?val = ReflectionTools.ConvertEx <DateTime?>(null);

            Assert.IsNull(val, "The converted value should be null!");
        }