gpt4 book ai didi

reflection - Nancy 和抽象基模块类/创建幻像类实例

转载 作者:行者123 更新时间:2023-12-01 10:04:12 25 4
gpt4 key购买 nike

我正在与 Nancy 一起构建一个小型应用程序

我想要一种基类,其他模块可以从中继承。
见下文(这不是抽象类是有原因的,我将在下面解释)

public class ImportModule<T> : NancyModule
{
protected ImportModule()
: this(typeof(T).Name.ToLower())
{
Get["/"] = _ => "need to select an action - xxx";
Get["/importnew"] = _ => ImportNew(); //note - method omitted for brevity
}
}

当我运行我的应用程序时,我得到了

Unable to resolve type: My.NameSpace.TypedImporter`1

作为旁注,如果 ImportModule 类是抽象类,则不会发生这种情况

好的-
现在,我可以有一个这样的类:

public class MyCustomImporter : ImportModule<MyCustomType>
{
//overrides....
}

但是,在其他地方,在“DefaultImportModule”中我有以下内容:

var importerModule = Activator.CreateInstance(typeof(ImportModule<>).MakeGenericType(type));

所以我需要能够根据传入的类型创建一种导入器

(基本上,如果用户点击 url/customer,它会像这样执行与类相同的操作)

public class CustomerImporter : ImporterModule<Customer>
{
}

所以,在我看来,我有两个选择:
1) 停止 nancy 试图映射我的 ImportModule
2) 实例化一个继承自 ImportModule 的“幻象类”,并再次使 ImportModule 抽象

最佳答案

Nancy群里有一个讨论可能和你的问题有关,肯定和Nancy不识别非抽象基础模块有关。以下是 Andreas Håkansson 的原话:

Nancy locates everything that's inheriting NancyModule and assumes it's something it can instantiate. Nancy is aware that it cannot create an instance of an abstract module. So if you have a base-module that's not abstract then Nancy will attempt to new it up. I'd like to think that for 99% of the time, having a non-abstract base class is a broken design.

这里是讨论的链接:Unable to resolve type: Nancy.Routing.DefaultRouteResolver

关于reflection - Nancy 和抽象基模块类/创建幻像类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12426770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com