简体   繁体   中英

c# generic method with other generic type argument

I have 2 generic classes: Db<E> , MR<T> I want to create a method which receives Db<T> and convert some fields in order to copy them or an MR<E> type. E and T are 100% different when I use it.

so my method looks like:

public static MR<E> GetMRFromDb<E>(Db<T> db) 
                where E : class 
                where T : class
{

}

When I hover the T in the where T : class I'm warned with the following error:

The type or namepsace name 'T' could not be found...

Both MR and Db are defined as generic classes.

Change your method's signature to:

public static MR<E> GetMRFromDb<T,E>(Db<T> db)
      where T : class,
      where E : class
{
      // ...
}

Please note that this will impact on how you call this method.

Instead of this:

// db is of type Db<DbUser>
MR<User> mr = GetMRFromDb<User>(db);

It should be:

// db is of type Db<DbUser>
MR<User> mr = GetMRFromDb<DbUser, User>(db);

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