Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4021,14 +4021,23 @@ private ISet<string> GetDeclarableProperties(IEnumerable<MethodDefinition> metho
badProperty |= !allowNonConsecutiveAccessors && priorPropertyData.Index != rowIndex - 1;
if (badProperty)
{
badProperties.Add(propertyName.Identifier.ValueText);
goodProperties.Remove(propertyName.Identifier.ValueText);
ReportBadProperty();
continue;
}
}

goodProperties[propertyName.Identifier.ValueText] = (propertyType, rowIndex);
}
else if (propertyName is not null)
{
ReportBadProperty();
}

void ReportBadProperty()
{
badProperties.Add(propertyName.Identifier.ValueText);
goodProperties.Remove(propertyName.Identifier.ValueText);
}
}

return goodProperties.Count == 0 ? ImmutableHashSet<string>.Empty : new HashSet<string>(goodProperties.Keys, StringComparer.Ordinal);
Expand All @@ -4039,8 +4048,18 @@ private bool TryGetPropertyAccessorInfo(MethodDefinition methodDefinition, [NotN
propertyName = null;
accessorKind = null;
propertyType = null;

if ((methodDefinition.Attributes & MethodAttributes.SpecialName) != MethodAttributes.SpecialName)
{
// Sometimes another method actually *does* qualify as a property accessor, which would have this method as another accessor if it qualified.
// But since this doesn't qualify, produce the property name that should be disqualified as a whole since C# doesn't like seeing property X and method set_X as seperate declarations.
string disqualifiedMethodName = this.Reader.GetString(methodDefinition.Name);
int index = disqualifiedMethodName.IndexOf('_');
if (index > 0)
{
propertyName = IdentifierName(disqualifiedMethodName.Substring(index + 1));
}

return false;
}

Expand Down
13 changes: 13 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,19 @@ public void COMPropertiesAreGeneratedAsStructProperties_NonConsecutiveAccessors(
Assert.NotEmpty(structSyntax.Members.OfType<MethodDeclarationSyntax>().Where(m => m.Identifier.ValueText == "put_ClassName"));
}

/// <summary>
/// Verifies that IPicture can be generated.
/// It is a special case because of <see href="https://github.com/microsoft/win32metadata/issues/1367">this metadata bug</see>.
/// </summary>
[Fact]
public void COMPropertiesAreGeneratedAsStructProperties_NonConsecutiveAccessors_IPicture()
{
this.generator = this.CreateGenerator(DefaultTestGeneratorOptions with { AllowMarshaling = false });
Assert.True(this.generator.TryGenerate("IPicture", CancellationToken.None));
this.CollectGeneratedCode(this.generator);
this.AssertNoDiagnostics();
}

[Theory, PairwiseData]
public void COMPropertiesAreGeneratedAsMethodsWhenTheirReturnTypesDiffer([CombinatorialValues(0, 1, 2)] int marshaling)
{
Expand Down