SilverlightアプリケーションでDIコンテナーとしてNinjectを使用しています。現在、インターセプトをサポートするようにアプリケーションを拡張し、Ninject用のDynamicProxy2拡張機能の統合を開始しました。ViewModelのプロパティへの呼び出しをインターセプトしようとすると、次の例外が発生します。
「メソッドへのアクセスに失敗しました:System.Reflection.Emit.DynamicMethod..ctor(System.String、System.Type、System.Type []、System.Reflection.Module、Boolean)」</ p>
この例外は、invocation.Proceed()メソッドが呼び出されたときにスローされます。インターセプターの2つの実装を試しましたが、どちらも失敗します
public class NotifyPropertyChangedInterceptor: SimpleInterceptor
{
protected override void AfterInvoke(IInvocation invocation)
{
var model = (IAutoNotifyPropertyChanged)invocation.Request.Proxy;
model.OnPropertyChanged(invocation.Request.Method.Name.Substring("set_".Length));
}
}
public class NotifyPropertyChangedInterceptor: IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
var model = (IAutoNotifyPropertyChanged)invocation.Request.Proxy;
model.OnPropertyChanged(invocation.Request.Method.Name.Substring("set_".Length));
}
}
プロパティ値が設定されているときに、ViewModelでOnPropertyChangedメソッドを呼び出したい。
属性ベースのインターセプトを使用しています。
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class NotifyPropertyChangedAttribute : InterceptAttribute
{
public override IInterceptor CreateInterceptor(IProxyRequest request)
{
if(request.Method.Name.StartsWith("set_"))
return request.Context.Kernel.Get<NotifyPropertyChangedInterceptor>();
return null;
}
}
コンソールアプリケーションを使用して実装をテストしましたが、問題なく動作します。
また、Ninject.dllと同じフォルダーにNinject.Extensions.Interception.DynamicProxy2.dllがある限り、コンソールアプリケーションで、DynamicProxy2Moduleをカーネルに明示的にロードする必要はありませんでしたが、Silverlightアプリケーション用に明示的にロードする必要がありました。次のように:
IKernel kernel = new StandardKernel(new DIModules(), new DynamicProxy2Module());
誰か助けてもらえますか?ありがとう