I am registering some types in Autofac using named parameters like this
builder.Register<LogRequest>((c, p) =>
{
var param = p.Named<TenantConfigurations>("myparam");
if (param is MyClass)
{
return new LogRequest(param as MyClass);
}
return null;
});
How can i do the same for types that I register with builder.RegisterAssemblyTypes method. Assuming that all types constructor takes same parameter param
Update
This is what i ended up doing
Assembly.GetAssembly(CustomType)
.GetTypes()
.Where(t => t.IsSubclassOf(CustomType) && !t.IsAbstract)
.ToList().ForEach(t =>
{
builder.Register((c, p) =>
{
var param = p.Named<TenantConfigurations>("myparam");
if (param is MyClass)
{
return t.GetConstructor(new Type[] { typeof(TenantConfigurations) }).Invoke(new object[] { config });
}
});
});
Yes, you can easily do that using “WithParameter” extension. Example:
Update:
Also, you can do that by using “LifetimeEvents”. Simple usage example: