问题
I have a generic object: MyGenericObject(of T), how do I register a custom renderer in log4net in the config file? For example, if this was a normal object the following would work:
<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject, MyClassLibrary" />
However I don't know how to write this for Generics, I attempted this:
<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject(Of MyNonGenericObject), MyClassLibrary" />
but this happened:
log4net:ERROR XmlHierarchyConfigurator: Could not find class [MyObject(Of MyNonGenericObject), MyClassLibrary].
System.TypeLoadException: Could not load type 'MyObject(Of MyNonGenericObject)' from assembly 'MyClassLibrary'.
Does anyone have any better ideas on how to achive what I want?
回答1:
The syntax you are using in XML file, MyObject(Of MyNonGenericObject)
, is specific to VB .NET, just like MyObject<MyNonGenericObject>
is specific to C#.
You change your declaration to use language-independent CLR type syntax:
MyObject`1[[MyNonGenericObject, MyClassLibrary]], MyClassLibrary
(1
after `
indicates the number of type parameters.)
So the line will be:
<renderer renderingClass="MyObjectRenderer, MyClassLibrary" renderedClass="MyObject`1[[MyNonGenericObject, MyClassLibrary]], MyClassLibrary" />
来源:https://stackoverflow.com/questions/6844163/how-to-configure-log4net-custom-object-renderer-for-generic-objects