Extend Blazor for source generator to perform service injection instead of built-in reflection #54857
Closed
1 task done
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Duplicate
Resolved as a duplicate of another issue
Status: Resolved
Is there an existing issue for this?
Is your feature request related to a problem? Please describe the problem.
No response
Describe the solution you'd like
We use blazor webassembly for an application and we are in the process of optimizing things, especially startup time (after resouces are downloaded).
Blazor typically uses reflection internally to supply properties to component from parent and also inject services.
We have used a source generator to generate an override for
SetParametersAsync
for all components in our application and this gives us a better startup time that Blazor would rather use to create reflection basedPropertySetter
for our components.We have also implemented our own
IComponentActivator
and it uses a source generator to create new instance of components on request instead of the built--inActivator.CreateInstance
.But we want more. Blazor also uses reflection to supply services to component properties decorated with
InjectAttribute
and we have a source generator that is part of our component activator that does this now.Now that all our component services are supplied to it by its generated module instantiator, there seems to be no way to tell blazor runtime to skip Injecting the properties again via its built-in reflection scheme.
Looking at the ComponentFactory source code, we can see that the reflection work starts here with
MemberAssignment.GetPropertiesIncludingInherited(type, _injectablePropertyBindingFlags)
and futher insidePropertySetter.cs
Much like one can skip using reflection on SetParametersAsync by not calling the base.SetParametersAsync after manually setting properties from ParameterView, we need something like this for InjectedServices.
We've thought of creating our own attribute and use this instead of
InjectAttribute
, so our source generator can use that to perform the injection, this will eliminate creating instances ofPropertySetter
, but that wont eliminate the call toMemberAssignment.GetPropertiesIncludingInherited(type, _injectablePropertyBindingFlags)
inCreatePropertyInjector
Additional context
No response
The text was updated successfully, but these errors were encountered: