mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-11-30 02:58:13 +08:00
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:
parent
0abc1db548
commit
4192d0c514
52
components/core/DynamicComponent/Component.cs
Normal file
52
components/core/DynamicComponent/Component.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user