如何从System.Type中获取对应的MonoDevelop.Projects.Dom.IType?
我正在尝试做问题标题中描述的事情。 为此,我只找到了一种方法,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管有参数名称,我认为 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.