Skip to content

Commit c14b192

Browse files
authored
Fix Make IViewFor use Generic type (#61)
1 parent 49bf713 commit c14b192

File tree

8 files changed

+24
-10
lines changed

8 files changed

+24
-10
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ public partial class MyReactiveClass
304304

305305
### IViewFor usage
306306

307-
IVIewFor is used to link a View to a ViewModel, this is used to link the ViewModel to the View in a way that ReactiveUI can use it to bind the ViewModel to the View.
308-
The ViewModel is passed as a string to the IViewFor Attribute.
307+
IViewFor is used to link a View to a ViewModel, this is used to link the ViewModel to the View in a way that ReactiveUI can use it to bind the ViewModel to the View.
308+
The ViewModel is passed as a type to the IViewFor Attribute using generics.
309309
The class must inherit from a UI Control from any of the following platforms and namespaces:
310310
- Maui (Microsoft.Maui)
311311
- WinUI (Microsoft.UI.Xaml)
@@ -317,7 +317,7 @@ The class must inherit from a UI Control from any of the following platforms and
317317
```csharp
318318
using ReactiveUI.SourceGenerators;
319319

320-
[IViewFor(nameof(MyReactiveClass))]
320+
[IViewFor<MyReactiveClass>]
321321
public partial class MyReactiveControl : UserControl
322322
{
323323
public MyReactiveControl()

src/ReactiveUI.SourceGenerators.Execute.Maui/IViewForTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace SGReactiveUI.SourceGenerators.Test.Maui
1111
/// IViewForTest.
1212
/// </summary>
1313
/// <seealso cref="NavigationPage" />
14-
[IViewFor(nameof(TestViewModel))]
14+
[IViewFor<TestViewModel>]
1515
public partial class IViewForTest : Shell;
1616
}

src/ReactiveUI.SourceGenerators.Execute/TestViewWinForms.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SGReactiveUI.SourceGenerators.Test
1111
/// TestViewWinForms.
1212
/// </summary>
1313
/// <seealso cref="System.Windows.Forms.Form" />
14-
[IViewFor(nameof(TestViewModel))]
14+
[IViewFor<TestViewModel>]
1515
public partial class TestViewWinForms : Form
1616
{
1717
/// <summary>

src/ReactiveUI.SourceGenerators.Execute/TestViewWpf.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace SGReactiveUI.SourceGenerators.Test;
1212
/// <summary>
1313
/// TestView.
1414
/// </summary>
15-
[IViewFor(nameof(TestViewModel))]
15+
[IViewFor<TestViewModel>]
1616
public partial class TestViewWpf : Window
1717
{
1818
/// <summary>

src/ReactiveUI.SourceGenerators/Core/Extensions/ITypeSymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string
208208

209209
symbol.AppendFullyQualifiedMetadataName(builder);
210210

211-
return builder.WrittenSpan.SequenceEqual(name.AsSpan());
211+
return builder.WrittenSpan.StartsWith(name.AsSpan());
212212
}
213213

214214
public static bool ContainsFullyQualifiedMetadataName(this ITypeSymbol symbol, string name)

src/ReactiveUI.SourceGenerators/Core/Helpers/AttributeDefinitions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ namespace ReactiveUI.SourceGenerators;
153153
/// <param name="viewModelType">Type of the view model.</param>
154154
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
155155
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
156-
internal sealed class IViewForAttribute(string? viewModelType) : Attribute;
156+
internal sealed class IViewForAttribute<T> : Attribute;
157157
#nullable restore
158158
#pragma warning restore
159159
""";

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The .NET Foundation licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System;
67
using System.CodeDom.Compiler;
78
using System.Collections.Immutable;
89
using System.IO;
@@ -57,8 +58,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5758
var classSymbol = symbol as INamedTypeSymbol;
5859
var classNamespace = classSymbol?.ContainingNamespace.ToString();
5960
var className = declaredClass.Identifier.ValueText;
60-
var constructorArgument = attributeData.GetConstructorArguments<string>().First();
61-
if (constructorArgument is string viewModelTypeName)
61+
token.ThrowIfCancellationRequested();
62+
63+
var genericArgument = GetGenericType(attributeData);
64+
token.ThrowIfCancellationRequested();
65+
if (genericArgument is string viewModelTypeName && viewModelTypeName.Length > 0)
6266
{
6367
token.ThrowIfCancellationRequested();
6468
GatherForwardedAttributes(attributeData, semanticModel, declaredClass, token, out var classAttributesInfo);
@@ -193,4 +197,11 @@ static void GatherForwardedAttributes(
193197

194198
classAttributesInfo = classAttributesInfoBuilder.ToImmutable();
195199
}
200+
201+
private static string? GetGenericType(AttributeData attributeData)
202+
{
203+
var success = attributeData?.AttributeClass?.ToDisplayString();
204+
var start = success?.IndexOf('<') + 1 ?? 0;
205+
return success?.Substring(start, success.Length - start - 1);
206+
}
196207
}

src/ReactiveUI.SourceGenerators/ReactiveUI.SourceGenerators.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
4242
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
4343
</ItemGroup>
44+
<ItemGroup>
45+
<None Remove="bin\Debug\netstandard2.0\\ReactiveUI.SourceGenerators.dll" />
46+
</ItemGroup>
4447

4548
<!-- This ensures the library will be packaged as a source generator when we use `dotnet pack` -->
4649
<ItemGroup>

0 commit comments

Comments
 (0)