Created
January 31, 2013 11:48
-
-
Save leekelleher/4682330 to your computer and use it in GitHub Desktop.
Umbraco: A bridge between RazorDataTypeModelBinding and PropertyEditorValueConverter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using umbraco.MacroEngines; | |
using Umbraco.Core; | |
using Umbraco.Core.PropertyEditors; | |
namespace Our.Umbraco | |
{ | |
public class RazorDataTypeModelBindingPropertyEditorValueConverter : IPropertyEditorValueConverter | |
{ | |
private static Dictionary<Tuple<Guid, int>, Type> _razorDataTypeModelTypes = null; | |
private IRazorDataTypeModel SelectedModelBinder = null; | |
public bool IsConverterFor(Guid propertyEditorId, string docTypeAlias, string propertyTypeAlias) | |
{ | |
if (_razorDataTypeModelTypes == null) | |
{ | |
// dirty, dirty hack! | |
var internalProperty = typeof(DynamicNode).GetProperty("RazorDataTypeModelTypes", BindingFlags.Static | BindingFlags.NonPublic); | |
if (internalProperty != null) | |
{ | |
_razorDataTypeModelTypes = internalProperty.GetValue(null, null) as Dictionary<Tuple<Guid, int>, Type>; | |
} | |
} | |
if (_razorDataTypeModelTypes != null && _razorDataTypeModelTypes.Any(x => x.Key.Item1.Equals(propertyEditorId))) | |
{ | |
var type = _razorDataTypeModelTypes.FirstOrDefault(x => x.Key.Item1.Equals(propertyEditorId)).Value; | |
this.SelectedModelBinder = Activator.CreateInstance(type) as IRazorDataTypeModel; | |
return true; | |
} | |
return false; | |
} | |
public Attempt<object> ConvertPropertyValue(object value) | |
{ | |
if (value is string) | |
{ | |
var result = default(object); | |
if (this.SelectedModelBinder.Init(0, value.ToString(), out result)) | |
{ | |
return new Attempt<object>(true, result); | |
} | |
} | |
return new Attempt<object>(false, value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment