简体   繁体   中英

C# class type - How to determine whether it is a standard .net framework class

C# / .net framework

What is the most reliable way to determine whether a class (type) is a class provided by the .net framework and not any of my classes or 3rd party library classes.

I have tested some approaches

  • The namespace, eg starting with "System."
  • The Codebase of the assembly, where the dll is located

All this "feels" a little clumsy though it works.

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

You could check the assembly's public key token. Microsoft (BCL) assemblies will have the public key token b77a5c561934e089 or b03f5f7f11d50a3a . WPF assemblies will have the public key token 31bf3856ad364e35 .

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

You can get the public key token from the assembly's full name (eg typeof(string).Assembly.FullName ), which is just a string, or you can get the raw public key token bytes from the assembly by doing a P/Invoke into StrongNameTokenFromAssembly .

Read the Assembly Company Attribute from the 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);
            }
        }
    }
}

A couple of ideas:

In Visual Studio, within Solution Explorer, expand References, right click a reference, then choose Properties and look at the path, example:
C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\System.dll

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

Also, you could look up the assembly in MSDN library, example:
http://msdn.microsoft.com/en-us/library/system.aspx .

You say about Base Class Library?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM