I want expose WebClient.DownloadDataInternal method like below:
[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 has a out parameter, I don't know how to invoke it. Help!
The invoke () method of Method class Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters automatically to match primitive formal parameters.
For using out keyword as a parameter both the method definition and calling method must use the out keyword explicitly. The out parameters are not allowed to use in asynchronous methods. The out parameters are not allowed to use in iterator methods. There can be more than one out parameter in a method.
Calling a method with an out argument In C# 6 and earlier, you must declare a variable in a separate statement before you pass it as an out argument. The following example declares a variable named number before it is passed to the Int32. TryParse method, which attempts to convert a string to a number.
The out parameter in C# is used to pass arguments to methods by reference. It differs from the ref keyword in that it does not require parameter variables to be initialized before they are passed to a method. The out keyword must be explicitly declared in the method's definition as well as in the calling method.
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];
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With