Qualcuno può dirmi come è possibile implementare Chiamata per nome in C#?Come implementare la chiamata per nome in C#?
risposta
è possibile farlo utilizzando 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); } }
Questo non è nome per nome. http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name –
L'OP molto probabilmente si riferisce a http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name che è diverso dal chiamare dinamicamente un metodo basato sul nome. –
Dato che hanno accettato questa risposta, sospetto che l'OP si riferisca effettivamente a [CallByName] di VB.NET (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.interaction.callbyname) – isedwards
Se vuoi dire this, allora penso che l'equivalente più sarebbe delegati.
delegati? puoi spiegare come ?! vuoi dire, basta usare un delegato? –
L'esempio di Ron Warholic è un esempio di delegato. –
Passa una funzione lambda anziché un valore. C# è valutato con interesse per rinviare l'esecuzione in modo che ogni sito rivaluta gli argomenti forniti che servono per avvolgere gli argomenti in una funzione.
int blah = 1;
void Foo(Func<int> somethingToDo) {
int result1 = somethingToDo(); // result1 = 100
blah = 5;
int result2 = somethingToDo(); // result = 500
}
Foo(() => blah * 100);
È possibile utilizzare la classe Lazy se siete in .NET 4.0 per ottenere un effetto simile (ma non identico). Lazy
memorizza il risultato in modo che gli accessi ripetuti non debbano rivalutare la funzione.
Per coloro che si stanno chiedendo, l'uso di 'Lazy
Una funzione lambda è un modo per generare un 'delegato'. –
@Steven: In effetti, tuttavia, i lambda in senso stretto non sono delegati ma implicitamente convertibili in tipi di delegati corrispondenti. –
Perché non utilizzare
Microsoft.VisualBasic.Interaction.CallByName
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;
}
Quale versione di C# stai usando (che cosa è voi versione di Visual Studio)? –
Un compilatore in C#, che lo avrebbe thunk! –
@ 0xA3: Non importa, dovrebbe essere come un compilatore o qualcosa del genere ... –