mercredi 15 novembre 2017

How do I specify a generic in Type.GetType()?

I am working on a VS extension that will generate documentation comments for a file. Roslyn provides classes that allow processing of source code and adding the comments. The specific problem I am having is determining the fully qualified name for a base class or interface in a class declaration. For example:

using System;
using System.Collections;
using System.Linq;

public class MyClass: IEnumerable
{ ... }

for which I want to generate:

<summary></summary>
<seealso cref="System.Collections.IEnumerable"/>

I determine that IEnumerable is defined in System.Collections by looping through each of the using directives to pick up the IdentifierName or QualifiedName and appending the interface name to generate a potential fully qualified name. I then call Type.GetType(potentialFullyQualifiedName), which returns the type if the name is valid, and null otherwise. If the returned value is not null, then I know I have the valid fully qualified name. For the example above, I first try "System.IEnumerable" which returns null. Then "System.Collections.IEnumerable" which returns a type, so I know I have the correct fully qualified name. Everything is good so far.

But if I change the code being processed to this:

using System;
using System.Collections.Generic;
using System.Linq;

public class MyClass : IEnumerable<int>
{ ... }

everything I have tried returns null. IEnumerable is in System.Collections.Generic, so I will limit further discussion to that.

None of:

System.Collections.Generic.IEnumerable
System.Collections.Generic.IEnumerable<>
System.Collections.Generic.IEnumerable<T>
System.Collections.Generic.IEnumerable<int>
System.Collections.Generic.IEnumerable<out T>

return a type from Type.GetType(). How do I specify the interface name to get Type.GetType() to return a type rather than null?





Aucun commentaire:

Enregistrer un commentaire