-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathPublishedModelUtility.cs
67 lines (56 loc) · 2.96 KB
/
PublishedModelUtility.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Linq;
using System.Linq.Expressions;
using Umbraco.Web.Composing;
using Umbraco.Core.Models.PublishedContent;
namespace Umbraco.ModelsBuilder.Umbraco
{
public static class PublishedModelUtility
{
// looks safer but probably useless... ppl should not call these methods directly
// and if they do... they have to take care about not doing stupid things
//public static PublishedPropertyType GetModelPropertyType2<T>(Expression<Func<T, object>> selector)
// where T : PublishedContentModel
//{
// var type = typeof (T);
// var s1 = type.GetField("ModelTypeAlias", BindingFlags.Public | BindingFlags.Static);
// var alias = (s1.IsLiteral && s1.IsInitOnly && s1.FieldType == typeof(string)) ? (string)s1.GetValue(null) : null;
// var s2 = type.GetField("ModelItemType", BindingFlags.Public | BindingFlags.Static);
// var itemType = (s2.IsLiteral && s2.IsInitOnly && s2.FieldType == typeof(PublishedItemType)) ? (PublishedItemType)s2.GetValue(null) : 0;
// var contentType = PublishedContentType.Get(itemType, alias);
// // etc...
//}
public static IPublishedContentType GetModelContentType(PublishedItemType itemType, string alias)
{
var facade = Current.UmbracoContext.PublishedSnapshot; // fixme inject!
switch (itemType)
{
case PublishedItemType.Content:
return facade.Content.GetContentType(alias);
case PublishedItemType.Media:
return facade.Media.GetContentType(alias);
case PublishedItemType.Member:
return facade.Members.GetContentType(alias);
default:
throw new ArgumentOutOfRangeException(nameof(itemType));
}
}
public static IPublishedPropertyType GetModelPropertyType<TModel, TValue>(IPublishedContentType contentType, Expression<Func<TModel, TValue>> selector)
//where TModel : PublishedContentModel // fixme PublishedContentModel _or_ PublishedElementModel
{
// fixme therefore, missing a check on TModel here
var expr = selector.Body as MemberExpression;
if (expr == null)
throw new ArgumentException("Not a property expression.", nameof(selector));
// there _is_ a risk that contentType and T do not match
// see note above : accepted risk...
var attr = expr.Member
.GetCustomAttributes(typeof (ImplementPropertyTypeAttribute), false)
.OfType<ImplementPropertyTypeAttribute>()
.SingleOrDefault();
if (string.IsNullOrWhiteSpace(attr?.Alias))
throw new InvalidOperationException($"Could not figure out property alias for property \"{expr.Member.Name}\".");
return contentType.GetPropertyType(attr.Alias);
}
}
}