@@ -21,6 +21,16 @@ public class MvvmComponentGenerator : ISourceGenerator
2121 true
2222 ) ;
2323
24+ private static readonly DiagnosticDescriptor ComponentWrongTypeParameterError = new (
25+ "MVVMBLAZOR005" ,
26+ "Wrong type parameter" ,
27+ "Mvvm Component class '{0}' needs to have exactly one type parameter named '{1}'" ,
28+ "MvvmBlazorGenerator" ,
29+ DiagnosticSeverity . Error ,
30+ true
31+ ) ;
32+
33+
2434 public void Execute ( GeneratorExecutionContext context )
2535 {
2636 if ( context . SyntaxContextReceiver is not MvvmComponentSyntaxReceiver syntaxReceiver ||
@@ -100,14 +110,26 @@ private static void AddGenericComponent(
100110 MvvmComponentClassContext componentClassContext ,
101111 TypeDeclarationSyntax componentClass )
102112 {
113+ const string typeParameterName = "T" ;
103114 var genericComponentSourceText = SourceText . From (
104- GenerateGenericComponentCode ( componentClassContext ) ,
115+ GenerateGenericComponentCode ( componentClassContext , typeParameterName ) ,
105116 Encoding . UTF8
106117 ) ;
107118
108- if ( componentClass . TypeParameterList is null || componentClass . TypeParameterList . Parameters . Count != 1 )
119+ var typeParameterList = componentClass . TypeParameterList ;
120+ if ( typeParameterList is null || typeParameterList . Parameters . Count != 1 || typeParameterList . Parameters [ 0 ] . Identifier . ValueText != typeParameterName )
109121 {
110- throw new InvalidOperationException ( "Expected exactly one type parameter" ) ;
122+ context . ReportDiagnostic (
123+ Diagnostic . Create (
124+ ComponentWrongTypeParameterError ,
125+ Location . Create (
126+ componentClass . SyntaxTree ,
127+ TextSpan . FromBounds ( componentClass . SpanStart , componentClass . SpanStart )
128+ ) ,
129+ componentClass . Identifier ,
130+ typeParameterName
131+ )
132+ ) ;
111133 }
112134
113135 context . AddSource ( componentClass . Identifier + "T.Generated.cs" , genericComponentSourceText ) ;
@@ -137,7 +159,7 @@ partial class {componentClassName} : IDisposable
137159 [Inject] protected IServiceProvider RootServiceProvider {{ get; set; }} = default!;
138160 protected bool IsDisposed {{ get; private set; }}
139161
140- public IBinder Binder {{ get; private set; }} = null!;
162+ public MvvmBlazor.Internal.Bindings. IBinder Binder {{ get; private set; }} = null!;
141163
142164#pragma warning disable CS0109
143165 protected new IServiceProvider ScopedServices
@@ -177,7 +199,7 @@ partial class {componentClassName} : IDisposable
177199
178200 private void InitializeDependencies()
179201 {{
180- Binder = ScopedServices.GetRequiredService<IBinder>();
202+ Binder = ScopedServices.GetRequiredService<MvvmBlazor.Internal.Bindings. IBinder>();
181203 Binder.ValueChangedCallback = BindingOnBindingValueChanged;
182204 }}
183205
@@ -224,7 +246,7 @@ protected virtual void Dispose(bool disposing)
224246 " ;
225247 }
226248
227- private static string GenerateGenericComponentCode ( MvvmComponentClassContext componentClassContext )
249+ private static string GenerateGenericComponentCode ( MvvmComponentClassContext componentClassContext , string typeParameterName )
228250 {
229251 var componentNamespace = componentClassContext . ComponentSymbol . ContainingNamespace ;
230252 var componentClassName = componentClassContext . ComponentClass . Identifier ;
@@ -241,10 +263,10 @@ private static string GenerateGenericComponentCode(MvvmComponentClassContext com
241263
242264namespace { componentNamespace }
243265{{
244- partial class { componentClassName } <T >
266+ partial class { componentClassName } <{ typeParameterName } >
245267 where T : ViewModelBase
246268 {{
247- private IViewModelParameterSetter? _viewModelParameterSetter;
269+ private MvvmBlazor.Internal.Parameters. IViewModelParameterSetter? _viewModelParameterSetter;
248270
249271#pragma warning disable CS8618
250272 protected internal { componentClassName } (IServiceProvider serviceProvider) : base(serviceProvider)
@@ -275,7 +297,7 @@ private void SetParameters()
275297 if (BindingContext is null)
276298 throw new InvalidOperationException($""{{nameof(BindingContext)}} is not set"");
277299
278- _viewModelParameterSetter ??= ScopedServices.GetRequiredService<IViewModelParameterSetter>();
300+ _viewModelParameterSetter ??= ScopedServices.GetRequiredService<MvvmBlazor.Internal.Parameters. IViewModelParameterSetter>();
279301 _viewModelParameterSetter.ResolveAndSet(this, BindingContext);
280302 }}
281303
0 commit comments