Tôi tự hỏi nếu có thể tải một DLL .net lúc chạy, hãy xem các phương thức có sẵn và thực hiện một lúc chạy.phương thức gọi tại thời gian chạy
Nếu đây là khả năng bạn có thể chỉ cho tôi đi đúng hướng
Tôi tự hỏi nếu có thể tải một DLL .net lúc chạy, hãy xem các phương thức có sẵn và thực hiện một lúc chạy.phương thức gọi tại thời gian chạy
Nếu đây là khả năng bạn có thể chỉ cho tôi đi đúng hướng
Nói chung, bạn sử dụng lớp System.Reflection để thực hiện tác vụ này.
Cụ thể, bạn tải DLL qua Assembly.Load (hoặc Assembly.LoadFrom) và sau đó gọi Assembly.GetTypes và sau đó cho mỗi loại gọi Type.GetMethods. Khi bạn có số MethodInfo, bạn có thể gọi số MethodInfo.Invoke trên đó.
Bạn cần phải sử dụng Reflection.
Bạn có thể gọi Assembly.LoadFile
để tải tệp .DLL chứa một hội đồng .Net, sau đó gọi phương thức GetTypes
trên đối tượng Assembly
trả về để xem các lớp trong DLL.
Khi bạn đã tìm thấy đối tượng Type
cho lớp bạn quan tâm, bạn có thể gọi phương thức InvokeMember
để gọi hàm.
Hãy coi chừng phản ánh có thể khá chậm.
Tôi thấy điều này ở 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);
Có, đây là có thể, bạn chỉ cần bắt đầu với tải dll của bạn:
Assembly assembly = Assembly.LoadFrom(assemblyPath);
Và sau đó để gọi một phương pháp bên trong dll của bạn bạn sẽ phải sử dụng reflection.
object obj = assembly.CreateInstance(<type.FullName>);
trong đó type.FullName
là thuộc tính FullName của một số loại trong hội đồng đó.
Một khi bạn đã dụ, bạn có thể gọi phương thức của bạn như thế này:
MethodInfo methodInfo = obj.GetMethod("MyMethod");
methodInfo.Invoke(obj,null);
Bạn có thể cải thiện hiệu suất của cách gọi một phương pháp thông qua phản ánh bằng cách sử dụng 'Delegate.CreateDelegate (...) 'thay vì nhận được phương pháp và gọi nó hoàn toàn bằng phản ánh: http://msdn.microsoft.com/en-us/library/system.delegate.createdelegate.aspx –
@Dan: True. Tuy nhiên, điều đó chỉ có thể xảy ra nếu bạn biết chữ ký ở thời gian biên dịch. – SLaks
hoặc sử dụng DynamicMethod. – codekaizen