1

EDIT: I want to convert Type variable to its c# representation.

I recently wanted to use the Type.FullName property to create c# scripts automatically but when I tried to use Type.FullName it returns plus (+) sign instead dot (.).

Here is an example.

class Outer
{

    class Inner
    {
    }

}

Now if you want to access Inner class you should do Outer.Inner for accessing the Inner class.

Type.FullName outputs:

Outer+Inner

While it uses a dot for namespaces.

I know I can fix it by doing a simple Replace call, but it is not the only problem because of the Type.FullName products more unexcepted character and I want to fix them all.

So now, how can I convert the syntax of Type.FullName to C# Syntax?

Here is an example of an illegal syntax of Type.FullName:

System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

While it should be:

System.Collections.Generic.List<System.String>

See more examples in MSDN documentation.

Try it Online

Type.FullName Property - MSDN Documentation

Hasan Bayat
  • 926
  • 1
  • 13
  • 24
  • What other unexpected characters are there? – ediblecode Oct 05 '17 at 12:34
  • There are many of them, here is an example: `System.Collections.Generic.List'1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]`, the [, numbers and assignments, all of them are illegal syntax. While it should be `System.Collections.Generic.List` and nothing more. – Hasan Bayat Oct 05 '17 at 12:36
  • @MikeDebela My question is different from that. I am looking for a solution to display Type as a string in c# syntax. – Hasan Bayat Oct 05 '17 at 12:47
  • I think you still need all "unexpected" characters. If you want to create a scripts like T4 scripts, you need to know where to get Generic.List, as it can be defined in a few dlls – Anatoli Klamer Oct 05 '17 at 12:49
  • So, what you are trying to archive finally? – Anatoli Klamer Oct 05 '17 at 12:49
  • I want to convert Type variable to its c# representation. – Hasan Bayat Oct 05 '17 at 12:50
  • System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is c# representation = dll and namespace – Anatoli Klamer Oct 05 '17 at 12:55
  • @zxxc It is CLR representation. I need the C# representation, and the C# representation for the Generic List is: `System.Collections.Generic.List`. – Hasan Bayat Oct 05 '17 at 12:58
  • I think you can use this answer: https://stackoverflow.com/questions/16466380/get-user-friendly-name-for-generic-type-in-c-sharp#answer-16466858 – Mike Debela Oct 05 '17 at 13:40
  • @MikeDebela Yes, this is my answer, thanks for sharing. – Hasan Bayat Oct 13 '17 at 10:11

0 Answers0