2010-05-06 121 views
4

C#/。NET框架C#類類型 - 如何確定它是否是一個標準的.NET Framework類

什麼是最可靠的方法來確定一個類(類型)是由.NET提供的類框架,而不是我的任何類或第三方庫類。

我已經測試的一些方法

  • 的命名空間,例如從「系統」開始。
  • 組裝,其中DLL位於

這一切「感覺」的代碼庫有一點笨拙,雖然它的工作原理。

問題:什麼是最簡單和最可靠的方法來確定這一點?

+1

您可能想要首先定義「標準」框架類的含義。如果你真的可以回答這個問題,其餘的應該很容易... – Aaronaught 2010-05-06 01:43:51

+0

我已經在下面添加了一條評論。感謝指出。 – HorstWalter 2010-05-06 12:45:54

回答

2

閱讀從組件 組裝企業屬性[大會:AssemblyCompany( 「微軟公司」)

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); 
      } 
     } 
    } 
} 
+0

謝謝,我使用了 的組合。)「公司屬性」和 2.)「Strongname/token」回答 解決我的問題。感謝您的反饋。 – HorstWalter 2010-05-06 12:12:46

+0

這不意味着是.NET Standart類。 .NET有許多類,但不是Standart。而像Mono這樣的其他.NET實現有另一個公司名稱 – 2010-05-06 12:17:34

+0

是的,你是對的。 「標準」意味着我的應用環境中特定的東西,然而「公司屬性」/「標記答案」有助於改進我的檢測方法。在學術上我的定義並不精確/不明確。 – HorstWalter 2010-05-06 12:43:56

6

你可以檢查程序集的公鑰標記。 Microsoft(BCL)程序集將擁有公鑰令牌b77a5c561934e089b03f5f7f11d50a3a。 WPF程序集將擁有公鑰令牌31bf3856ad364e35

通常,要獲得程序集的公鑰標記,可以使用sn.exe-Tp foo.dllsn.exe是Windows SDK的一部分,您應該已擁有該SDK。

你可以從組件的全名公鑰標記(例如typeof(string).Assembly.FullName),這僅僅是一個字符串,或者您可以通過在P獲得從組件的原始公鑰標記字節/調用到StrongNameTokenFromAssembly

+0

謝謝,我已經使用了 的組合1.)「公司屬性」和 2。)「Strongname/token」回答 解決我的問題。感謝您的反饋。 – HorstWalter 2010-05-06 12:02:31

0

一對夫婦的想法:

在Visual Studio中,內解決方案資源管理器,展開引用,右鍵單擊引用,然後選擇屬性並查看路徑,例如:
C:\ WINDOWS \ Microsoft.NET \ F ramework \ v2.0.50727 \ System.dll

我猜測C:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \中的大多數程序集很可能是標準的.NET。另外,您可以在MSDN庫中查找程序集,例如:
http://msdn.microsoft.com/en-us/library/system.aspx

相關問題