4

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

Alexander
  • 431
  • 2
  • 5
  • 19
  • How do you define your "own" type? – Steve B May 03 '13 at 09:24
  • I meant something like class MyException : Exception. MyException is my own class, Exception is MS's class. – Alexander May 03 '13 at 09:25
  • 4
    `Type.Assembly` might help you here. – AakashM May 03 '13 at 09:28
  • you mean after that - to check if assembly name starts with System ? – Alexander May 03 '13 at 09:31
  • If you're using one defined root namespace (like "MyApp."...) it would be easier to check if it's your assembly. – MatthiasG May 03 '13 at 09:33
  • ok, maybe its the simplest way. So if Type.Assembly wont return my assembly thats means Type.Assembly points on some MS's assembly (i dont use any 3rd parties which could make a mess). Right ? – Alexander May 03 '13 at 09:36
  • @Alexander yes, if your code is all in a single assembly, and `Type.Assembly` returns a different assembly for a given type, that means the type is not one of yours. – AakashM May 08 '13 at 09:36

4 Answers4

2

I guess you should move to:

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

and investigate retrieved attribute for value

Uzzy
  • 550
  • 3
  • 14
2

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

Community
  • 1
  • 1
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
0

this is a little example just to give you a possible solution:

 private static void Main(string[] args)
    {

        var persian = new Persian();

        var type = persian.GetType();

       var location = type.Assembly.Location;

        do
        {
            if (type == null) break;

            Console.WriteLine(type.ToString());

            type = type.BaseType;

        } while (type != typeof(object) && type.Assembly.Location == location);

        Console.ReadLine();

    }
}

class Animal : Dictionary<string,string>
{

}

class Cat : Animal
{

}

class Persian : Cat
{

}

as you can test yourself the program execution will stop at Animal. This program won't cover the case of custom types defined in another assemblies. Hope this gives you the idea on how to proceeed.

codingadventures
  • 2,924
  • 2
  • 19
  • 36
0

If you don't define type converter, then you can check easily. Microsoft define mostly every class define a type converter( StringConverter,DoubleConverter, GuidConverter etc.), so you must check its type converter is default or not. All type converters are intherited by TypeConverter, so you can check exactly Is type of converter TypeConverter.

    public static bool IsUserDefined(Type type)
    {
        var td = TypeDescriptor.GetConverter(type);
        if (td.GetType() == typeof(TypeConverter))
            return true;
        return false;
    }
Doctor
  • 788
  • 9
  • 12