我怎样才能调用一个out参数的方法?
我想要公开WebClient.DownloadDataInternal方法如下所示:
[ComVisible(true)] public class MyWebClient : WebClient { private MethodInfo _DownloadDataInternal; public MyWebClient() { _DownloadDataInternal = typeof(WebClient).GetMethod("DownloadDataInternal", BindingFlags.NonPublic | BindingFlags.Instance); } public byte[] DownloadDataInternal(Uri address, out WebRequest request) { _DownloadDataInternal.Invoke(this, new object[] { address, out request }); } }
WebClient.DownloadDataInternal有一个out参数,我不知道如何调用它。 帮帮我!
public class MyWebClient : WebClient { delegate byte[] DownloadDataInternal(Uri address, out WebRequest request); DownloadDataInternal downloadDataInternal; public MyWebClient() { downloadDataInternal = (DownloadDataInternal)Delegate.CreateDelegate( typeof(DownloadDataInternal), this, typeof(WebClient).GetMethod( "DownloadDataInternal", BindingFlags.NonPublic | BindingFlags.Instance)); } public byte[] DownloadDataInternal(Uri address, out WebRequest request) { return downloadDataInternal(address, out request); } }
您可以像使用其他方法一样通过reflection调用带out参数的方法。 所不同的是,返回的值将被复制回参数数组,以便您可以从调用函数中访问它。
object[] args = new object[] { address, request }; _DownloadDataInternal.Invoke(this, args); request = (WebRequest)args[1];