【发布时间】:2011-08-30 22:06:57
【问题描述】:
在创建接口方法的委托时,我正在努力找出哪里出错了
我的代码如下:
private static Func<HtmlDocument, IObservable<IData>> FindScrapeMethod(ICrawlerStrategy crawler, string scrapeDelegate)
{
Func<HtmlDocument, IObservable<IData>> action;
var fullDelegateName = String.Format("ICrawlerStrategy.{0}", scrapeDelegate);
if (!_delegateCache.TryGetValue(fullDelegateName, out action))
{
var method = typeof(ICrawlerStrategy).GetMethod(scrapeDelegate, BindingFlags.Public | BindingFlags.Instance );
action = (Func<HtmlDocument, IObservable<IData>>)
Delegate.CreateDelegate(typeof(Func<HtmlDocument, IObservable<IData>>), crawler, method);
_delegateCache.Add(fullDelegateName, action);
}
return action;
}
接口声明是
public interface ICrawlerStrategy
{
Func<HtmlDocument, IObservable<IData>> ExtractorAsync();
}
实现类如下
public class MyCrawler : ICrawlerStrategy
{
<snip>
Func<HtmlDocument, IObservable<IData>> ICrawlerStrategy.ExtractorAsync()
{
return (doc) => AsyncScraper(doc);
}
}
Edit1 - 按照@Yahia 的要求:
public IObservable<IData> AsyncScraper(HtmlDocument page)
在尝试创建委托时,我收到“错误绑定到目标方法”。当我单步执行代码时,
- 方法不为空,所以很明显可以在类型上找到方法
- 实例也不为空
任何指针,请。
谢谢
S
【问题讨论】:
-
请显示
AsyncScraper的声明和对FindScrapeMethod的调用包含参数定义...