我刚刚定义了一个静态类实例来实现这个,但是我的程序有一些bug,我想知道静态实例会导致bug。 那么有没有什么好方法可以访问其他cs文件中的类实例?
我的代码如下:
public class AdvCANIO
{
private IntPtr hDevice;
//Device handle
private IntPtr orgWriteBuf = IntPtr.Zero;
// Fields
private Win32Events events;
private Win32Ovrlap ioctlOvr;
.....
public AdvCANIO()
{
hDevice = INVALID_HANDLE_VALUE;
.......
}
~AdvCANIO()
{
if (hDevice != INVALID_HANDLE_VALUE)
{
AdvCan.CloseHandle(hDevice);
Thread.Sleep(100);
........
Marshal.FreeHGlobal(lpCommandBuffer);
hDevice = INVALID_HANDLE_VALUE;
}
}
xx.cs
public static AdvCANIO Device ;
public MainWindow()
{
InitializeComponent();
Device = new AdvCANIO();
}
yy.cs
xx.Device.hDevice... ; //Device is the class instance in xx file.