views:

249

answers:

4

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?

A: 

You say about Base Class Library?

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

Fujiy
+6  A: 

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.

Chris Schmich
Thanks, I have used a combination of 1.) The "Company Attribute" and 2.) "Strongname / token" answer to solve my issues. I appreciate your feedback.
HorstWalter
+2  A: 

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);
            }
        }
    }
}
Raj Kaimal
Thanks, I have used a combination of 1.) The "Company Attribute" and 2.) "Strongname / token" answer to solve my issues. I appreciate your feedback.
HorstWalter
This dont means that is .NET Standart class. Many classes come with .NET but not is Standart. And other .NET implementation like Mono have another Company Name
Fujiy
Yes, you are right. "Standard" meant something specific in my very application context, however the "company attribute" / "token answer" helped to improve my detection method. Academically my definition is not precise / ambiguous.
HorstWalter
A: 

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.

regards ~~ gerry (lowry)

gerryLowry