मैं निम्नलिखित वर्ग पदानुक्रम:मैं संकलित अभिव्यक्तियों के लिए सी # विधियों को कैसे परिवर्तित कर सकता हूं?
public class Parent
{
[DebuggerStepThrough]
public void SayParent()
{
Console.WriteLine("Parent");
}
}
public sealed class Child : Parent
{
private static int _number = 0;
public Child() // May contain parameter i.e. not always parameterless consctructor
{
_number++;
}
[DebuggerStepThrough]
public void SayInstance()
{
Console.WriteLine("{0}-Say", _number);
}
[DebuggerStepThrough]
public void SayInstanceWithArg(string input)
{
Console.WriteLine("{0}-Say: {1}", _number, input);
}
[DebuggerStepThrough]
public static void SayStatic()
{
Console.WriteLine("{0}-Say", _number);
}
[DebuggerStepThrough]
public static void SayStaticWithArg(string input)
{
Console.WriteLine("{0}-Say: {1}", _number, input);
}
[DebuggerStepThrough]
public static Task SayStaticWithArgAndReturn(string input)
{
Console.WriteLine("{0}-Say: {1}", _number, input);
return null;
}
}
मैं तथापि प्रतिबिंब का उपयोग कर प्रदर्शन मैं Delegate
का सहारा की जरूरत है सुधार करने के लिए किसी भी समय पर Child
का एक नया उदाहरण के लिए इनमें से किसी भी आह्वान करने के लिए सक्षम होना चाहिए और/या Compiled Expressions
।
इसलिए उदाहरण के लिए मैं हो सकता है:
var instanceOne = new Child();
var instanceTwo = new Child();
जिसके लिए मैं क्रम में करने की आवश्यकता होगी इन तरीकों उन इसकी जरूरत है कि के लिए तर्क गुजर आह्वान। ध्यान दें कि उनमें कुछ पैरामीटर स्वीकार करने वाले static
और instance
विधियां शामिल हैं।
मैं अब तक "SayInstance" विधि के लिए निम्नलिखित की कोशिश की है:
var sayInstanceMethod = typeof(Child)
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
.Where(m => m.GetCustomAttributes(typeof(DebuggerStepThroughAttribute), true).Length > 0)
.Where(t => t.Name == "SayInstance")
.First()
और फिर:
var instance = Expression.Constant(new Child()); // This should NOT be Constant, but then what should it be?!
var mCallInstance = Expression.Call(instance, sayInstanceMethod);
Action action = Expression.Lambda<Action>(mCallInstance).Compile();
action();
action(); // I need to pass in a new instance of Child to this method somehow
हालांकि मैं हो रही है:
1-Say
1-Say
के बजाय:
1-Say
2-Say
मुझे संदेह है कि यह Expression.Constant
के कारण है, लेकिन मुझे यह पता नहीं लगा सकता कि मैं इसे Child
के रनटाइम पर अपने लक्ष्य के रूप में कैसे स्वीकार कर सकता हूं।
मैं निराश हूँ जब यह :-(
मैं मूल रूप से लागू करने के लिए क्या जॉन स्कीट का उल्लेख है HERE या तो Delegates
या Compiled Expressions
का उपयोग कर कोशिश कर रहा हूँ Expressions
लिए आता है।
किसी भी मदद की बहुत सराहना की है।
आप कन्स्ट्रक्टर पर संख्या बढ़ा रहे हैं। और यह स्थैतिक क्षेत्र है। तो, आपके पास कभी भी नतीजा नहीं होगा। _number फ़ील्ड को एक निजी क्षेत्र में स्टोर करें –
यह सुनिश्चित करने का तरीका है कि अंतिम परिणाम काम करेगा। मुख्य समस्या यह है कि मैं एक अभिव्यक्ति बनाने में असमर्थ हूं जो रनटाइम पर 'चाइल्ड' के नए उदाहरण को स्वीकार करता है। – MaYaN
लाइनें 'var instanceOne = new child(); 'और' var instanceTwo = new child();' उसी परीक्षण में भाग गया जो 'क्रिया();' दो बार 'कहता है? – Mat