2010-01-31 17 views
9

मुझे आश्चर्य है कि रनटाइम पर .NET DLL लोड करना संभव है, उपलब्ध विधियों को देखें और रनटाइम पर निष्पादित करें।रनटाइम पर कॉल विधि

यदि यह संभव है कि तुम मुझे सही दिशा

उत्तर

7

आम तौर पर, आप इस कार्य को करने के लिए System.Reflection कक्षाओं का उपयोग करते हैं।

विशेष रूप से, आप Assembly.Load (या Assembly.LoadFrom) के माध्यम से DLL लोड होता है और फिर प्रत्येक प्रकार के कॉल Type.GetMethods के लिए Assembly.GetTypes फोन और उसके बाद। जब आपके पास MethodInfo है, तो आप MethodInfo.Invoke पर कॉल कर सकते हैं।

3

आप Reflection उपयोग करने की आवश्यकता में बात कर सकते हैं।

आप DLL में कक्षाओं को देखने के लिए लौट आए Assembly वस्तु पर एक .DLL एक नेट विधानसभा युक्त लोड करने के लिए है, तो फोन GetTypes विधि Assembly.LoadFile कॉल कर सकते हैं।
एक बार जब आप रुचि रखने वाले वर्ग के लिए Type ऑब्जेक्ट पा लेते हैं, तो आप फ़ंक्शन को कॉल करने के लिए InvokeMember विधि को कॉल कर सकते हैं।

सावधान रहें कि प्रतिबिंब काफी धीमा हो सकता है।

+0

आप प्रतिबिंब के माध्यम से एक विधि लागू के प्रदर्शन में सुधार कर सकते हैं:

बार जब आप अपने उदाहरण मिल गया, तो आप अपने विधि इस तरह आह्वान कर सकते हैं विधि और प्रतिबिंब द्वारा इसे पूरी तरह से कॉल करना: http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx –

+1

@Dan: True। हालांकि, यह केवल तभी संभव है जब आप संकलन समय पर हस्ताक्षर जानते हों। – SLaks

+0

या डायनामिक मोड का उपयोग करें। – codekaizen

1

मैं पर इस पाया reflection eamples

// get all public static methods of MyClass type 
    MethodInfo[] methodInfos = typeof(MyClass).GetMethods(BindingFlags.Public | 
                BindingFlags.Static); 


[C#] 
// dynamically load assembly from file Test.dll 
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll"); 

[C#] 
// get type of class Calculator from just loaded assembly 
Type calcType = testAssembly.GetType("Test.Calculator"); 

[C#] 
// create instance of class Calculator 
object calcInstance = Activator.CreateInstance(calcType); 

[C#] 
// get info about property: public double Number 
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number"); 

[C#] 
// get value of property: public double Number 
double value = (double)numberPropertyInfo.GetValue(calcInstance, null); 

[C#] 
// set value of property: public double Number 
numberPropertyInfo.SetValue(calcInstance, 10.0, null); 

[C#] 
// get info about static property: public static double Pi 
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi"); 

[C#] 
// get value of static property: public static double Pi 
double piValue = (double)piPropertyInfo.GetValue(null, null); 

[C#] 
// invoke public instance method: public void Clear() 
calcType.InvokeMember("Clear", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, 
null, calcInstance, null); 

[C#] 
// invoke private instance method: private void DoClear() 
calcType.InvokeMember("DoClear", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, 
null, calcInstance, null); 

[C#] 
// invoke public instance method: public double Add(double number) 
double value = (double)calcType.InvokeMember("Add", 
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, 
null, calcInstance, new object[] { 20.0 }); 

[C#] 
// invoke public static method: public static double GetPi() 
double piValue = (double)calcType.InvokeMember("GetPi", 
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, 
null, null, null); 

[C#] 
// get value of private field: private double _number 
double value = (double)calcType.InvokeMember("_number", 
BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, 
null, calcInstance, null); 
3

हाँ, यह संभव है, आप बस अपने dll लोड के साथ शुरू:

Assembly assembly = Assembly.LoadFrom(assemblyPath); 

और फिर अपने dll आप हूँ अंदर एक विधि आह्वान करने के लिए reflection का उपयोग करना होगा।

object obj = assembly.CreateInstance(<type.FullName>); 

जहां type.FullName उस असेंबली में किसी प्रकार की पूर्ण नाम संपत्ति है। का उपयोग करते हुए `Delegate.CreateDelegate (...)` के बजाय हो रही द्वारा

MethodInfo methodInfo = obj.GetMethod("MyMethod"); 
methodInfo.Invoke(obj,null); 
संबंधित मुद्दे