【发布时间】:2021-03-30 07:42:17
【问题描述】:
我有一个剃须刀组件库,我正在其中创建自定义的、可重用的组件。我有一个“ContentItem”组件,我想简单地在组件中绑定对象的属性,然后使用反射或其他方法来发现必要的信息。举个例子:
ContentItem.razor
<div>
<div>@DisplayName</div>
<div>@PropertyValue</div>
</div>
ContentItem.razor.cs
public partial class ContentItem
{
#region PARAMETERS
/// <summary>
/// The property that this component will bind to
/// </summary>
[Parameter]
public **???** ObjectProperty{ get; set; }
#endregion
#region PROTECTED
public string DisplayName;
public string PropertyValue;
#endregion
#region OVERRIDES
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);
}
}
#endregion
客户端应用中的页面
<div>
<ContentItem ObjectProperty="@User.FirstName" />
</div>
所以基本上你在使用“ContentItem”组件时所要做的就是传递 ObjectProperty,然后“ContentItem”组件会对该参数执行某种反射和/或检查,以根据需要呈现 HTML .
【问题讨论】:
标签: c# .net-core blazor razor-component-library