问题
I'm running into some problems configuring Unity 3.0 via XML configuration. Resolving the generic type will give me the following exception message:
The type INLog`1 does not have an accessible constructor.
Here's the layout for my logging classes:
namespace Common.Utils.Logging {
public interface INLog
{
}
public interface INLog<T> : INLog where T : class
{
}
public class NLog : INLog
{
public NLog(Type type)
{
}
}
public class NLog<T> : NLog, INLog<T>
where T : class
{
public NLog()
: base(typeof(T))
{
}
}
}
I register the generic type with this XML code:
<type type="Common.Utils.Logging.INLog[], Common.Utils" mapTo="Common.Utils.Logging.NLog[], Common.Utils" />
Loading this configuration gives me the following container registration:
LifetimeManager: {Microsoft.Practices.Unity.TransientLifetimeManager} LifetimeManagerType: {Name = "TransientLifetimeManager" FullName = "Microsoft.Practices.Unity.TransientLifetimeManager"} MappedToType: {Name = "NLog[]" FullName = "Common.Utils.Logging.NLog[]"} Name: null RegisteredType: {Name = "INLog[]" FullName = "Common.Utils.Logging.INLog[]"}
Instead of the registration I'm expecting, namely:
LifetimeManager: null LifetimeManagerType: {Name = "TransientLifetimeManager" FullName = "Microsoft.Practices.Unity.TransientLifetimeManager"} MappedToType: {Name = "NLog`1" FullName = "Common.Utils.Logging.NLog`1"} Name: null RegisteredType: {Name = "INLog`1" FullName = "Common.Utils.Logging.INLog`1"}
Registering in code works just fine and gives the expected registration:
using (IUnityContainer container = new UnityContainer())
{
container.RegisterType(typeof(INLog<>), typeof(NLog<>));
}
But I can't manage to get it loaded correctly via the configuration file. I guess this has to to with the inheritance of the non-generic types (I)NLog...
Does anyone of you guys know how to fix this?
Best regards, Rick
回答1:
Try this instead of the []
notation
<type type="Common.Utils.Logging.INLog`1, Common.Utils" mapTo="Common.Utils.Logging.NLog`1, Common.Utils" />
Feels awkward, I know, but it should work.
来源:https://stackoverflow.com/questions/18766650/unity-fails-to-load-generic-type-definition-via-xml-configuration