Skip to content

Instantly share code, notes, and snippets.

@SQL-MisterMagoo
Created September 6, 2020 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SQL-MisterMagoo/49c676aa24c24e2068692aa20896b578 to your computer and use it in GitHub Desktop.
Save SQL-MisterMagoo/49c676aa24c24e2068692aa20896b578 to your computer and use it in GitHub Desktop.
Provide content from a base component that is not overridden
public class MyBase : ComponentBase
{
string someValue = "test";
public MyBase()
{
var rf = typeof(ComponentBase).GetField("_renderFragment", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var pqr= typeof(ComponentBase).GetField("_hasPendingQueuedRender", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var nr= typeof(ComponentBase).GetField("_hasNeverRendered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
rf.SetValue(this, (RenderFragment)(builder =>
{
pqr.SetValue(this, false);
nr.SetValue(this, false);
builder.OpenComponent<CascadingValue<string>>(1);
builder.AddAttribute(2, "Value", someValue);
builder.AddAttribute(3, "ChildContent", (RenderFragment)( builder2 => BuildRenderTree(builder2)));
builder.CloseComponent();
}));
}
}
@SQL-MisterMagoo
Copy link
Author

This will automatically include a CascadingValue for "someValue" to all child components.

If you don't want to use reflection, you can create your own base component that implements everything ComponentBase does, then modify the constructor in a similar way, but directly referencing the fields instead of using reflection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment