[英]C# class type - How to determine whether it is a standard .net framework class
C#/。net框架
确定类(类型)是否是.net框架提供的类而不是我的任何类或第三方库类的最可靠方法是什么?
我已经测试了一些方法
所有这些“感觉”起来都有些笨拙,尽管它起作用了。
问题:确定这一点的最简单,最可靠的方法是什么?
您可以检查程序集的公钥令牌。 Microsoft(BCL)程序集将具有公共密钥令牌b77a5c561934e089
或b03f5f7f11d50a3a
。 WPF程序集将具有公共密钥令牌31bf3856ad364e35
。
通常,要获取程序集的公钥令牌,可以使用sn.exe
-Tp foo.dll
。 sn.exe
是Windows SDK的一部分,您应该已经拥有。
您可以从程序集的全名(例如typeof(string).Assembly.FullName
)中获取公钥令牌,它只是一个字符串,也可以通过对程序集进行P / Invoke来从程序集中获取原始的公钥令牌字节。 StrongNameTokenFromAssembly 。
从程序集中读取Assembly Company属性[assembly:AssemblyCompany(“ Microsoft Corporation”)]
http://msdn.microsoft.com/en-us/library/y1375e30.aspx
using System;
using System.Reflection;
[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]
namespace CustAttrs1CS {
class DemoClass {
static void Main(string[] args) {
Type clsType = typeof(DemoClass);
// Get the Assembly type to access its metadata.
Assembly assy = clsType.Assembly;
// Iterate through the attributes for the assembly.
foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
// Check for the AssemblyTitle attribute.
if (attr.GetType() == typeof(AssemblyTitleAttribute))
Console.WriteLine("Assembly title is \"{0}\".",
((AssemblyTitleAttribute)attr).Title);
// Check for the AssemblyDescription attribute.
else if (attr.GetType() ==
typeof(AssemblyDescriptionAttribute))
Console.WriteLine("Assembly description is \"{0}\".",
((AssemblyDescriptionAttribute)attr).Description);
// Check for the AssemblyCompany attribute.
else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
Console.WriteLine("Assembly company is {0}.",
((AssemblyCompanyAttribute)attr).Company);
}
}
}
}
一些想法:
在Visual Studio的“解决方案资源管理器”中,展开“引用”,右键单击引用,然后选择“属性”并查看路径,例如:
C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\ System.dll
我猜想C:\\ WINDOWS \\ Microsoft.NET \\ Framework \\ v2.0.50727 \\中的大多数程序集可能都是标准的.NET。
另外,您可以在MSDN库中查找程序集,例如:
http://msdn.microsoft.com/zh-CN/library/system.aspx 。
您说的是基类库吗?
您可以在这里查询: http : //en.wikipedia.org/wiki/Base_Class_Library
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.