2015-11-20 1 views
1

В следующем коде, я хотел бы вызвать метод, который я объявил в производном классе:новообращенный/литой базовый тип для производного родового типа

class BaseClass 
{ 
    // ... 
} 
class A<T> : BaseClass 
{ 
    public void F(T args){ //... } 
} 
class B<T> : BaseClass 
{ 
    // B<T> doesn't have method F() 
} 
///.... 
class myApplication 
{ 
    // ... 
    public void DoSomething(BaseClass arg) 
    { 
     // Now I know that arg is of type A<T> for some type T 
     // but I don't know what T is. Also declaring DoSomething 
     // as DoSomething<T>() is not an option. 
     // 
     // I would like to call (arg as A<T>).F(...) but how can I 
     // deduce T? Can it be done in any other way? 
    } 
} 

Пожалуйста, прочтите комментарий в коде. Как я могу сделать что-то подобное?

+0

have u попытался 'class myApplication '? –

+1

Предположительно у вас есть объект, который вы собираетесь передать в F, так что вы не знаете его тип? –

ответ

2

Чтобы вызвать метод, вы можете запустить следующий код:

class myApplication 
{ 
    // ... 
    public void DoSomething(BaseClass arg) 
    { 
     var type = arg.GetType(); 
     // Check whether a generic type was passed 
     if (type.IsGenericType) 
     { 
      var genType = type.GetGenericTypeDefinition(); 
      // Check whether it is of type A<> 
      if (genType == typeof(A<>)) 
      { 
       // Get generic argument type 
       var genArg = type.GenericTypeArguments[0]; 
       // Create a default instance; might not work under all circumstances 
       // Better to get the method parameter in another way 
       var mthArg = Activator.CreateInstance(genArg); 
       // Get method that is to be called 
       var mth = type.GetMethod("F"); 
       // Invoke method dynamically 
       mth.Invoke(arg, new object[] { mthArg }); 
      } 
     } 
    } 
} 

Пожалуйста, обратите внимание, что очень важно, чтобы передать аргумент типа T методу F. Вы должны подготовить для этого значение. В моем примере я добавил вызов Activator.CreateInstance, который требует, чтобы T имел стандартный конструктор по умолчанию (я использовал int для тестирования).

+0

Ницца! Спасибо! – rashmatash

Смежные вопросы