2010-03-13 11 views
37

मैं नीचे की तरह 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 एक बाहर पैरामीटर है, मैं इसे कैसे लागू करने की पता नहीं है। सहायता!

उत्तर

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); 
    } 
} 
102

आप किसी अन्य विधि की तरह प्रतिबिंब के माध्यम से आउट पैरामीटर के साथ एक विधि का आह्वान करते हैं। अंतर यह है कि लौटाया गया मान पैरामीटर सरणी में वापस कॉपी किया जाएगा ताकि आप इसे कॉलिंग फ़ंक्शन से एक्सेस कर सकें।

object[] args = new object[] { address, request }; 
_DownloadDataInternal.Invoke(this, args); 
request = (WebRequest)args[1]; 
+0

पहली पंक्ति संकलित किया जा cann't। – ldp615

+4

मैन, आप सबसे अच्छे हैं! – Luca

+0

क्या होगा यदि कोई और ओवरलोडेड विधि हो ??? ------- 1 -------- int test (int i, स्ट्रिंग एस) { एस = ""; वापसी 0; } -------------- और ----------- int test (int i) { वापसी 0; } – MrClan

संबंधित मुद्दे