0

私はジェネリッククラスを次のようにしています:

public interface IFoo<T> where T : class
{
    IList<T> GetFoo();
}

public class Foo<T> : IFoo<T> where T : class
{
    public IList<T> GetFoo()
    {
       //return something in here
    }
}

そして、次のようなアセンブリの型のコレクションからそのクラスを使用したいと思います。

public class Bar
{
    public IList<string> GetTheFoo()
    {
        IList<Type> theClass = Assembly.GetExecutingAssembly().GetTypes()
        .Where(t => t.IsClass).ToList();

        var theList = new List<string>();
        foreach (Type theType in theClass)
        {
            //not working...
            theList.Add(new Foo<theType>().GetFoo() );
        }
    }
}

しかし、コンパイラはリストから型を受け入れることができません。これを回避するにはどうすればよいですか?

4

1 に答える 1

3

Type.MakeGenericType必要なタイプを動的に作成するために使用できます。

 var item = typeof(Foo<>).MakeGenericType(theType);

これらのアイテムはすべて異なるため、保管することはできませList<object>ん。

于 2013-10-27T05:44:25.470 に答える