Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Autofac进行属性注入_C#_Autofac Configuration - Fatal编程技术网

C# 无法使用Autofac进行属性注入

C# 无法使用Autofac进行属性注入,c#,autofac-configuration,C#,Autofac Configuration,我是autofac的新手。我正在尝试使用这个IoC容器进行属性注入。下面是我的代码。我得到一个错误: 对象引用未设置为对象的实例 在这一行:。 return_salary.employeeSalary;在GetSalaryint employeeId方法中。在我甚至尝试过的程序类中,build.RegisterType.WithProperty_salary,new salary{employeeId=5,employeeSalary=500} 您需要像下面这样定义您的属性 public ISa

我是autofac的新手。我正在尝试使用这个IoC容器进行属性注入。下面是我的代码。我得到一个错误:

对象引用未设置为对象的实例

在这一行:。 return_salary.employeeSalary;在GetSalaryint employeeId方法中。在我甚至尝试过的程序类中,build.RegisterType.WithProperty_salary,new salary{employeeId=5,employeeSalary=500}


您需要像下面这样定义您的属性

public ISalary _salary { get; set; }
那么下面的代码将适用于您

var build = new ContainerBuilder();
// build.RegisterType<Salary>().As<ISalary>();
build.RegisterType<Salary>().As<ISalary>();
build.RegisterType<Employee>().As<IEmployee>().PropertiesAutowired();
var container = build.Build();
IEmployee employee = container.Resolve<IEmployee>();

Console.WriteLine(employee.GetSalary(5));
Console.ReadLine();

在Employee中创建构造函数

    public Employee(ISalary salary)
    {
        _salary = salary;
    }
这样登记员工

build.RegisterType<Employee>().As<IEmployee>()
            .WithParameter(new TypedParameter(typeof(ISalary), "salary"));
var employee = container.Resolve<IEmployee>(new NamedParameter("salary",
                new Salary { employeeId = 5, employeeSalary = 500 }));
像这样解决它

build.RegisterType<Employee>().As<IEmployee>()
            .WithParameter(new TypedParameter(typeof(ISalary), "salary"));
var employee = container.Resolve<IEmployee>(new NamedParameter("salary",
                new Salary { employeeId = 5, employeeSalary = 500 }));
备选案文1: 为了让属性注入与属性一起工作,请注意_salary是一个字段,以及需要将其配置为属性的内容

除@ehasanul hoque外,您还可以将_salary字段修改为private,并添加公共属性,如下图所示,以了解更多详细信息:

private ISalary _salary;

public ISalary Salary
{
    get
    {
        return this._salary;
    }
    set
    {
        _salary = this.value;
    }
}
备选案文2: 如果您仍然不想将其转换为属性,可以使用方法注入,方法是添加一个SetSalary方法,如:

public class Employee: IEmployee
{
    private ISalary _salary;

    public void SetSalary(ISalary salary)
    {
        this._salary = salary;
    }
}
建筑商可能看起来像:

var builder = new ContainerBuilder();
builder.RegisterType<Salary>().As<ISalary>();
builder.RegisterType<Employee>()
    .OnActivated(IActivatedEventArgs<Employee> e) =>
    {
        var salary = e.Context.Resolve<ISalary>();
        e.Instance.SetSalary(salary);
    });

var container = build.Build();
Employee employee = container.Resolve<Employee>();

我给你小费_工资不是财产@SilentTremor属性注入肯定会在Autofac中工作,我已经使用过很多次了。不,公共收入;这不是财产。这是一个领域。这不是争论的焦点。这不是财产,嗨,普拉善。谢谢你的建议。我已经实现了构造函数注入。我想通过财产注入来做到这一点。