如何通过事件收集有关它的信息时正确创建A类的新实例?

时间:2014-01-14 02:23:11

标签: c# .net events

我有一个:

class A{
int val1, val2, val3}

在我的其他课程中

 static void eventA(int val1){ //invoked somewhere
  EventManager eventManager1 = new EventManager();
  eventManager1.ServiceResolved += new Events_ServiceResolvedEventHandler(eventB);
 }

 static void eventB(int val2){
  EventManager eventManager1 = new EventManager();
  eventManager1.AddressInfoFound += new Events_AddressFoundEventHandler(eventC);
 }

 static void eventC(int val3){
   //CODE
 }

这些事件仅按顺序调用:eventA,eventB,eventC。在eventA中我可以访问val1,在eventB中我可以访问val2等。 我应该如何在这个班级中设置A类对象呢?


编辑:

    uusing Bonjour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace HomeSecurity {
    class Scanner {
        public static DNSSDService service;
        public static DNSSDEventManager eventManager;
        public static DNSSDService browse;

        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);
                //   Application.Run(form = new PumpingForm());
            } 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-----------------");
            }
        }

        private static void eventManager_AddressFound(DNSSDService service, DNSSDFlags flags, uint ifIndex, string hostname, DNSSDAddressFamily addressFamily, string address, uint ttl) {
            try {
                Console.WriteLine("------------------eventManager_AddressFound----------------------");
                Console.WriteLine("hostname " + hostname);
                Console.WriteLine("address " + address);
                Console.WriteLine("----------------------------------------");
            } catch (Exception e) {

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

            }

        }
    }
}

2 个答案:

答案 0 :(得分:0)

你可以使用默认构造函数A,然后有3个函数来初始化val1,val2和val3。然后我会有第4个函数来确保初始化所有三个值。

答案 1 :(得分:0)

以下是粗略示例如何按顺序收听3个事件并收集前几个事件的数据(即字典中):

class MyEventListner 
{
    Dictionary<string, string> fields = new Dictionary<string, string>();
    public MyObject Data {get;private set;}

    void HandleEvent1(EventData eventData) { fields["Field1"] = eventData.Field;}
    void HandleEvent2(EventData2 eventData) { fields["Field2"] = eventData.FieldFoo;}
    void HandleEvent3(EventData eventData) { 
       Data  = new MyObject { 
          Field1 = fields["Field1"],
          Field2 = fields["Field2"],
          Filed3 = eventData.Field
    }
}

MyEventListner listener = new MyEventListner ();
WhateverEventManager eventManager = new eventManager();

void Subscribe()
{
    eventManager.OneEvent += d => listener.HandleEvent1(d);
    eventManager.OtherEvent += d => listener.HandleEvent2(d);
    eventManager.LastEvent += d => listener.HandleEvent3(d);
}
LastEvent解雇listener.Data之后

将填写数据对象。可能想要通知某人(即通过另一事件)Data准备就绪。