如何使用类型没有NO PARAMETER构造函数的容器注册类型.
实际上我的构造函数接受一个字符串,我通常传入一个表示Path的字符串.
所以当我解决它时会自动创建新类型但是传入一个字符串?
Autofac自动生成工厂Func<T>; 我甚至可以传递参数.
public class MyClass
{
public MyClass(Func<A> a, Func<int, B> b)
{
var _a = a();
var _b = b(1);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以和Ninject一样吗?如果没有,我可以申请哪种解决方法?
谢谢.
更新:
刚发现这篇帖子,似乎答案是否定的:
如何使用Ninject处理带静态方法的类?
也就是说,在C#中,一个接口中不能有静态方法,而Ninject在使用接口的基础上工作?
我的用例是一个类,我希望它有一个静态方法来创建一个未填充的自身实例.
编辑1
只是在TopologyImp类中添加一个示例,在GetRootNodes()方法中,如何创建一些要返回的iNode类?我会用正常的代码练习构建这些或者我会以某种方式使用Ninject吗?但是,如果我使用容器创建那么我没有给IOC这个库知识呢?
public interface ITopology
{
List<INode> GetRootNodes();
}
public class TopologyImp : ITopology
{
public List<INode> GetRootNodes()
{
List<INode> result = new List<INode>();
// Need code here to create some instances, but how to without knowledge of the container?
// e.g. want to create a few INode instances and add them to the list and then return the list
}
}
public interface INode
{
// Parameters
long Id { get; set; }
string Name { …Run Code Online (Sandbox Code Playgroud)