[英]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.