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
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
I guess you should move to:
exception.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
and investigate retrieved attribute for value
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
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.
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;
}