44 lines
1.8 KiB
C#
44 lines
1.8 KiB
C#
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
namespace Umbraco.Core.Models
|
|||
|
|
{
|
|||
|
|
public static class ContentExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Set property values by alias with an annonymous object
|
|||
|
|
/// </summary>
|
|||
|
|
public static void PropertyValues(this IContent content, object value)
|
|||
|
|
{
|
|||
|
|
if (value == null)
|
|||
|
|
throw new Exception("No properties has been passed in");
|
|||
|
|
|
|||
|
|
var propertyInfos = value.GetType().GetProperties();
|
|||
|
|
foreach (var propertyInfo in propertyInfos)
|
|||
|
|
{
|
|||
|
|
//Check if a PropertyType with alias exists thus being a valid property
|
|||
|
|
var propertyType = content.PropertyTypes.FirstOrDefault(x => x.Alias == propertyInfo.Name);
|
|||
|
|
if (propertyType == null)
|
|||
|
|
throw new Exception(
|
|||
|
|
string.Format(
|
|||
|
|
"The property alias {0} is not valid, because no PropertyType with this alias exists",
|
|||
|
|
propertyInfo.Name));
|
|||
|
|
|
|||
|
|
//Check if a Property with the alias already exists in the collection thus being updated or inserted
|
|||
|
|
var item = content.Properties.FirstOrDefault(x => x.Alias == propertyInfo.Name);
|
|||
|
|
if (item != null)
|
|||
|
|
{
|
|||
|
|
item.Value = propertyInfo.GetValue(value, null);
|
|||
|
|
//Update item with newly added value
|
|||
|
|
content.Properties.Add(item);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//Create new Property to add to collection
|
|||
|
|
var property = propertyType.CreatePropertyFromValue(propertyInfo.GetValue(value, null));
|
|||
|
|
content.Properties.Add(property);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|