문제

나는 Castle DynamicProxy2를 사용하여 인터페이스의 "Tack"을 사용하여 사전에서 필드를 검색합니다. 예를 들어, 다음 수업이 주어지면 :

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

인터셉터에서 "대상"데이터 컨테이너를 가져 와서 "이름"값을 반환하고 싶습니다.

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");

기본 목표를 얻는 방법에 대한 모든 생각

참고 : 이것은 내가 가지고있는 질문에 대한 컨텍스트를 설정하기 위해 내가 사용하고있는 예제입니다.

도움이 되었습니까?

해결책

나는 그것을 알아. 프록시를 iproxytargetAccessor에 시전해야합니다.

public void Intercept(IInvocation invocation)
{
    object fieldName = omitted; // get field name based on invocation information

    var accessor = invocation.Proxy as IProxyTargetAccessor;

    DataContainer container = (DataContainer) accessor.DynProxyGetTarget();
    invocation.ReturnValue = container.Fields[fieldName];
}

다른 팁

왜 번거 로움?

사용

var container = invocation.InvocationTarget as DataContainer;

BTW, IIUC, 이미 제공 한 것을 구현하려고합니다. 캐슬 dictionaryadapter. 이미 거기에있는 것을 사용하지 않는 이유는 무엇입니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top