/// <summary> /// Get a value (of specified type) from a content item's property. /// </summary> /// <typeparam name="T">The type.</typeparam> /// <param name="item">The content item.</param> /// <param name="propertyAlias">alias of property to get</param> /// <returns>default(T) or property value cast to (T)</returns> public static T GetProperty <T>(this Content item, string propertyAlias) { // check to see if return object handles it's own object hydration if (typeof(uQuery.IGetProperty).IsAssignableFrom(typeof(T))) { // create new instance of T with empty constructor uQuery.IGetProperty t = (uQuery.IGetProperty)Activator.CreateInstance <T>(); // call method to hydrate the object from a string value t.LoadPropertyValue(item.GetProperty <string>(propertyAlias)); return((T)t); } var typeConverter = TypeDescriptor.GetConverter(typeof(T)); if (typeConverter != null) { // Boolean if (typeof(T) == typeof(bool)) { return((T)typeConverter.ConvertFrom(item.GetPropertyAsBoolean(propertyAlias).ToString())); } // XmlDocument else if (typeof(T) == typeof(XmlDocument)) { var xmlDocument = new XmlDocument(); try { xmlDocument.LoadXml(item.GetPropertyAsString(propertyAlias)); } catch { } return((T)((object)xmlDocument)); } // // umbraco.MacroEngines.DynamicXml // else if (typeof(T) == typeof(DynamicXml)) // { // try // { // return (T)((object)new DynamicXml(item.GetPropertyAsString(propertyAlias))); // } // catch // { // } // } try { return((T)typeConverter.ConvertFromString(item.GetPropertyAsString(propertyAlias))); } catch { } } return(default(T)); }
/// <summary> /// Get a value of type T from a property /// </summary> /// <typeparam name="T">type T to cast to</typeparam> /// <param name="node">an umbraco.interfaces.INode object</param> /// <param name="propertyAlias">alias of property to get</param> /// <returns>default(T) or property cast to (T)</returns> public static T GetProperty <T>(this INode node, string propertyAlias) { // check to see if return object handles it's own object hydration if (typeof(uQuery.IGetProperty).IsAssignableFrom(typeof(T))) { // create new instance of T with empty constructor uQuery.IGetProperty t = (uQuery.IGetProperty)Activator.CreateInstance <T>(); // call method to hydrate the object from a string value t.LoadPropertyValue(node.GetProperty <string>(propertyAlias)); return((T)t); } //TODO: This should be converted to our extension method TryConvertTo<T> .... var typeConverter = TypeDescriptor.GetConverter(typeof(T)); if (typeConverter != null) { // Boolean if (typeof(T) == typeof(bool)) { // Use the GetPropertyAsBoolean method, as this handles true also being stored as "1" return((T)typeConverter.ConvertFrom(node.GetPropertyAsBoolean(propertyAlias).ToString())); } // XmlDocument else if (typeof(T) == typeof(XmlDocument)) { var xmlDocument = new XmlDocument(); try { xmlDocument.LoadXml(node.GetPropertyAsString(propertyAlias)); } catch { // xml probably invalid } return((T)((object)xmlDocument)); } // // umbraco.MacroEngines DynamicXml // else if (typeof(T) == typeof(DynamicXml)) // { // try // { // return (T)((object)new DynamicXml(node.GetPropertyAsString(propertyAlias))); // } // catch // { // } // } else { try { return((T)typeConverter.ConvertFromString(node.GetPropertyAsString(propertyAlias))); } catch { } } } return(default(T)); }