以下のサンプルで、呼び出しの前後の繰り返し行のペアを回避するにはどうすればよいですか? 詳細:これは、実際のより大きなコードのコンパイル可能なモックです。通常、これはさまざまなAPIを備えたサービスクライアントを含むプロキシクラスのレイヤーです。繰り返しの部分は、すべてのクライアントのすべてのメソッドの呼び出しの前後です。残念ながら、考えられるすべてのメソッドに単一の署名はありません。前部と後部には、クライアントのチャネルとコンテキストへのポインタが必要です。AOP、ジェネリック、デリゲート、属性などの高度なものを適用することは可能ですか?ありがとうございました
using System;
namespace ConsoleApplication
{
class ClassServiceClient: IDisposable
{
public Object channel()
{
return "something";
}
public Object context()
{
return "something other";
}
}
class ClassA : ClassServiceClient
{
public Object methodA()
{
return "something other";
}
}
class ClassB : ClassServiceClient
{
public void methodB(string param)
{
return;
}
}
class ClassAProxy
{
public Object methodA()
{
using (ClassA client = new ClassA())
{
Program.preparation(client.channel()); //<---- repetitive part
Object result = client.methodA();
Program.postinvocation(client.context());//<---- repetitive part
return result;
}
}
}
class ClassBProxy
{
public void methodB(string param)
{
using (ClassB client = new ClassB())
{
Program.preparation(client.channel()); //<---- repetitive part
client.methodB(param);
Program.postinvocation(client.context());//<---- repetitive part
return;
}
}
}
class Program
{
public static void preparation(Object channel)
{
// Do something with channel
}
public static void postinvocation(Object context)
{
// Do something with context
}
static void Main(string[] args)
{
}
}
}