質問

Castle dynamicproxyとプロパティを傍受するより良い方法についての提案はありますか?

具体的には、私は傍受しているpropertyInfoが必要ですが、それはi invocationに直接ではないので、私がしていることは次のとおりです。

public static PropertyInfo GetProperty(this MethodInfo method)
{
    bool takesArg = method.GetParameters().Length == 1;
    bool hasReturn = method.ReturnType != typeof(void);
    if (takesArg == hasReturn) return null;
    if (takesArg)
    {
        return method.DeclaringType.GetProperties()
            .Where(prop => prop.GetSetMethod() == method).FirstOrDefault();
    }
    else
    {
        return method.DeclaringType.GetProperties()
            .Where(prop => prop.GetGetMethod() == method).FirstOrDefault();
    }
}

その後、私のIinterceptorで:

public void Intercept(IInvocation invocation)
{
    bool doSomething = invocation.Method
                                 .GetProperty()
                                 .GetCustomAttributes(true)
                                 .OfType<SomeAttribute>()
                                 .Count() > 0;

}

役に立ちましたか?

解決

通常、これは利用できません。 DynamicProxyはメソッド(ゲッターとセッターを含む)をインターセプトし、プロパティを気にしません。

インターセプターを作成することで、このコードを少し最適化できます IOnBehalfAware (見る ここ)およびメソッドの発見 - >プロパティマッピング事前。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top