Click here to Skip to main content
13,004,870 members (66,395 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 7 Sep 2010

Determining if a type is defined in the .NET Framework

, 7 Sep 2010
Rate this:
Please Sign up or sign in to vote.
There have been several questions on StackOverflow about how to determine if a type is defined in the .NET Framework or is a third-party or custom type.

There have been several questions on StackOverflow about how to determine if a type is defined in the .NET Framework or is a third-party or custom type. Based on the answers provided to these questions, this can be accomplished using some reflection to retrieve the public key token of the

.NET assembly in which the type is defined and compare it to a public key token known to be used by Microsoft to sign the .NET Framework assemblies.

To do this, you can make use of the following extension method:

public static class TypeExtensions
    private static List<byte[]> tokens = new List<byte[]>()  
        new byte[] {0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89}, 
        new byte[] {0x31, 0xbf, 0x38, 0x56, 0xad, 0x36, 0x4e, 0x35},  
        new byte[] {0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a} 
    public static bool IsFrameworkType(this Type type) 
        if (type == null) { throw new ArgumentNullException("type"); } 
        byte[] publicKeyToken = type.Assembly.GetName().GetPublicKeyToken(); 
        return publicKeyToken != null && publicKeyToken.Length == 8 
            && tokens.Contains(publicKeyToken, new ByteArrayEqualityComparer());

The set of public key tokens are valid for all versions of the .NET Framework starting with .NET Framework 2.0. The ByteArrayEqualityComparer class looks like:

public class ByteArrayEqualityComparer : EqualityComparer<byte[]>
    public override bool Equals(byte[] x, byte[] y)
        return x != null && y != null
                    && x.Length == 8 && y.Length == 8 
                    && x[0] == y[0]
                    && x[1] == y[1] 
                    && x[2] == y[2]
                    && x[3] == y[3] 
                    && x[4] == y[4]
                    && x[5] == y[5] 
                    && x[6] == y[6]
                    && x[7] == y[7]; 
    public override int GetHashCode(byte[] obj)
        return obj.GetHashCode();

You would then use this extension method like:

Debug.WriteLine("Is type `string` a .NET Framework type? {0}", 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Scott Dorman
Software Developer (Senior)
United States United States
I am a Microsoft C# MVP, author, speaker, blogger, and software developer. I also created the WP Requests and WinStore Requests sites for Windows Phone and Windows Sotre apps as well as several open source projects.

I've been involved with computers in one way or another for as long as I can remember, but started professionally in 1993. Although my primary focus right now is commercial software applications, I prefer building infrastructure components, reusable shared libraries and helping companies define, develop and automate process and code standards and guidelines.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 7 Sep 2010
Article Copyright 2010 by Scott Dorman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid