2012-09-20 12:42:43 +07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
2012-09-11 05:58:16 +07:00
|
|
|
using System.Linq;
|
2012-10-01 02:48:08 +05:00
|
|
|
using Umbraco.Core.Dynamics;
|
2012-09-11 05:58:16 +07:00
|
|
|
using Umbraco.Core.Models;
|
2012-09-20 12:42:43 +07:00
|
|
|
using umbraco.interfaces;
|
2012-09-11 05:58:16 +07:00
|
|
|
|
|
|
|
|
namespace Umbraco.Core
|
|
|
|
|
{
|
2012-10-04 01:31:08 +05:00
|
|
|
/// <summary>
|
|
|
|
|
/// Extension methods for IPublishedContent
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class PublishedContentExtensions
|
2012-09-11 05:58:16 +07:00
|
|
|
{
|
2012-10-01 02:48:08 +05:00
|
|
|
|
2012-10-02 01:35:39 +05:00
|
|
|
public static dynamic AsDynamic(this IPublishedContent doc)
|
2012-10-01 02:48:08 +05:00
|
|
|
{
|
2012-10-04 01:31:08 +05:00
|
|
|
var dd = new DynamicPublishedContentBase(doc);
|
2012-10-01 02:48:08 +05:00
|
|
|
return dd.AsDynamic();
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-11 05:58:16 +07:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the property as the specified type, if the property is not found or does not convert
|
|
|
|
|
/// then the default value of type T is returned.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="prop"></param>
|
|
|
|
|
/// <param name="alias"></param>
|
|
|
|
|
/// <returns></returns>
|
2012-10-02 01:35:39 +05:00
|
|
|
public static T GetPropertyValue<T>(this IPublishedContent prop, string alias)
|
2012-09-11 05:58:16 +07:00
|
|
|
{
|
|
|
|
|
return prop.GetPropertyValue<T>(alias, default(T));
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-02 01:35:39 +05:00
|
|
|
public static T GetPropertyValue<T>(this IPublishedContent prop, string alias, T ifCannotConvert)
|
2012-09-11 05:58:16 +07:00
|
|
|
{
|
|
|
|
|
var p = prop.GetProperty(alias);
|
|
|
|
|
if (p == null)
|
|
|
|
|
return default(T);
|
2012-09-27 15:41:24 -02:00
|
|
|
var converted = p.Value.TryConvertTo<T>();
|
2012-09-11 05:58:16 +07:00
|
|
|
if (converted.Success)
|
|
|
|
|
return converted.Result;
|
|
|
|
|
return ifCannotConvert;
|
|
|
|
|
}
|
2012-09-20 12:42:43 +07:00
|
|
|
|
2012-09-11 05:58:16 +07:00
|
|
|
}
|
|
|
|
|
}
|