feat: add dynamic component (#1703)

* feat: weakly-typed/dynamic component

* add TypeName support

* override BuildRenderTree instead of using a RenderFragment

* rename
This commit is contained in:
anranruye 2021-09-10 12:14:43 +08:00 committed by GitHub
parent 0abc1db548
commit 4192d0c514

View File

@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
namespace AntDesign
{
public class Component : ComponentBase
{
static Assembly _antAssembly;
[Parameter]
public Type Type { get; set; }
[Parameter]
public string TypeName
{
set
{
if (Type != null) return;
_antAssembly ??= Assembly.GetExecutingAssembly();
Type componentType =
_antAssembly.GetType($"AntDesign.{value}") ??
_antAssembly.GetType(value) ??
Type.GetType(value);
if (componentType == null)
{
throw new ArgumentException($"Not found the component with the name \"{value}\"", nameof(TypeName));
}
Type = componentType;
}
}
[Parameter(CaptureUnmatchedValues = true)]
public IDictionary<string, object> Parameters { get; set; }
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.OpenComponent(0, Type);
if (Parameters != null)
{
builder.AddMultipleAttributes(1, Parameters);
}
builder.CloseComponent();
}
}
}