C# · 12月 30, 2021

c# – 自动填充:注册组件并根据解析父项进行解析

我想要注册一个组件以根据它可能解析的类来解析参数. (这听起来有点混乱,所以我举一个例子).

这是一个使用记录器的对象:

class MyObject : IMyObject{ public ILogger Logger; public MyObject(ILogger logger) { Logger = logger; }}

现在在Could中传递的记录器不同于类到类.所以我有一个相当修补的想法,如何做到以下:

class MyLogger : ILogger{ public string Name{get; protected set;} public static ILogger GetLogger(string className) { Name = className; MyLogger logger; // Do something to choose a logger for that specific class return logger; }}

所以当我注册Logger我想能够告诉它的className.我希望有一个类似这样的方法:

ContainerBuilder builder = new ContainerBuilder();builder.Register<MyLogger>(ctx => { string className = //Get resolving class name somehow; return MyLogger.GetLogger(className); }).As<ILogger>();builder.Register<MyObject>().As<IMyObject>();var container = builder.Build();IMyObject myObj = container.Resolve<IMyObject>();//myObject.Logger.Name should Now == “MyObject”

我想这样做的原因是为了避免使用代码中的autofac在日志记录器中注册每个类.我想要能够注册xml中的所有对象,并且只需要一个LoggerModule来添加这个注册.

提前致谢!

解决方法 这是我做的(ILog只是我自己的包装在log4net周围): public class LoggingModule : Module{ protected override void Load(ContainerBuilder builder) { builder.Register((c,p) => GetLogger(p.TypedAs<Type>())); } protected override void AttachToComponentRegistration( IComponentRegistry registry,IComponentRegistration registration) { registration.Preparing += (sender,args) => { var forType = args.Component.Activator.LimitType; var logParameter = new ResolvedParameter( (p,c) => p.ParameterType == typeof (ILog),(p,c) => c.Resolve<ILog>(TypedParameter.From(forType))); args.Parameters = args.Parameters.Union(new[] {logParameter}); }; } public static ILog GetLogger(Type type) { return new Log4NetLogger(type); }}public interface ILog{ void Debug(string format,params object[] args); void Info(string format,params object[] args); void Warn(string format,params object[] args); void Error(string format,params object[] args); void Error(Exception ex); void Error(Exception ex,string format,params object[] args); void Fatal(Exception ex,params object[] args);}public class Log4NetLogger : ILog{ private readonly log4net.ILog _log; static Log4NetLogger() { XmlConfigurator.Configure(); } public Log4NetLogger(Type type) { _log = LogManager.GetLogger(type); } public void Debug(string format,params object[] args) { _log.DebugFormat(format,args); } public void Info(string format,params object[] args) { _log.InfoFormat(format,args); } public void Warn(string format,params object[] args) { _log.WarnFormat(format,args); } public void Error(string format,params object[] args) { _log.ErrorFormat(format,args); } public void Error(Exception ex) { _log.Error(“”,ex); } public void Error(Exception ex,params object[] args) { _log.Error(string.Format(format,args),ex); } public void Fatal(Exception ex,params object[] args) { _log.Fatal(string.Format(format,ex); }}