Can't get value of Ninject ConstructorArgument (passed in as parameter to kernel.Get)

前端 未结 2 1712
广开言路
广开言路 2021-01-26 10:50

Having trouble getting the value of a ConstructorArgument parameter passed to kernel.Get(). I want to use the parameter\'s value to determine which of two string values will

2条回答
  •  梦毁少年i
    2021-01-26 11:34

    I needed to use ToMethod() to get access to the appropriate context.

    Bind().ToMethod(
        ctx => {
            var idNum = ctx.Parameters.First(p => p.Name == "idNum")
                .GetValue(ctx, null) as string
            ;
    
            if (idNum == 2) {
                return new DBProviderB(new EF_DBEntities(
                    ConfigurationManager.ConnectionStrings["EF_DB_b_conn1"]
                .ToString()));
            } else {
                return new DBProviderB(new EF_DBEntities(
                    ConfigurationManager.ConnectionStrings["EF_DB_b_conn2"]
                .ToString()));
            }
        };
    );
    
    //later...
    var db = kernel.Get(
        new Ninject.Parameters.Parameter("idNum", idNum, true)
    );
    

提交回复
热议问题