How can I invoke a method with an out parameter?

后端 未结 2 1677
傲寒
傲寒 2020-12-04 23:39

I want expose WebClient.DownloadDataInternal method like below:

[ComVisible(true)]
public class MyWebClient : WebClient
{
    private MethodInfo _DownloadDat         


        
相关标签:
2条回答
  • 2020-12-05 00:09

    You invoke a method with an out parameter via reflection just like any other method. The difference is that the returned value will be copied back into the parameter array so you can access it from the calling function.

    object[] args = new object[] { address, request };
    _DownloadDataInternal.Invoke(this, args);
    request = (WebRequest)args[1];
    
    0 讨论(0)
  • 2020-12-05 00:20
    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题