C # class type - How to determine if it is a standard .net environment class

C # /. Net framework

What is the most reliable way to determine if a class (type) is a class provided by the .net framework, and not classes of classes or third-party libraries.

I checked several approaches

  • Namespace, for example. starting with System.
  • Assembly code base where the dll is located

All this β€œfeels” a little awkwardly, although it works.

Question: What is the easiest and most reliable way to determine this?

+4
source share
4 answers

Read the assembly company attribute from assembly [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); } } } } 
+2
source

You can check the public key token of the assembly. Microsoft builds (BCL) will install the public key token b77a5c561934e089 or b03f5f7f11d50a3a . In WPF builds, the public key token 31bf3856ad364e35 will be installed.

In general, to get the assembly public key token, you can use sn.exe -Tp foo.dll . sn.exe is part of the Windows SDK that you should already have.

You can get the public key token from the fully typeof(string).Assembly.FullName name of the assembly (e.g. typeof(string).Assembly.FullName ), which is just a string, or you can get the raw byte of the public key token from the assembly by running P / Invoke in StrongNameTokenFromAssembly .

+6
source

A few ideas:

In Visual Studio, in Solution Explorer, expand the link, right-click the link, then select "Properties" and look at the path, for example:
C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ System.dll

I assume that most assemblies in C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ are likely to be standard .NET.

In addition, you can see the assembly in the MSDN library, for example:
http://msdn.microsoft.com/en-us/library/system.aspx .

0
source

Are you talking about a base class library?

You can consult here: http://en.wikipedia.org/wiki/Base_Class_Library

-2
source

Source: https://habr.com/ru/post/1308953/


All Articles