2011-09-17 9 views
1

DataAccessのサブクラスであるProgramEというクラスがあります。呼び出し元クラスの型を返すジェネリックメソッドを作るにはどうすればよいですか?メソッドの戻り値の型を変更するベースクラス

これはDataAccessクラスのコードです。

public MongoCursor<DataAccess> GetAll(QueryComplete query) 
    { 
     MongoServer server = MongoServer.Create(C.connectionString); 
     MongoDatabase db = server.GetDatabase(C.database); 
     MongoCollection<DataAccess> collection = db.GetCollection<DataAccess>(_collectionName); 
     var result = collection.FindAs<DataAccess>(query); 

     return result; 
    } 

ここに私がやろうとしていることがあります。私はこのエラーが発生します。 「暗黙的にタイプを変換できません 『System.Collections.Generic.List』 『System.Collections.Generic.List』に」

 ProgramE p = new ProgramE(); 
     QueryComplete query = Query.EQ("InstalledOn.SystemID", audit.SystemID); 

     List<ProgramE> ServerPrograms = p.GetAll(query).ToList(); 

答えて

1

でしょうあなたのためにその仕事のようなものを?私がする必要があるだろうが、「p.GetAll 」のような何かをしたし、代わりにp.GetAllを使用せずにそれを行うにはとにかくがあるものを達成ん

public MongoCursor<T> GetAll<T>(QueryComplete query) where T : DataAccess { 
    MongoServer server = MongoServer.Create(C.connectionString); 
    MongoDatabase db = server.GetDatabase(C.database); 
    MongoCollection<T> collection = db.GetCollection<T>(_collectionName); 
    return collection.FindAs<T>(query); 
} 

List<ProgramE> ServerPrograms = p.GetAll<ProgramE>(query).ToList(); 
+0

? – MattAitchison