开发者

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

开发者 https://www.devze.com 2022-12-29 02:28 出处:网络
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.

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, e.g. 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 (e.g. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消