Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a problem which I am not quite sure how to handle. When my application starts, I need to read the configuration file and - based on the data read from the file construct some objects. The objects are constructed using reflection - I look for a most suitable constructor and invoke it. I know that all instances constructed this way implemented the same interface - let's call it ISomeInterface. Then constructed instances than have to be registered with an IoC container in order to be used when constructing the object graph. The problem is that some of those objects might also implement any other interface. and they have to be registered under all interfaces they implement(not just ISomeInterface). I was thinking about using reflection to retrieve the list of interfaces each object implements and than registering them with the IoC, but I am not sure how to register an object with the interface which is represented with an instance of Type class. Can anybody suggests any other way of doing that beside manually checking whether an instance implements a specific interface, type-casting it to that interface and than registering it in a "normal" way. It might be worth noting that I intend to use AutoFac. Any ideas will be appreciated.

Uros
Posted

1 solution

Sorry for the lame question. The solution is obvious but I somehow missed it. To register all interfaces implemented by some instance all one has to do is:

C#
builder.RegisterInstance(instance).AsImplementedInterfaces();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900