Diễn đàn    Dotnet framework    Late Binding

Thành viênTrả lời
radiogaga

Ta đây thủy tinh, có tài gọi mưa hô gió ... Sơn Tinh ta đây : có tài là đốn gốc cây, đánh hốc cây ...
45  bài
16-9-2008 11:36:46
Mới tìm được cái này hay hay, copy cho anh em xem. Đó là late binding, tức là gọi function sau.

MethodInfo mi;
object result = null;
object[] args = new object[] {"ABC123"};
try
{
Assembly assemblyInstance = Assembly.LoadFrom(@"c:\AssemblyDir\SomeControls.dll");
Type[] types = assemblyInstance.GetTypes();
foreach (Type t in types)
{
mi = t.GetMethod("GetUserName");
if (mi != null)
{
string typeName = t.FullName;
object lateBoundObj = Asm.CreateInstance(typeName);
result = t.InvokeMember (MethodName, BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, lateBoundObj, args);
break;
}
}
string userName = result.ToString();
MessageBox("User Name is: {0}", userName)
}
catch(Exception ex)
{
MessageBox.Show(ex.Message)
}
 
radiogaga

Ta đây thủy tinh, có tài gọi mưa hô gió ... Sơn Tinh ta đây : có tài là đốn gốc cây, đánh hốc cây ...
45  bài
16-9-2008 11:41:16
Còn một cái nữa cũng khá hay

class Class1{
[DllImport("kernel32")]
public extern static int LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
public extern static bool FreeLibrary(int hLibModule);
[DllImport("kernel32", CharSet=CharSet.Ansi)]
public extern static int GetProcAddress(int hModule, string lpProcName);
/*
[DllImport("msjava", CharSet=CharSet.Unicode)]
public extern static int call(int funcptr, int hwnd, string message, string title, int flags);
*/
[DllImport("Invoke", CharSet=CharSet.Unicode)]
public extern static int InvokeFunc(int funcptr, int hwnd, string message, string title, int flags);


static void Main(string[] args) {
int hmod=LoadLibrary("User32");
int funcaddr=GetProcAddress(hmod, "MessageBoxW");
int result=InvokeFunc(funcaddr, 0, "Hello World", ".Net dynamic export invocation", 1 /*MB_OKCANCEL*/);
Console.WriteLine("Result of invocation is " + result);

FreeLibrary(hmod);

Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM