eiriktsarpalis / PolyType

Practical generic programming for .NET
https://eiriktsarpalis.github.io/PolyType/
MIT License
154 stars 8 forks source link

CS0436 (duplicate type) when using InternalsVisibleTo and project references #64

Open joelverhagen opened 1 week ago

joelverhagen commented 1 week ago

Suppose project ConsoleApp2 depends on project ConsoleApp1 Project ConsoleApp1 depends on PolyType. Project ConsoleApp1 has [assembly:InternalsVisibleTo("ConsoleApp2")].

This means the following type is duplicate in ConsoleApp2's build:

namespace PolyType.SourceGenerator
{
    /// <summary>The source generated <see cref="global::PolyType.ITypeShapeProvider"/> implementation for the current assembly.</summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("PolyType.SourceGenerator.PolyTypeGenerator", "0.18.1+5e98a961a4")]
    internal sealed partial class ShapeProvider
    {
        private const global::System.Reflection.BindingFlags __BindingFlags_Instance_All = 
            global::System.Reflection.BindingFlags.Public | 
            global::System.Reflection.BindingFlags.NonPublic | 
            global::System.Reflection.BindingFlags.Instance;

        /// <summary>Gets the default instance of the <see cref="ShapeProvider"/> class.</summary>
        public static ShapeProvider Default { get; } = new();

        /// <summary>Initializes a new instance of the <see cref="ShapeProvider"/> class.</summary>
        private ShapeProvider() { }
    }
}

Love the look of the project BTW! I have been building my own CSV and Kusto source generator from POCO for a while. This might be a fun project to simplify with.

eiriktsarpalis commented 1 week ago

Great to hear that you like it!

What solution would you recommend? It is conceivable that the generator could just suffix the class name with an identifier deriving from the name of the assembly, but I'm wondering if there's a better way.

joelverhagen commented 1 week ago

I can think of three options:

  1. Disambiguate the name automatically or by configuration (as you stated)
  2. Add a hook to allow assembly aliasing. Seems like a pain and may not even work for project reference (never tried).
  3. Not sure of feasibility -- but could ConsoleApp2 re-use ConsoleApp1's implementation?

I am not sure if existing users of the library expect the shape provider to be called ShapeProvider. If so, option 1 would need to be opt-in (or enabled only when InternalsVisibleTo is detected).