步行类型有反射问题

发布于 2025-01-01 11:45:14 字数 682 浏览 1 评论 0原文

因此,尝试遍历类型来寻找具有接口的类。

方法:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach(...)
var types=assembly.GetTypes();

异常:

{“无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。”}

LoaderExceptions 的内容: {“程序集 'namespaceInfo.Importer,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null' 中类型 'namespaceinfo.ImportR' 中的方法 'Do' 没有实现。":"nameSpaceInfo.ImportR"}

发现信息到目前为止,SO -

  • .Net 按需加载类型/程序集。也许依赖项尚未加载。

  • ...现在就这样。

在花了几个小时之后,我开始认为,在某些情况下,用属性装饰类,然后出于某些原因遍历它们可能是一个坏主意。显然其他服务成功地做到了这一点。我缺少什么?有像 Fluent 这样的智能实现值得我看看吗?


到目前为止的答案是不要在一个特定的程序集中遍历类型。 IE。过滤掉它

So, attempting to walk types looking for those classes with an interface.

Methods:

var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach(...)
var types=assembly.GetTypes();

Exception:

{"Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."}

Content of LoaderExceptions:
{"Method 'Do' in type 'namespaceinfo.ImportR' from assembly 'namespaceInfo.Importer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.":"nameSpaceInfo.ImportR"}

Info discovered so far on SO --

  • .Net loads in types / assemblies on demand. Perhaps a dependency hasn't been loaded yet.

  • ... that's about it for now.

After spending several hours on this, I'm starting to think that decorating classes with attributes and then walking them afterwards for certain reasons could be a bad idea in some situations. Clearly other services do this successfully. What am I missing? Is there a smart implementation in something like Fluent that I should take a look at?


The answer so far is do not walk the types in one particular assembly. ie. filter it out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2025-01-08 11:45:14

我会亲自深入研究 StructureMap 或其他依赖注入容器内的代码。

I would dig into the code inside of StructureMap or another Dependency Injection container, personally.

不奢求什么 2025-01-08 11:45:14

看起来您正在尝试在运行时加载特定接口的实现。大概是为了创建实例并在您的应用程序中使用它们。如果是这样,那么我建议使用像 MEF 这样的框架来代替手动滚动,它的设计正是为了做到这一点。

It seems like you are trying to load implementations of a specific interface at runtime. Presumably to create instances and use them within your application. If so then instead of hand rolling this I would suggest using a framework like MEF which is designed to do exactly this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文