如何在回調函數中使用main()中創建的對象「communicator」? PacketCommunicator類沒有構造函數。c# - 公開在函數內部創建的對象
class Program
{
//public static PacketCommunicator communicator = new PacketCommunicator(); - can't do this; PacketCommunicator constructor doesn't exist
//public static PacketCommunicator communicator = null; - this gives "Object reference not set to an instance of an object"
public static void Main(string[] args)
{
PacketCommunicator communicator = selectedDevice.Open(...)
communicator.ReceivePackets(0, PacketHandler);
}
// Callback function invoked by ReceivePackets for every incoming packet
private static void PacketHandler(Packet packet)
{
communicator.SendPacket(packet);
}
}
我使用的庫是Pcap.Net。 基本上,我試圖做的是捕獲一個接口上的數據包,對它們執行NAT,並在另一個接口上發送它們。
使用lambda表達式和閉合 – SLaks