我有这门课:
namespace Ns1.Ns2.Domain
{
public class Process
{
private IIndex IndexBuilderConcr { get; set; }
public Processo(String processType) {
IndexBuilderConcr = new UnityContainer().RegisterType<IIndex, *>().Resolve<IIndex>();
}
}
}
这里我有一个采用String
的构造函数。该字符串表示应替换第 8 行 * 符号的类型。
我用谷歌搜索了一下,但没有运气。
最佳答案
您需要做的是按照 James S 建议的方式获取类型,但是您需要以与调用 Method<resolvedProcessType>
略有不同的方式将该值传递到方法中。无效:
var type = Type.GetType("Some.Name.Space." + processType);
var methodInfo = typeof(UnityContainer).GetMethod("RegisterType");
// this method's argument is params Type[] so just keep adding commas
// for each <,,,>
var method = methodInfo.MakeGenericMethod(IIndex, type);
// we supply null as the second argument because the method has no parameters
unityContainer = (UnityContainer)method.Invoke(unityContainer, null);
unityContainer.Resolve<IIndex>();
关于c# - 从字符串中检索类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23063945/