Skip to content

Commit dc4cda6

Browse files
authored
FIx For IViewFor with Generic Class (#118)
1 parent e9ebdb6 commit dc4cda6

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,23 @@ The class must inherit from a UI Control from any of the following platforms and
389389
- Avalonia (Avalonia)
390390
- Uno (Windows.UI.Xaml).
391391

392+
### Usage IViewFor with ViewModel Name - Generic Types should be used with the fully qualified name, otherwise use nameof(ViewModelTypeName)
393+
```csharp
394+
using ReactiveUI.SourceGenerators;
395+
396+
[IViewFor("MyReactiveGenericClass<int>")]
397+
public partial class MyReactiveControl : UserControl
398+
{
399+
public MyReactiveControl()
400+
{
401+
InitializeComponent();
402+
MyReactiveClass = new MyReactiveClass();
403+
}
404+
}
405+
```
406+
407+
### Usage IViewFor with ViewModel Type
408+
392409
```csharp
393410
using ReactiveUI.SourceGenerators;
394411

src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ namespace ReactiveUI.SourceGenerators;
207207
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
208208
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
209209
internal sealed class IViewForAttribute<T> : Attribute;
210+
211+
/// <summary>
212+
/// IViewForAttribute.
213+
/// </summary>
214+
/// <seealso cref="System.Attribute" />
215+
/// <remarks>
216+
/// Initializes a new instance of the <see cref="IViewForAttribute"/> class.
217+
/// </remarks>
218+
/// <param name="viewModelType">Type of the view model.</param>
219+
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
220+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
221+
internal sealed class IViewForAttribute(string? viewModelType) : Attribute;
210222
#nullable restore
211223
#pragma warning restore
212224
""";

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public partial class IViewForGenerator
5050

5151
token.ThrowIfCancellationRequested();
5252

53+
var constructorArgument = attributeData.GetConstructorArguments<string>().FirstOrDefault();
5354
var genericArgument = attributeData.GetGenericType();
5455
token.ThrowIfCancellationRequested();
55-
if (!(genericArgument is string viewModelTypeName && viewModelTypeName.Length > 0))
56+
var viewModelTypeName = string.IsNullOrWhiteSpace(constructorArgument) ? genericArgument : constructorArgument;
57+
if (string.IsNullOrWhiteSpace(viewModelTypeName))
5658
{
5759
return default;
5860
}

0 commit comments

Comments
 (0)