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
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);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With