I want to distinctly determine if the type that I have is of custom class type (MyClass) or one provided by the Framework (System.String).
Is there any way in reflection that I can distinguish my class type from system.string or other Framework provided types?
The only way to safely check if a type is part of an assembly is to check the assembly's fully qualified name which contains its name, version, culture and public key (if signed). All .Net base class libraries (BCL) are signed by microsoft using their private keys. This makes it almost impossible for anyone else to create an assembly with same fully qualified name as a base class library.
A slightly less brittle solution is to use the AssemblyName class and skip the version number/culture check. This of course assumes the public key doesn't change between versions.
Most of the BCL have been signed with the same key but not all. You could use the AssemblyName class to just check the public key token. It depends on your needs.
Not all framework classes start in the System namespace (they can also be Microsoft etc).
As such, you could probably compare the location of a known framework class with that of the type that you are testing such as:
Not the greatest solution; but safer than just testing if the namespace starts with System (I could create a namespace that starts with System that isn't a framework class).
Edit
Also, in addition to the above test, it wouldn't hurt to verify that the type is loaded from the Global Assembly Cache:
You can check the Assembly in which the type is declared.
If you simply want to distinguish between
MyClass
andstring
then you can check for those types directly:If you need a more general check for whether or not a given type is a framework type then you could check whether it belongs to the
System
namespace:Check if the assembly belongs to the CLR library:
as explained here.