2013-06-22 109 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 
    } 
}