I have a razor component library where I'm creating custom, reusable components. I have a "ContentItem" component that I would like to simply bind the property of an object in the component and then use reflection or some other method to discover the necessary information. As an example:
ContentItem.razor
<div>
<div>@DisplayName</div>
<div>@PropertyValue</div>
</div>
ContentItem.razor.cs
public partial class ContentItem
{
/// <summary>
/// The property that this component will bind to
/// </summary>
[Parameter]
public **???** ObjectProperty{ get; set; }
public string DisplayName;
public string PropertyValue;
protected override void OnParametersSet()
{
try
{
DisplayName = //reflection or some way to get the display attribute from the Object Property
PropertyValue = //reflection or inspection of the ObjectProperty
base.OnParametersSet();
}
catch (Exception ex)
{
throw new exception("Error", ex);
}
}
Page in the client app
<div>
<ContentItem ObjectProperty="@User.FirstName" />
</div>
So basically all you would have to do when using the "ContentItem" component would be to pass the ObjectProperty and then the "ContentItem" component would perform some sort of reflection and/or inspection of that parameter to render the HTML as desired.
