如何从System.Type中获取对应的MonoDevelop.Projects.Dom.IType?

发布于 2024-10-09 01:34:01 字数 827 浏览 0 评论 0原文

我正在尝试做问题标题中描述的事情。 为此,我只找到了一种方法,但 id 没有给我任何预期的结果。

我执行以下操作:

System.Type type = ...; //known System.Type instance;
var dom = ProjectDomService.GetAssemblyDom(project.TargetRuntime, type.Assembly.FullName);

之后(如我所料)dom.Types 应该包含所有导出类型的程序集,但它是空的;

有没有办法从Type中获取IType?

UPD:(已解决)

这里的 cod 有助于获取正确的 AssemblyDom 实例,然后按名称获取 IType:

string assemblyName = targetRuntime.AssemblyContext
                        .GetAssemblyNameForVersion(type.Assembly.FullName, targetFramework);

var assemblyLocation = targetRuntime.AssemblyContext
            .GetAssemblyLocation(assemblyName, targetFramework);

var assemblyDom = ProjectDomService.GetAssemblyDom(targetRuntime, assemblyLocation);

var t = assemblyDom.GetType(type.FullName);

I'm trying to do what is descriped in question's title.
To do it i've found only one way but id gives me no expected results.

I do the following:

System.Type type = ...; //known System.Type instance;
var dom = ProjectDomService.GetAssemblyDom(project.TargetRuntime, type.Assembly.FullName);

After that (as i expect) dom.Types should contain all assembly exported typed but it's empty;

Is there any way to get IType from Type?

UPD: (solved)

Here the cod that helps to obtain a correct AssemblyDom instance and then Get IType by name:

string assemblyName = targetRuntime.AssemblyContext
                        .GetAssemblyNameForVersion(type.Assembly.FullName, targetFramework);

var assemblyLocation = targetRuntime.AssemblyContext
            .GetAssemblyLocation(assemblyName, targetFramework);

var assemblyDom = ProjectDomService.GetAssemblyDom(targetRuntime, assemblyLocation);

var t = assemblyDom.GetType(type.FullName);

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

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

发布评论

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

评论(1

若沐 2024-10-16 01:34:01

尽管有参数名称,我认为 GetAssemblyDom 的第二个参数是程序集文件路径,而不是程序集名称。 (即使它可以解析程序集名称,这只适用于目标运行时中的系统程序集或包。)

没有从 System.Type 到 IType 的简单查找的原因是它几乎从不需要。 IType 的目的是避免加载程序集和实际类型,因为它们无法卸载,并且不可能从不同的框架或程序集加载类型。因此,处理通用 IType 的代码永远不应该处理通用类型。

IType 的内存效率也更高 - ProjectDom 程序集数据库具有有助于快速完成代码的索引,并允许延迟加载信息,然后以粒度方式卸载。

Despite the parameter name, I think GetAssemblyDom's second parameter is the assembly file path, not the assembly name. (Even if it can resolve assembly names, that would only be true for system assemblies or packages in the target runtime.)

The reason there is no simple lookup from a System.Type to an IType is that it is almost never needed. The purpose of IType is to avoid loading assemblies and real types, since they can't be unloaded, and it's not possible to load Types from different frameworks or assemblies. Therefore, code that deals with general ITypes should never deal with general Types.

IType is also much more memory efficient - the ProjectDom assembly databases have indices that help make code completion fast, and allow information can be lazily loaded and then unloaded in granular way.

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