简体   繁体   English

如何调用返回接口的方法

[英]How to invoke a method which returns an interface

I am invoking a method on a type via reflection which takes a couple of parameters: 我通过反射调用一个类型的方法,它采用了几个参数:

var myType = typeof(myClass);

var myMethod = myType.GetMethod("myMethodInClass", 
                                 new[] { typeof(string), typeof(string) });

myMethod.Invoke(?, new object[] { "", "" });

I want the target to be an IDataReader, which is what the method will return, but I obviously cannot instantiate a new instance of an interface. 我希望目标是一个IDataReader,这是该方法将返回的,但我显然无法实例化一个新的接口实例。

You cannot return interface, but you can return an instance of class which implement the interface your method returns. 您不能返回接口,但可以返回实现您的方法返回的接口的类实例。 Just cast it. 只是施展它。

IDataReader implemented = new YourClass(); // or any other constructor

Your class must only implement IDataReader interface. 您的类只能实现IDataReader接口。 You can insert your class instance in the place of ? 你可以在你的位置插入你的类实例? and implemented might be result of myMethod.Invoke(yourClassInstance, new object[] { "", "" }) . implemented可能的结果myMethod.Invoke(yourClassInstance, new object[] { "", "" })

myMethod.Invoke(?, new object[] { "", "" });

? has nothing with returning interface, but it's the actual object of which's method you are calling. 返回接口没有任何内容,但它是您调用的方法的实际对象。 If you know that this method returns class which implements IDataReader just write 如果你知道这个方法返回实现IDataReader类只是写

IDataReader rd=myMethod.Invoke(yourInstance, new object[] { "", "" });.

Where you have put ? 你放在哪里? in the the question should not be IDataReader , but an instance of myClass . 在问题中不应该是IDataReader ,而是myClass一个实例。 You are passing in the object on which you want to invoke myMethod . 您正在传入要调用myMethod的对象。

The result from calling .Invoke() will be an IDataReader , but that is not something you are creating; 调用.Invoke()的结果将是一个IDataReader ,但这不是你要创建的东西; it is created inside the method you are invoking. 它是在您调用的方法内创建的。

你不能返回接口类型实例。但是你可以显式地转换为接口类型并且可以解决这个问题。

((myMethod.Invoke . . . ) as InterfaceType) 

Just the way you think you would: 就像你认为的那样:

class Gadget
{
  public IList<int> FizzBuzz( int length , int startValue )
  {
    if ( length < 0 ) throw new ArgumentOutOfRangeException("length") ;
    int[] list = new int[length];
    for ( int i = 0 ; i < list.Length ; ++i )
    {
      list[i] = startValue++ ;
    }
    return list ;
  }
}
class Program
{
  static void Main( string[] args )
  {
    object     x             = new Gadget() ;
    Type       t             = x.GetType() ;
    MethodInfo mi            = t.GetMethod("FizzBuzz") ;
    object     returnedValue = mi.Invoke( x , new object[]{ 10 , 101 } ) ;
    IList<int> returnedList  = (IList<int>) returnedValue ;
    string     msg           = returnedList.Select( n => n.ToString(CultureInfo.InvariantCulture)).Aggregate( (s,v) => string.Format("{0}...{1}" , s , v) ) ;
    Console.WriteLine(msg) ;
    return;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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