Skip to content

Instantly share code, notes, and snippets.

@ProNotion
Last active November 10, 2023 14:07
Show Gist options
  • Save ProNotion/ba402802794049e587eb677d4e9b706e to your computer and use it in GitHub Desktop.
Save ProNotion/ba402802794049e587eb677d4e9b706e to your computer and use it in GitHub Desktop.
Helper Method to get Umbraco ModelsBuilder Model Property Aliases
internal static string GetModelPropertyTypeAlias<TModel>(Expression<Func<TModel, object>> selector)
where TModel : PublishedElementModel
{
Expression body = selector.Body;
// Unwrap conversions, if any
if (body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.Convert)
body = unaryExpression.Operand;
if (!(body is MemberExpression memberExpression))
throw new ArgumentException("Not a property expression.", nameof(selector));
ImplementPropertyTypeAttribute propertyTypeAttribute = memberExpression.Member.GetCustomAttribute<ImplementPropertyTypeAttribute>();
if (string.IsNullOrWhiteSpace(propertyTypeAttribute?.Alias))
throw new InvalidOperationException(
$"Could not figure out property alias for property \"{memberExpression.Member.Name}\".");
return propertyTypeAttribute.Alias;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment