方法 XXX 由于其保护级别而无法访问
method XXX is inaccessible due to its protection level
我有一个奇怪的问题。我使用 Unity 作为 IOC/DI。我想创建 class LoggerService 的实例,但出现错误 'LoggerService.LogException(System.Exception, string, string)' is unaccessible due to its protection level'。我有一些 Unity 使用的 classes 内部访问修饰符(class 没有访问修饰符),并且它们可以在代码中访问。为什么它在这里不起作用?
我的代码如下:
//console app executable module:
static void CommonOnUnhandledException(Exception exception, string messagePrefix)
{
var logger = Ioc.Resolve<LoggerService>(Config);
logger.LogException(exception, String.Empty, String.Empty);
Console.WriteLine(String.Format("{0} unhandled exception: {1}; {2}", messagePrefix, exception.Message, exception.StackTrace));
}
//library Services.dll
class LoggerService : ILoggerService
{
public LoggerService(AppConfiguration configuration)
{
...
}
public void LogException(Exception ex, string url = "", string messageContent = "")
{
...
}
}
//library services dll, started in console app
public static class ServicesModule
{
public static void Init(UnityContainer container)
{
// register all other ordinary types:
container.RegisterType<IIntelShopService, IntelShopService>(new ContainerControlledLifetimeManager());
container.RegisterType<IBackendService, BackendService>(new ContainerControlledLifetimeManager());
container.RegisterType<IIntelODataService, IntelODataService>(new ContainerControlledLifetimeManager());
container.RegisterType<ILoggerService, Log4NetLoggerService>(new ContainerControlledLifetimeManager());
container.RegisterType<IPerformanceTrackerService, PerformanceTrackerService>(new ContainerControlledLifetimeManager());
}
}
你必须classpublic
。如果没有修饰符,默认值为 internal
,这 "visible" 不足以满足您的目的。另见 here。
你必须class public
。始终对方法、属性和 classes 使用修饰符很有用。
您可以尝试解决 ILoggerService
而不是特定的实现。
如果您解析具体类型而不是接口,那么通常您只使用了控制的统一或依赖性 injection/inversion 一半的力量。
顺便说一句:实现 应该 大多数时候是 internal
,而另一方面接口往往是 public
.
我有一个奇怪的问题。我使用 Unity 作为 IOC/DI。我想创建 class LoggerService 的实例,但出现错误 'LoggerService.LogException(System.Exception, string, string)' is unaccessible due to its protection level'。我有一些 Unity 使用的 classes 内部访问修饰符(class 没有访问修饰符),并且它们可以在代码中访问。为什么它在这里不起作用?
我的代码如下:
//console app executable module:
static void CommonOnUnhandledException(Exception exception, string messagePrefix)
{
var logger = Ioc.Resolve<LoggerService>(Config);
logger.LogException(exception, String.Empty, String.Empty);
Console.WriteLine(String.Format("{0} unhandled exception: {1}; {2}", messagePrefix, exception.Message, exception.StackTrace));
}
//library Services.dll
class LoggerService : ILoggerService
{
public LoggerService(AppConfiguration configuration)
{
...
}
public void LogException(Exception ex, string url = "", string messageContent = "")
{
...
}
}
//library services dll, started in console app
public static class ServicesModule
{
public static void Init(UnityContainer container)
{
// register all other ordinary types:
container.RegisterType<IIntelShopService, IntelShopService>(new ContainerControlledLifetimeManager());
container.RegisterType<IBackendService, BackendService>(new ContainerControlledLifetimeManager());
container.RegisterType<IIntelODataService, IntelODataService>(new ContainerControlledLifetimeManager());
container.RegisterType<ILoggerService, Log4NetLoggerService>(new ContainerControlledLifetimeManager());
container.RegisterType<IPerformanceTrackerService, PerformanceTrackerService>(new ContainerControlledLifetimeManager());
}
}
你必须classpublic
。如果没有修饰符,默认值为 internal
,这 "visible" 不足以满足您的目的。另见 here。
你必须class public
。始终对方法、属性和 classes 使用修饰符很有用。
您可以尝试解决 ILoggerService
而不是特定的实现。
如果您解析具体类型而不是接口,那么通常您只使用了控制的统一或依赖性 injection/inversion 一半的力量。
顺便说一句:实现 应该 大多数时候是 internal
,而另一方面接口往往是 public
.