简体   繁体   中英

Instantiating a generic class where type argument comes from a string

First I have to reference Jon Skeet's answer to a similar question

https://stackoverflow.com/a/266282/1184296

In his answer he wrote this

Type genericClass = typeof(Generic<>);

What I don't understand here is how to get Generic<> to show up.

I tried adding using s with all the namespaces under System but no luck. Nothing shows up in IntelliSense and Generic<> stays underlined in red stating "The type or namespace 'Generic' could not be found...".

Does anyone knows how to solve this?

Thanks.

If you look at the code in the Post, John Skeet has created a custom class ..

public class Generic<T>
{
    public Generic()
    {
        Console.WriteLine("T={0}", typeof(T));
    }
}

So, you could do the same, you might need John Skeets permission to take that code though? :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM