我正在使用Castle DynamicProxy2来“添加”接口以从字典中检索字段。例如,给出下面的类:Castle DynamicProxy2:获取拦截器内的目标?
public class DataContainer : IDataContainer
{
private Dictionary<string, object> _Fields = null;
public Dictionary<string, object> Data
{
get { return _Fields ?? (_Fields = new Dictionary<string, object>()); }
}
}
我想用下面的接口作为接口代理提取“名称”值从田野词典:
public interface IContrivedExample
{
string Name { get; }
}
从一个拦截器,我想要得到的“目标” DataContainer,并返回“名称”值:
public void Intercept(IInvocation invocation)
{
object fieldName = omitted; // get field name based on invocation information
DataContainer container = ???; // this is what I'm trying to figure out
invocation.ReturnValue = container.Fields[fieldName];
}
// Somewhere in code
var c = new DataContainer();
c.Fields.Add("Name", "Jordan");
var pg = new ProxyGenerator();
IContrivedExample ice = (IContrivedExample) pg.CreateInterfaceProxyWithTarget(..., c, ...);
Debug.Assert(ice.Name == "Jordan");
如何让潜在的目标有什么想法
注意:这是一个人为设计的例子,我用它来建立一些关于我所拥有的问题的背景。
最初,我尝试将InvocationTarget作为DataContainer进行投射,但没有奏效。这是什么导致我使用IProxyTargetAccessor。 至于DictionaryAdapter 1)我不知道它存在和2)我有“DataContainer”其他属性不只是字段。我忽略了这一点,因为它与我所问的问题无关。 谢谢! – Jordan 2009-05-14 16:16:23