是否可以向事件方法添加其他参数?

时间:2014-01-14 21:23:12

标签: c# events

我有事件方法eventManager_ServiceFound然后在该方法中有一个事件分配给方法eventManager_ServiceResolved,我想从方法eventManager_ServiceFound传递一些额外的参数。

类似的东西:

static void eventManager_ServiceResolved(parameters which are already there, string domain(form eventManager_ServiceFound))

代码如何显示:

  public static void ScanService() {
            try {
                service = new DNSSDService();
                eventManager = new DNSSDEventManager();
                eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(eventManager_ServiceFound);

                browse = service.Browse(0, 0, "_axis-video._tcp", null, eventManager);
            } catch (Exception e) {

                Console.WriteLine("--------------------EXCEPTION-----------------");
                Console.WriteLine(e);
                Console.WriteLine("--------------------EXCEPTION-----------------");
            }
        }



        static void eventManager_ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain) {
            try {
                Console.WriteLine("---------------- eventManager_ServiceFound------------------------");
                Console.WriteLine("browser: " + browser + "\nDNSSDFlags " + flags + "\nifIndex " + ifIndex + "\nserviceName: " + serviceName + "\nregtype: " + regtype + "\ndomain: " + domain);
                Console.WriteLine("----------------------------------------");

                //   DNSSDService service2 = new DNSSDService();
                eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(eventManager_ServiceResolved);
                browser.Resolve(flags, ifIndex, serviceName, regtype, domain, eventManager);
            } catch (Exception e) {

                Console.WriteLine("--------------------EXCEPTION--------eventManager_ServiceFound---------");
                Console.WriteLine(e);
                Console.WriteLine("--------------------EXCEPTION-----------------");
            }
        }

        private static void eventManager_ServiceResolved(DNSSDService service, DNSSDFlags flags, uint ifIndex, string fullname, string hostname, ushort port, TXTRecord record) {
            try {
                Console.WriteLine("-------------------eventManager_ServiceResolved---------------------");
                Console.WriteLine("DNSSDService " + service + "\nDNSSDFlags " + flags + "\nifindex " + ifIndex + "\nfullname " + fullname + "hostname " + hostname + "\nport " + port + "\nrecord " + record);
                var str = System.Text.Encoding.Default.GetString(record.GetValueForKey("macaddress"));
                Console.WriteLine("mac " + str);
                Console.WriteLine("----------------------------------------");

                //    DNSSDService service2 = new DNSSDService();
                eventManager.AddressFound += new _IDNSSDEvents_AddressFoundEventHandler(eventManager_AddressFound);
                DNSSDAddressFamily family = new DNSSDAddressFamily();
                service.GetAddrInfo(flags, ifIndex, family, hostname, eventManager);
            } catch (Exception e) {

                Console.WriteLine("--------------------EXCEPTION--------eventManager_ServiceResolved---------");
                Console.WriteLine(e);
                Console.WriteLine("--------------------EXCEPTION-----------------");
            }
        }

编辑:

   eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(eventManager_ServiceResolved);//how to convert this line into this one below
            eventManager.ServiceResolved += (x, y, z) => eventManager_ServiceResolved(x, y, z, a, b, c);
            browser.Resolve(flags, ifIndex, serviceName, regtype, domain, eventManager);

EDIT2:

只有一次服务火灾解决了一次服务火灾。但是会有很多服务发现火灾

1 个答案:

答案 0 :(得分:1)

使用lambda:

eventManager.ServiceResolved += (x, y, z) => eventManager_ServiceResolved(x, y, z, a, b, c);