Skip to content

How to create a component dynamically through its assembly type path and reflection? #26111

Discussion options

You must be logged in to vote

You shouldn't instantiate the component yourself. You need the renderer to instantiate it for you, because then it becomes part of the component hierarchy, gets wired into all the re-rendering logic, and has its lifecycle managed by the framework.

One way to do this is create a class called RenderingHelpers containing:

public static class RenderingHelpers
{
    public static readonly Func<Type, IDictionary<string, object>, RenderFragment> RenderComponent = (componentType, parameters) => builder =>
    {
        builder.OpenComponent(0, componentType);

        if (parameters != null)
        {
            foreach (var kvp in parameters)
            {
                builder.AddAttribute(1, 

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by SteveSandersonMS
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@CuteLeon
Comment options

@SteveSandersonMS
Comment options

@CuteLeon
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants