获取装配类型名称而不加载装配

编程入门 行业动态 更新时间:2024-10-24 02:35:17
本文介绍了获取装配类型名称而不加载装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有人知道如何在没有 实际将程序集加载到您的应用程序域中的情况下获取程序集中的类型列表?

Does anyone know how to get a list of the types in an Assembly without actually loading the Assembly into your app domain?

推荐答案

有没有人知道如何在没有实际将程序集加载到您的应用程序域中的情况下获取程序集中的类型列表? Does anyone know how to get a list of the types in an Assembly withoutactually loading the Assembly into your app domain?

是的,通过使用反射之外的元数据读取库。 Microsoft提供了一个COM API,还有一些其他可用(带 C#源代码)在线。 Mattias - Mattias Sj?gren [MVP] mattias @ mvps www.msjogren/dotnet/ | www.dotnetinterop 请回复到新闻组。

Yes, by using a metadata reading library other than Reflection. Microsoft provides a COM API, and there are some other available (with C# source code) online. Mattias -- Mattias Sj?gren [MVP] mattias @ mvps www.msjogren/dotnet/ | www.dotnetinterop Please reply only to the newsgroup.

Hello Neo! 您可以使用Type.GetType()进行操作。但在进入 编程世界之前,你需要记住以下几点; 1)如果你使用的是Type.GetType(< YOUR_TYPE> )"即没有程序集 指定的运行时只会在调用程序集中查找类型 然后在mscorlib.dll中查找。 2)如果你有一个弱组件,即没有强名称..对于运行时 在任何其他组件中寻找你的类型,你需要拨打电话这样的 as,Type.GetType(< Your_Type>,< Your_Assembly>);例如,如果 你有一个类型 " MyTestLib.MyClass"在程序集MyTestLib中那么你需要像这样的Type.GetType(MyTestLib.MyClass,MyTestLib);进行 调用。原因是 简单,部分类型标识是程序集。 3)对于强名称程序集,您需要在 格式如下:"< Your_Type>,< Assembly_Which_Contains_Your_Type>, < Assembly_Version>,< Assembly_Culture>,< Public_Key_Token>,< Custom> 。 为了您的方便,下面是一个加载 System.Windows.Forms.Shortcut的示例。键入然后在控制台上打印 所有字段名称。 < code> private String SHORTCUT_TYPE_STRING =" System.Windows.Forms.Shortcut, System.Windows.Forms,Version = 1.0.5000.0,Culture = neutral, PublicKeyToken = b77a5c561934e089,Custom = null" ;; 类型type = Type.GetType(this.SHORTCUT_TYPE_STRING); if(type.IsEnum) { FieldInfo [] fields = type.GetFields(); foreach(字段中的FieldInfo字段) { if(field.FieldType.Name ==" Shortcut") { Console.Writeline(field.Name); } } } < / code> 注意:但是你需要记住组装会得到加载 但它对你来说是透明的。 你明确地说WONT需要使用3 静态方法中的任何一个加载程序集装配类型,即Assembly.Load()& QUOT;或者 " Assembly.LoadFrom()"或Assembly.LoadFile()或然后使用加载的 程序集引用获取所需的类型信息, < Loaded_Assembly_Reference> .GetType(< Your_Type>); 希望这可能对您有所帮助。 如有任何不符之处,请通知我。 祝您有个美好的一天。 问候, Moiz Uddin Shaikh 软件工程师 Kalsoft( Pvt)Ltd Hello Neo ! You can do it with "Type.GetType()" but before diving into the programming world you need to remember following points; 1) If you are using "Type.GetType(<YOUR_TYPE>)" i.e. no assembly is specified runtime will only look for the type in the calling assembly and then in mscorlib.dll. 2) If you have a weak assembly i.e. NOT Strong-Named .. for runtime to look for your type in any other assembly, you need to make a call such as, Type.GetType(<Your_Type>, <Your_Assembly>); i.e. for example if you have a type "MyTestLib.MyClass" in an assembly "MyTestLib" then you need to make a call like this Type.GetType("MyTestLib.MyClass, MyTestLib");. Reason is simple, "part of type identity is the assembly". 3) For Strong-Named assembly you need to specify the information in the following format: "<Your_Type>, <Assembly_Which_Contains_Your_Type>, <Assembly_Version>, <Assembly_Culture>, <Public_Key_Token>, <Custom>". For your convenience, below is an example that loads the "System.Windows.Forms.Shortcut" type and then prints on the console all the fields name. <code> private String SHORTCUT_TYPE_STRING = "System.Windows.Forms.Shortcut, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null"; Type type = Type.GetType(this.SHORTCUT_TYPE_STRING); if(type.IsEnum) { FieldInfo[] fields = type.GetFields(); foreach(FieldInfo field in fields) { if(field.FieldType.Name=="Shortcut") { Console.Writeline(field.Name); } } } </code> NOTE: But you need to remember onething that assembly WILL GET LOADED but it will be transparent to you. You explicitly WONT need to load the assembly by using any of the 3 static methods of Assembly type i.e. "Assembly.Load()" or "Assembly.LoadFrom()" or "Assembly.LoadFile()" and then with the loaded assembly reference get the desired type information as, <Loaded_Assembly_Reference>.GetType(<Your_Type>) ; Hope this might be of some help. Let me know in case of any inconsistancy. Have a good day. Regards, Moiz Uddin Shaikh Software Engineer Kalsoft (Pvt) Ltd

谢谢,但所有这些方法都会加载程序集。我不会为我工作。很多时候我正在加载多个版本/版本的 相同的程序集导致崩溃,这些程序集永远不会在构建时链接起来。 但是我需要的只是我不需要执行的类型名称 任何代码。 Thank you but all of these methods will load the assembly. I This won''t work for me. A lot of times I am loading multiple versions/builds of the same assembly which causes a crash these assemblies are never linked in at build time. However all I need from them is the type names I don''t need to execute any code.

更多推荐

获取装配类型名称而不加载装配

本文发布于:2023-11-13 10:35:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   加载   名称   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!