Reputation: 75
I'm developping a log4net wrapper and I must inherit a custom class from log4net.Core.LogImpl to provide a TRACE level.
With a reference to log4net, I would have :
using log4net.Core;
public class TraceLogImpl : LogImpl, ITraceLog
{
...
}
The problem is that I must have no direct reference to log4net.dll in my projet : the library is in the bin directory (that way, I do not depend on a specific version of log4net) and it is loaded at runtime.
-> I can't use "using log4net.Core".
My question is : how can I inherit from this type by using reflection?
Thank you for your help!
Upvotes: 4
Views: 1578
Reputation: 2315
You can inherit from a class at a runtime, by creating dynamic assembly and injecting some IL code into it. Here is an example of library, that inherits from an unknown interface and known class at runtime.
So, the recipe is might be like this:
However, I would'd be glad to debug this later.
Upvotes: 3
Reputation: 56537
To inherit from a class using reflection, your class has to be created via reflection itself. Take a look at Reflection.Emit.
But, to work with dynamic loaded assemblies, the best way is to have a set of well defined interfaces that you can then use to reference dynamic loaded types. You don't extend a class through inheritance, but by composition, using something like the strategy pattern.
Upvotes: 1
Reputation: 14972
If you want to add a custom level to log4net, you don't have to write code; Just by using configuration you can add your own levels
Upvotes: 0
Reputation: 16812
I would say that Reflection is a runtime action where as inheritance is an action performed during compilation. You might be able to do something with CodeDom and build a dynamic class at runtime, but I am not sure it will give you what you need.
Upvotes: 0
Reputation: 1504122
You can't, basically.
It sounds like you should possibly extract this into a separate project which does depend on log4net, and effectively treat it as an extra part of log4net itself (versioned alongside it etc).
Upvotes: 4