c#4的动态特性
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;
}
}