2013-06-22 203 views
0

好的,所以我有一个关于事件触发时可以传递的EventArgs的问题。我正在设计一个小型的基本搜索引擎,并且有一个名为Query的类,其中包含一个搜索方法。当这个方法被调用时,我想要触发一个事件,它将结果传递到各种缓存类实例(SizeBoundedCache和TimeBoundedCache)中。所以我认为最好的方法是使用一个事件。C#事件参数传递

的委托声明如下 - >

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

有关这一问题的查询类内代码的其余部分是在这里(使用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; 
    } 
} 

所有缓存无论何时调用搜索,类都会被通知,我也需要接受结果。

非常感谢您的帮助。另外,如果有一种更优雅的方式可以做到这一点,我没有想到,请加入。干杯!

回答

3

你可以创建你自己的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)); 
+0

哇,好的! –

1

做一个类类型CacheStoreEventArgs从EventArgs的派生

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; 
    } 
} 

然后声明事件(使用预定义的通用的一个,所以没必要申报一个)

public event EventHandler<CacheStoreEventArgs> AddToCache; 

在你的方法内搜索你打电话给你的方法“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 
    } 
}