【发布时间】:2012-11-08 15:26:45
【问题描述】:
我正在尝试使用 Autofac 并注册下面的类,该类接收参数之一作为可选参数(或者更确切地说是 null)。 我的课程是:
class BaseSpanRecord : ISpanRecord
{
public BaseSpanRecord(RecordType recordType, List<SpanRecordAttribute> properties)
{
RecordType = recordType;
Properties = properties;
}
}
这里 RecordType 是一个枚举,而 SpanRecordAttribute 是一个只有属性的类,我不想为其创建任何接口。
构造函数中RecordType和Properties是接口ISpanRecord的两个公共属性 这个类可以在程序的不同地方通过以下方式实例化:
ISpanRecord spanFileRecord = new BaseSpanRecord(recordType, null);
或
ISpanRecord spanFileRecord = new BaseSpanRecord(recordType, recordAttributeList);
我应该如何尝试在 Autofac 容器中注册它以便它可以处理上述两种情况? 或者我应该改变 BaseSpanRecord 类的编写方式以使其注册更容易?
【问题讨论】:
-
我尝试使用以下方法:
builder.RegisterType<BaseSpanRecord>().UsingConstructor(typeof(RecordType), typeof(List<SpanRecordAttribute>)).As<ISpanRecord>().InstancePerDependency();但是它不适用于第一种情况。