C# 如何将对象属性作为参数传递给Razor组件

C# 如何将对象属性作为参数传递给Razor组件,c#,.net-core,blazor,razor-component-library,C#,.net Core,Blazor,Razor Component Library,我有一个razor组件库,我在其中创建自定义的、可重用的组件。我有一个“ContentItem”组件,我想简单地绑定组件中对象的属性,然后使用反射或其他方法来发现必要的信息。例如: ContentItem.razor <div> <div>@DisplayName</div> <div>@PropertyValue</div> </div> @显示名 @财产价值 ContentI

我有一个razor组件库,我在其中创建自定义的、可重用的组件。我有一个“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
公共部分类ContentItem
{
#区域参数
/// 
///此组件将绑定到的属性
/// 
[参数]
公共**???**对象属性{get;set;}
#端区
#区域保护
公共字符串显示名;
公共字符串PropertyValue;
#端区
#区域覆盖
受保护的覆盖无效OnParametersSet()
{
尝试
{
DisplayName=//反射或从对象属性获取显示属性的某种方法
PropertyValue=//对ObjectProperty的反射或检查
base.OnParametersSet();
}
捕获(例外情况除外)
{
抛出新异常(“错误”,ex);
}
} 
#端区
客户端应用程序中的页面

<div>
  <ContentItem ObjectProperty="@User.FirstName" />
</div>  


因此,使用“ContentItem”组件时,基本上只需传递ObjectProperty,然后传递“ContentItem”组件将对该参数执行某种反射和/或检查,以根据需要呈现HTML。

您需要将类的类型、属性和值分别传递给组件

类型将是
typeof(User)
,属性名称可以从
nameof(User.FirstName)
派生,值将是
User的任何值。FirstName
值被保存为
string
或任何值

ContentItem
组件中的参数如下所示:

[参数]
公共类型ObjectType{get;set;}
[参数]
公共字符串ObjectProperty{get;set;}
[参数]
公共字符串ObjectValue{get;set;}
可以这样称呼:


假设您的类是这样的:

公共类用户
{
[显示名称(“名字”)]
公共字符串名{get;set;}
}
之后,在组件中使用下面的帮助器方法获取
DisplayName

公共静态字符串GetDisplayName(Type@Type,string propertyName)
{
var memberInfo=@type?.GetMember(propertyName)[0];
var displayNameAttribute=memberInfo?.GetCustomAttribute();
字符串displayName=displaynamattribute?.displayName???”;
返回字符串.IsNullOrEmpty(displayName)?propertyName:displayName;
}

我最终用级联值/参数解决了这个问题。我有一个容器组件,它公开了一个名为“BindObject”的级联值

现在,我能够以最小的工作量显示模型中的信息。这对于模板、动作卡等非常有用

<CascadingValue Name="BindObject" Value="@BindObject">
@if (BindObject != null)
{
    @ChildContent
}
else
{
    if (PlaceholderLines > 0)
    {
        <Placeholder DisplayLines="@PlaceholderLines"></Placeholder>
    }
}
</CascadingValue>
    /// <summary>
    /// When the ContentItem is placed inside of a ContentItems
    /// container then the BindObject will be passed as a Cascading
    /// Parameter.  This ContentItem will then use reflection to
    /// populate the Label and Value of this control.
    /// </summary>
    [CascadingParameter(Name = "BindObject")]
    public object BindObject { get; set; }

    /// <summary>
    /// If a BindProperty is specified then a generic placeholder
    /// will be used while the property is being loaded or evaluated.
    /// The BindProperty only works when the ContentItem is placed
    /// inside of a ContentItems control.
    /// </summary>
    [Parameter]
    public string BindProperty { get; set; }
        <ContentItems @ref="requestItems" BindObject="@jsonObject">
            <ContentItem BindProperty="@nameof(jsonObject.UserId)" />
            <ContentItem BindProperty="@nameof(jsonObject.FirstName)" />
            <ContentItem BindProperty="@nameof(jsonObject.LastName)" />
            <ContentItem BindProperty="@nameof(jsonObject.Email)" />
            <ContentItem BindProperty="@nameof(jsonObject.RegionId)" />
            <ContentItem BindProperty="@nameof(jsonObject.Password)" />
            <ContentItem BindProperty="@nameof(jsonObject.Id)" />
        </ContentItems>
    [Required]
    [DisplayName("First Name:")]
    [KzAlphaOnly(ErrorMessage = "Only letters are allowed here.")]
    [MaxLength(16, ErrorMessage = "First name must be 16 characters or less.")]
    [KzIcon(FaIcon = FaIcons.User)]
    public string FirstName { get; set; }

    [Required]
    [KzAlphaOnly(ErrorMessage = "Only letters are allowed here.")]
    [MaxLength(64, ErrorMessage = "Last Name name must be 64 characters or less.")]
    [KzIcon(FaIcon = FaIcons.User)]
    [DisplayName("Last Name:")]
    public string LastName { get; set; }

    [Required]
    [StringLength(256)]
    [DisplayName("Email:")]
    [KzEmail]
    [KzIcon(FaIcon = FaIcons.EnvelopeSquare)]
    public string Email { get; set; }