我想要公开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参数,我不知道如何调用它。 救命!
答案 0 :(得分:112)
通过反射调用out参数的方法就像任何其他方法一样。不同之处在于返回的值将被复制回参数数组,以便您可以从调用函数访问它。
object[] args = new object[] { address, request };
_DownloadDataInternal.Invoke(this, args);
request = (WebRequest)args[1];
答案 1 :(得分:18)
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);
}
}