Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#. Check if Type is .NET Framework, not my own type

Im using reflection to check attributes of the methods.

Going deeper and deeper using reflection and checking classes inheritance.

I need to stop when class is .NET Framework, not my own.

How i can check this ?

Thx

like image 311
Alexander Avatar asked Oct 16 '25 10:10

Alexander


2 Answers

I guess you should move to:

exception.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);

and investigate retrieved attribute for value

like image 114
Uzzy Avatar answered Oct 19 '25 02:10

Uzzy


If you want to check an assembly is published by Microsoft, you could do something like this:

public static bool IsMicrosoftType(Type type)
{
    if (type == null)
        throw new ArgumentNullException("type");

    if (type.Assembly == null)
        return false;

    object[] atts = type.Assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
    if ((atts == null) || (atts.Length == 0))
        return false;

    AssemblyCompanyAttribute aca = (AssemblyCompanyAttribute)atts[0];
    return aca.Company != null && aca.Company.IndexOf("Microsoft Corporation", StringComparison.OrdinalIgnoreCase) >= 0;
}

It's not bullet proof as anyone could add such an AssemblyCompany attribute to a custom assembly, but it's a start. For more secure determination, you would need to check Microsoft's authenticode signature from the assembly, like what's done here: Get timestamp from Authenticode Signed files in .NET

like image 34
Simon Mourier Avatar answered Oct 19 '25 03:10

Simon Mourier