Skip to content

Instantly share code, notes, and snippets.

@tranquan
Created November 12, 2018 16:32
Show Gist options
  • Save tranquan/a3b32ef57ab0b0ff9a2e9a07a6bd0bd9 to your computer and use it in GitHub Desktop.
Save tranquan/a3b32ef57ab0b0ff9a2e9a07a6bd0bd9 to your computer and use it in GitHub Desktop.
A way to override property in umbraco
// source: https://www.formfakten.de/themen/2018/08/published-content-with-overridden-properties
public class OverriddenProperty : IPublishedProperty
{
private readonly string alias;
private readonly object value;
public OverriddenProperty(string alias, object value)
{
this.alias = alias;
this.value = value;
}
public string PropertyTypeAlias => alias;
public bool HasValue => true;
public object DataValue => value;
public object Value => value;
public object XPathValue => null;
}
public class OverriddenPublishedContent : PublishedContentWrapped
{
private IPublishedContent _mirror;
public OverriddenPublishedContent(IPublishedContent mirror, IPublishedContent content) : base(content)
{
_mirror = mirror;
}
public override ICollection<IPublishedProperty> Properties
{
get
{
return base.Properties;
}
}
public override IPublishedProperty GetProperty(string alias)
{
if (alias == "Theme")
{
return this._mirror.GetProperty(alias);
}
return base.GetProperty(alias);
}
public override IPublishedProperty GetProperty(string alias, bool recurse)
{
if (alias == "Theme")
{
return this._mirror.GetProperty(alias);
}
return base.GetProperty(alias, recurse);
}
public object GetPropertyValue<T>(string alias)
{
if (alias == "Theme")
{
return this._mirror.GetProperty(alias).Value;
}
return base.GetProperty(alias).Value;
}
public object GetPropertyValue<T>(string alias, bool recursive)
{
if (alias == "Theme")
{
return this._mirror.GetProperty(alias, recursive).Value;
}
return base.GetProperty(alias, recursive).Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment