Skip to content

Commit bdff314

Browse files
authored
Merge branch 'main' into CP_FixTargetInfoToAllowGenericTypes
2 parents ace3c58 + dc4cda6 commit bdff314

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
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: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ 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
}
5961

60-
var compilation = context.SemanticModel.Compilation;
61-
var semanticModel = compilation.GetSemanticModel(context.SemanticModel.SyntaxTree);
62-
token.ThrowIfCancellationRequested();
63-
attributeData.GatherForwardedAttributesFromClass(semanticModel, declaredClass, token, out var classAttributesInfo);
64-
var forwardedClassAttributes = classAttributesInfo.Select(static a => a.ToString())
65-
.Where(x => !x.Contains(AttributeDefinitions.IViewForAttributeType))
66-
.ToImmutableArray();
6762
token.ThrowIfCancellationRequested();
6863

6964
var viewForBaseType = IViewForBaseType.None;
@@ -105,14 +100,13 @@ public partial class IViewForGenerator
105100
targetInfo.TargetVisibility,
106101
targetInfo.TargetType,
107102
viewModelTypeName!,
108-
viewForBaseType,
109-
forwardedClassAttributes);
103+
viewForBaseType);
110104
}
111105

112106
private static string GenerateSource(string containingTypeName, string containingNamespace, string containingClassVisibility, string containingType, IViewForInfo iviewForInfo)
113107
{
114108
// Prepare any forwarded property attributes
115-
var forwardedAttributesString = string.Join("\n ", excludeFromCodeCoverage.Concat(iviewForInfo.ForwardedAttributes));
109+
var forwardedAttributesString = string.Join("\n ", excludeFromCodeCoverage);
116110

117111
switch (iviewForInfo.BaseType)
118112
{

src/ReactiveUI.SourceGenerators/IViewFor/Models/IViewForInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ internal sealed record IViewForInfo(
1818
string TargetVisibility,
1919
string TargetType,
2020
string ViewModelTypeName,
21-
IViewForBaseType BaseType,
22-
EquatableArray<string> ForwardedAttributes);
21+
IViewForBaseType BaseType);

0 commit comments

Comments
 (0)