PageTypeBuilder with Dynamic properties (updated)
I’m currently working on a project where we started using PageTypeBuilder after a refactoring process. We had some dynamic properties we needed to make available. I therefore made a hack around PTB that includes support for dynamic properties.
The concept is easy. In your top PageType base class you add an attribute. Then you tag up your properties but use Dynamic Property instead of PageTypeProperty
[DynamicPropertyContainer]
public class BasePageData : TypedPageData
{
[DynamicProperty]
public string JavaScript { get; set; }
}
The code will find all class's tagged with DynamicPropertyContainer and find all properties in those classes tagged with DynamicProperty. If it finds 2 properties with the same name it will skip the last one :)
In front code you can of course use
<%=CurrentPage.JavaScript%>
I’,m using the version of PTB that is compailed for CMS 5, so it could maybe be some change in the CMS 6 version. But I needed to copy some code to my own project cause use of internal and private… (sorry couldn’t resist:) ).
The code is available as 4 cs files, and 1 dll.
Quick Tip
Some times you need to access the property instead of the underline object that PageTypeBuilder returns. So if you make a class like this
public static class ClassInfo<T>
{
public static string GetName<R>(Expression<Func<T, R>> expr)
{
var node = expr.Body as MemberExpression;
if (object.ReferenceEquals(null, node))
throw new InvalidOperationException("Expression must be of member access");
return node.Member.Name;
}
}
Then you can access the property like this
CurrentPage.Property[ClassInfo<BasePageData>(p=>p.JavaScript)]
Comments