如何获得大会(System.Reflection.Assembly)给定类型在.net中?

编程入门 行业动态 更新时间:2024-10-26 18:16:55
本文介绍了如何获得大会(System.Reflection.Assembly)给定类型在中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在.NET中,给定类型名称,有没有一种方法,告诉我哪个组件(System.Reflection.Assembly的实例)在该类型被定义?

In .Net, given a type name, is there a method that tells me in which assembly (instance of System.Reflection.Assembly) that type is defined?

我认为我的项目已经有一个引用该程序集,只需要知道它是哪一个。

I assume that my project already has a reference to that assembly, just need to know which one it is.

推荐答案

Assembly.GetAssembly假设你有类型的实例,并Type.GetType假设你有其中包括集名称完全限定的类型名称。

Assembly.GetAssembly assumes you have an instance of the type, and Type.GetType assumes you have the fully qualified type name which includes assembly name.

如果你只拥有基本类型的名字,你需要做更多的东西是这样的:

If you only have the base type name, you need to do something more like this:

public static String GetAssemblyNameContainingType(String typeName) { foreach (Assembly currentassembly in AppDomain.CurrentDomain.GetAssemblies()) { Type t = currentassembly.GetType(typeName, false, true); if (t != null) {return currentassembly.FullName;} } return "not found"; }

这也假定您的类型根声明。您需要提供名字的命名空间或封闭类型,或者重复同样的方式。

This also assumes your type is declared in the root. You would need to provide the namespace or enclosing types in the name, or iterate in the same manner.

更多推荐

如何获得大会(System.Reflection.Assembly)给定类型在.net中?

本文发布于:2023-11-12 13:43:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   大会   类型   Reflection   System

发布评论

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

>www.elefans.com

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