Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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?

like image 317
HorstWalter Avatar asked May 06 '10 01:05

HorstWalter


2 Answers

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.

like image 178
Chris Schmich Avatar answered Sep 21 '22 13:09

Chris Schmich


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);
            }
        }
    }
}
like image 26
Raj Kaimal Avatar answered Sep 24 '22 13:09

Raj Kaimal