20

I have a database table where I store the height, width, state, et cetera, of windows. As identifier for the windows I use the full type name of form. It works well, but I discovered that some forms that are generic gets names which are incredibly long. The reason is that the generic type is listed with full assembly information. Is there a way to skip that?

For example the full name of a regular form would look like this:

Some.Name.Space.NameOfForm

But the full name of a generic form looks like this:

Some.Name.Space.NameOfForm`1[[Some.Other.Name.Space.GenericType, AssemblyName, Version=1.0.2.0, Cuntulre=neutral, PublicKeyToken=null]]

Why does it get so long? Is there a way I can get a shorter version? For example something like:

Some.Name.Space.NameOfForm`1[[Some.Other.Name.Space.GenericType]]

Any clues?

svick
  • 236,525
  • 50
  • 385
  • 514
Svish
  • 152,914
  • 173
  • 462
  • 620

2 Answers2

28

I agree with dbemerlin in that your end goal seems odd, but I just wanted to point out that

GetType().ToString();

seems to return a shorter version of the typename (w/o the assembly information of the generic arguments).

I guess this could be handy for a more human readable version of the type name.

jnm2
  • 7,960
  • 5
  • 61
  • 99
b8adamson
  • 317
  • 3
  • 6
  • It's not weird at all. This is exactly what I needed for a problem that was causing a massive amount of issues. If you try to send data to a mono framework project the assembly information doesn't match with .net core. – Daniel Tranca Aug 04 '20 at 22:30
  • I find it very weird that MS did not expose this through a property too, but it works, so I am happy! :) – user2173353 Dec 28 '20 at 17:24
2

I guess the Type used for the generic class depends on a specific assembly (as do most of the .NET classes) so i don't think you will get anything else without creating this string manually by reflection or string parsing.

OTOH i don't think using type names as keys is a good idea, maybe you should think about getting some other key (but i do not know the requirements, so i cannot recommend something else)