46

我想公开 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 参数,我不知道如何调用它。帮助!

4

2 回答 2

125

就像任何其他方法一样,您可以通过反射调用带有 out 参数的方法。不同之处在于返回的值将被复制回参数数组,以便您可以从调用函数中访问它。

object[] args = new object[] { address, request };
_DownloadDataInternal.Invoke(this, args);
request = (WebRequest)args[1];
于 2010-03-13T10:53:45.470 回答
22
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);
    }
}
于 2010-03-13T10:58:51.060 回答