1.調用COM組件:
Type type = Type.GetTypeFromProgID("VisualPHD.Data");
dynamic phd = Activator.CreateInstance(type);
phd.HostName = "192.168.53.186";
2. 反射調用dotNet類
Type type = Type.GetType("ConsoleApplication1.Test");
dynamic t = Activator.CreateInstance(type); t.Print("你好!");
3. 使用PInvoke更簡單
下載開源組件http://code.google.com/p/dynamicdllimport/
可以使用下面方面調用非託管dll,包括Windows api和自己開發的c格式dll
dynamic user32 = new DynamicDllImport("user32.dll", callingConvention : CallingConvention.Winapi);
user32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0);
dynamic asmproject = new DynamicDllImport("asmproject.dll");
int value = asmproject.add<int>(3, 4);
Console.WriteLine(value);
dynamic sdl = new DynamicDllImport("SDL.dll", CharSet.Ansi);
Sdl.SDL_Rect rect = new Sdl.SDL_Rect(
0,
0,
(short)width,
(short)height);
int result = sdl.SDL_FillRect<int>(rgbSurfacePtr, ref rect, 0);
Sdl.SDL_Event evt;
while (sdl.SDL_WaitEvent(out evt) != 0)
{
if (evt.type == Sdl.SDL_QUIT)
{
break;
}
}