Skip to content

TypeUtil.ToCSharpString(this Type) does not handle nested generic types correctly #404

@stakx

Description

@stakx

Migrated from castleproject/Core#365.


@pmg23 wrote:

The useful extension method TypeUtil.ToCSharpString(this Type) does not correctly deal with types nested within generic types [as of Core 4.2.1]. For example:

internal class A<T1>
{
    internal class B {}
    internal class C<T2> {}
}

Assert.That(typeof(A<string>.B).ToCSharpString(),
        Is.EqualTo("A<String>.B")); // FAIL: actually "A<·T1·>.B<String>"
Assert.That(typeof(A<string>.C<int>).ToCSharpString(),
        Is.EqualTo("A<String>.C<Int32>")); // FAIL: actually "A<·T1·>.C<String, Int32>"

@stakx replied:

(Shameless advertisement:)

Type name formatting is surprisingly complex because there are quite a few corner cases to deal with. I happen to have written a small library recently (stakx/TypeNameFormatter) that would solve this problem. It's available as a source code NuGet package.


@stakx replied:

That being said, I cannot seem to find a method TypeUtil.ToCSharpString(this Type) in Castle Core (this repository). Did you mean this method in Castle Windsor (castleproject/Windsor)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions