简体   繁体   English

C#事件参数传递

[英]C# Event Argument passing

Ok, so I have a question regarding the EventArgs that can be passed when an event is triggered. 好的,所以我有一个关于触发事件时可以传递的EventArgs的问题。 I am designing a small, basic search engine and have a class called Query that contains a method Search. 我正在设计一个小的基本搜索引擎,并且有一个名为Query的类,其中包含搜索方法。 When this method is called, I want to trigger an event which will pass the results to be storred in a variety of cache class instances (SizeBoundedCache and TimeBoundedCache). 调用此方法时,我想触发一个事件,该事件将结果存储在各种缓存类实例(SizeBoundedCache和TimeBoundedCache)中。 So I thought the best way to do this would be to use an event. 所以我认为最好的方法是使用一个事件。

The delegate is declared like this -> 像这样声明委托->

public delegate void CacheStoreDelegate(object sender, EventArgs e);

The rest of the code within the Query class relevant to this question is here (uses Linq) -> Query类中与此问题相关的其余代码在这里(使用Linq)->

public event CacheStoreDelegate AddToCache;

public virtual void OnQuery (EventArgs e)
{
    if(AddToCache != null)
        AddToCache(this, e);
}

public Query()
{
}

public Query(string queryString, OOP5.Provided.QueryOperator op)
{
    //Access and set the terms array
    this.Terms = OOP5.Provided.QueryUtils.GetTermsFromString(queryString);
    this.Operator = op; 
}

public static IEnumerable<string> Search (this SearchCore s, IQuery q)
{
    // Accept a query and return IEnumerable<string> of
    // all document IDs matching that query
    if (q.Operator == QueryOperator.Any)
    {
        var GetAnyMatch = from single_query in q.Terms
                          group s.Search(single_query)
                          by s.documents.Keys
                          into results
                          where results.Count >= 1
                          select results[0];

        this.OnQuery(GetAnyMatch);
        return GetAnyMatch;
   }

   if (q.Operator == QueryOperator.All)
   {
       var GetAllMatch = from single_query in q.Terms
                         group s.Search(single_query)
                         by s.documents.Keys
                         into results
                         where results.Count >= q.Terms.Lengthselect results[0];

        this.OnQuery(GetAllMatch);
        return GetAllMatch;
   }
}

All the cache classes will be notified whenever a search is called and I also need thme to receive the results. 每当调用搜索时,所有缓存类都会收到通知,我也需要thme来接收结果。

Thanks so much in advance for the help. 非常感谢您的帮助。 Also, if there is a more elegant way to do this that I am not thinking of, please chime in. Cheers! 另外,如果有一种我没有想到的更优雅的方法,请发出提示。干杯!

You could create your own EventArgs implementation : 您可以创建自己的EventArgs实现

class QueryResultEventArgs : EventArgs
{
    public IEnumerable<string> Results { get; private set; }

    public QueryResultEventArgs(IEnumerable<string> results)
    {
        Results = results;
    }
}

...

public delegate void CacheStoreDelegate(object sender, QueryResultEventArgs e);

...

this.OnQuery(new QueryResultEventArgs(GetAnyMatch));

Make a class of type CacheStoreEventArgs deriving from eventargs 制作一个从eventargs派生的CacheStoreEventArgs类型的类

public class CacheStoreEventArgs:eventargs
{
    private IEnumerable<string> Data;//List<string> better

    public IEnumerable<string> data
    {
        get { return Data; }
        set { this.Data = value; }
    }

    public CacheStoreEventArgs(IEnumerable<string> NewData)
    {
        this.data = NewData;
    }
}

then declare the event(use predefined generic one,so no need to declare one) 然后声明事件(使用预定义的通用事件,因此无需声明一个)

public event EventHandler<CacheStoreEventArgs> AddToCache;

inside your method search you call your method "On...." 在方法搜索中,您将方法称为“开...”。

public static IEnumerable<string> Search (this SearchCore s, IQuery q)
{
    //after you get query result
    CacheStoreEventArgs cs = new CacheStoreEventArgs(queryresultvariablehere);
    //and call your method now with the instance of your derived eventargs class
    OnQuery(cs);
}

public virtual void OnQuery (CacheStoreEventArgs e)
{
     try
     {
        EventHandler<CacheStoreEventArgs> temp = AddToCache
        if( temp != null)
              temp(this,e);
     }
     catch(Exception ex) 
     {
        //exception handling
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM