Skip to content

SourceGen MauiXamlInflator using wrong type when working with Enum and extension class #34021

@tschramme86

Description

@tschramme86

Description

Having an Enum and a static extension class for this enum. When now trying to use that Enum in Xaml and the name of the extension class is [NameOfEnum]Extension, SourceGen will generate code that references the extension class accidentally instead of the Enum itself, which won't compile.

Steps to Reproduce

  1. Create new MAUI project from template and add a file with an enum definition + a static extension class for it.
    public enum MyEnum
    {
        Value1,
        Value2,
        Value3
    }

    // Rename to any name except [NameOfEnum]Extension and the issue is resolved.
    internal static class MyEnumExtension
    {
        public static string ToFriendlyString(this MyEnum value)
        {
        }
    }
  1. Create a random typed class with a property that accepts enums
    internal class MyDataObject<T> where T: Enum
    {
        public T? EnumValue { get; set; }
    }
  1. Use the MyDataObject with MyEnum somewhere in Xaml, e.g. as a resource:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:enums="clr-namespace:MauiXamlInflatorBug.Enums"
             xmlns:app="clr-namespace:MauiXamlInflatorBug"
             x:Class="MauiXamlInflatorBug.MainPage">

    <ContentPage.Resources>
        <ResourceDictionary>
            <app:MyDataObject x:Key="MyDataObject" EnumValue="{x:Static enums:MyEnum.Value1}" x:TypeArguments="enums:MyEnum" />
        </ResourceDictionary>
    </ContentPage.Resources>
  1. Ensure MauiXamlInflator is set to SourceGen in the project file
  2. The project won't build. Check the generated file for MainPage and see that it uses the name of the extension class there instead of the enum itself
    var myDataObject = new global::MauiXamlInflatorBug.MyDataObject<global::MauiXamlInflatorBug.Enums.MyEnumExtension>();
    ...
    myDataObject.EnumValue = (global::MauiXamlInflatorBug.Enums.MyEnumExtension)object0;
  1. Rename the extension class MyEnumExtension to something else, e.g. MyCoolEnumExtension or even MyEnumExtensions
  2. Build the project again. Now it succeeds. Check the generated code and see that it now uses the Enum correctly
    var myDataObject = new global::MauiXamlInflatorBug.MyDataObject<global::MauiXamlInflatorBug.Enums.MyEnum>();
    ....
    myDataObject.EnumValue = (global::MauiXamlInflatorBug.Enums.MyEnum)staticExtension;

Link to public reproduction project repository

https://github.com/tschramme86/MauiXamlInflatorBug

Version with bug

10.0.30

Is this a regression from previous behavior?

No, this is something new, Not sure, did not test other versions

Last version that worked well

No response

Affected platforms

I was not able test on other platforms

Affected platform versions

Android

Did you find any workaround?

Renaming the extension class as described above.

Relevant log output

Compiler output for the case described above:


1>CSC : error MAUIG1001: An error occured while parsing Xaml: Member not found.
1>D:\Source\MauiXamlInflatorBug\obj\Debug\net10.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\MainPage.xaml.xsg.cs(21,67,21,116): error CS0718: 'MyEnumExtension': static types cannot be used as type arguments
1>D:\Source\MauiXamlInflatorBug\obj\Debug\net10.0-android\Microsoft.Maui.Controls.SourceGen\Microsoft.Maui.Controls.SourceGen.CodeBehindGenerator\MainPage.xaml(10,28,10,86): error CS0716: Cannot convert to static type 'MyEnumExtension'

Metadata

Metadata

Assignees

No one assigned

    Labels

    partner/syncfusionIssues / PR's with Syncfusion collaborationplatform/androids/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't workingxsgXaml sourceGen

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions