2010-10-25 13 views
5

क्या कोई मुझे बता सकता है कि मैं सी # में नाम से कैसे कॉल कर सकता हूं?सी # में नाम से कॉल कैसे कार्यान्वित करें?

+0

का प्रयोग क्यों न आप सी # के किन संस्करणों का उपयोग कर रहे हैं (जो आप दृश्य स्टूडियो संस्करण है)? –

+0

सी # में एक कंपाइलर, जो इसे थक गया था! –

+0

@ 0xA3: इससे कोई फर्क नहीं पड़ता, यह एक कंपाइलर या ऐसा कुछ होना चाहिए ... –

उत्तर

2

आपको लगता है कि Reflection उपयोग कर सकते हैं:

 
using System; 
using System.Reflection; 

class CallMethodByName 
{ 
    string name; 

    CallMethodByName (string name) 
    { 
     this.name = name; 
    } 

    public void DisplayName()  // method to call by name 
    { 
     Console.WriteLine (name); // prove we called it 
    } 

    static void Main() 
    { 
     // Instantiate this class 
     CallMethodByName cmbn = new CallMethodByName ("CSO"); 

     // Get the desired method by name: DisplayName 
     MethodInfo methodInfo = 
     typeof (CallMethodByName).GetMethod ("DisplayName"); 

     // Use the instance to call the method without arguments 
     methodInfo.Invoke (cmbn, null); 
    } 
} 
+5

यह कॉल-बाय-नाम नहीं है। http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name –

+5

ओपी सबसे अधिक संभावना http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name को संदर्भित करता है जो नाम के आधार पर गतिशील रूप से एक विधि को कॉल करने से अलग है। –

+0

यह देखते हुए कि उन्होंने यह जवाब स्वीकार कर लिया है, मुझे संदेह है कि ओपी वास्तव में वीबी.नेट के [कॉलबीनाम] (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.interaction.callbyname) का संदर्भ दे रहा था – isedwards

1

तुम्हारा मतलब हैं this, तो मुझे लगता है कि सबसे करीब बराबर प्रतिनिधियों होगा।

+0

प्रतिनिधियों? क्या आप समझा सकते हैं ?! क्या आपका मतलब है, सिर्फ एक प्रतिनिधि का उपयोग करना? –

+0

रॉन वॉरहोलिक का उदाहरण एक प्रतिनिधि का उदाहरण है। –

9

एक मूल्य के बजाय एक लैम्ब्डा फ़ंक्शन पास करें। सी # निष्पादन को स्थगित करने के लिए बेसब्री से मूल्यांकन किया जाता है ताकि प्रत्येक साइट आपूर्ति किए गए तर्कों का फिर से मूल्यांकन करे, आपको किसी फ़ंक्शन में तर्कों को लपेटने की आवश्यकता है।

int blah = 1; 

void Foo(Func<int> somethingToDo) { 
    int result1 = somethingToDo(); // result1 = 100 

    blah = 5; 
    int result2 = somethingToDo(); // result = 500 
} 

Foo(() => blah * 100); 

यदि आप .NET 4.0 में हो एक समान (लेकिन समान नहीं) प्रभाव प्राप्त करने के Lazy वर्ग का उपयोग कर सकते हैं। Lazy परिणाम को याद करता है ताकि बार-बार पहुंच को फ़ंक्शन का पुनः मूल्यांकन करने की आवश्यकता न हो।

+3

जो लोग सोच रहे हैं, 'Lazy ' का उपयोग करके * कॉल-बाय-आवश्यकता * होगा। – porges

+0

एक लैम्ब्डा फ़ंक्शन 'प्रतिनिधि' उत्पन्न करने का एक तरीका है। –

+2

@ स्टेवेन: दरअसल, हालांकि कड़ाई से बोलने वाले लैम्बडा प्रतिनिधि नहीं हैं लेकिन प्रतिनिधि प्रतिनिधि प्रकारों के लिए पूरी तरह से परिवर्तनीय हैं। –

0

Microsoft.VisualBasic.Interaction.CallByName 
1
public enum CallType 
{ 
/// <summary> 
/// Gets a value from a property. 
/// </summary> 
Get, 
/// <summary> 
/// Sets a value into a property. 
/// </summary> 
Let, 
/// <summary> 
/// Invokes a method. 
/// </summary> 
Method, 
/// <summary> 
/// Sets a value into a property. 
/// </summary> 
Set 
} 

/// <summary> 
/// Allows late bound invocation of 
/// properties and methods. 
/// </summary> 
/// <param name="target">Object implementing the property or method.</param> 
/// <param name="methodName">Name of the property or method.</param> 
/// <param name="callType">Specifies how to invoke the property or method.</param> 
/// <param name="args">List of arguments to pass to the method.</param> 
/// <returns>The result of the property or method invocation.</returns> 
public static object CallByName(object target, string methodName, CallType callType, params object[] args) 
{ 
    switch (callType) 
    { 
    case CallType.Get: 
     { 
     PropertyInfo p = target.GetType().GetProperty(methodName); 
     return p.GetValue(target, args); 
     } 
    case CallType.Let: 
    case CallType.Set: 
     { 
     PropertyInfo p = target.GetType().GetProperty(methodName); 
     p.SetValue(target, args[0], null); 
     return null; 
     } 
    case CallType.Method: 
     { 
     MethodInfo m = target.GetType().GetMethod(methodName); 
     return m.Invoke(target, args); 
     } 
    } 
    return null; 
}