views:

317

answers:

3

Hi all,

I am to build a SOA gui framework, and I'd like to autodetect services, and service dependencies from client modules. I have code such as this so far, which works using attributes, placed on class modules:

[ServiceProvider(typeof(DemoService3))]
[ServiceConsumer(typeof(DemoService1))]

I am wondering how I can scan for these automagically, so that people wouldn't forget to add the marker and potentially get null references at runtime. In the code services are registered and fetched via the following commands:

Services.RegisterService(new DemoService1());
Services.FetchService<DemoService3>();

I want to find these calls, and also the types being passed in (both take a type param, implicit for the first one)... the rest of the code for doing my dependencies and construction is already done :)

+4  A: 

You will need to analyze the IL at the CLR level, not the C# level to figure this out.

You should be able to leverage Mono Cecil to pull this off.

FlySwat
+2  A: 

You can either use Mono.Cecil or .NET reflection to accomplish that.

Mono.Cecil is recommended due to its better performance and flexibility. Here are some samples (Cecil + simple extensions on top) that could get you started:

Rinat Abdullin
+1  A: 

If you're unable to use Mono.Cecil for some reason, you could consider parsing the IL by hand: you'd effectively just need to find call and callvirt instructions, possibly doing static analysis enough to understand the type returned by new DemoService1().

typeof(YourClass).GetMethod("YourMethod").GetMethodBody().GetILAsByteArray() is your friend.

Tim Robinson