Except generic types which are not instantiated, is pointed out with the link Not everything derives from object @ Mr. Lippert's blog by Mr. Gravell's good answer, I suggest that to find the other types met your requirement can be done by yourself.
Now, let's start from scratch to solve this question. First off, the types your are going to figure out, should be in the core runtime library, and which is mscorlib.dll
:
public static partial class MartinMulderExtensions {
public static IEnumerable<Type> GetMscorlibTypes() {
return
from assembly in AppDomain.CurrentDomain.GetAssemblies()
let name=assembly.ManifestModule.Name
where 0==String.Compare("mscorlib.dll", name, true)
from type in assembly.GetTypes()
select type;
}
}
And then, the types have the MakeXXXXType()
methods such as MakeByRefType()
. Here we take more possibility into account, that is any method which returns a type or types. Since we have zero knowledge about the arguments for an arbitrary type, we consider methods take zero argument:
partial class MartinMulderExtensions {
public static IEnumerable<Type> GetRetrievableTypes(this Type type) {
var typesArray=(
from method in type.GetMethods()
where 0==method.GetParameters().Count()
let typeArray=
method.InvokeZeroArgumentMethodWhichReturnsTypeOrTypes(type)
where null!=typeArray
select typeArray).ToArray();
var types=
typesArray.Length>0
?typesArray.Aggregate(Enumerable.Union)
:Type.EmptyTypes;
return types.Union(new[] { type });
}
}
For the implementation of InvokeZeroArgumentMethodWhichReturnsTypeOrTypes
, however, there are several invalid cases of this kind of invocation, such as invoking GetGenericParameterConstraints()
on a non-generic type; we are avoiding these cases with try-catch:
partial class MartinMulderExtensions {
public static IEnumerable<Type>
InvokeZeroArgumentMethodWhichReturnsTypeOrTypes(
this MethodInfo method, Type t
) {
try {
if(typeof(Type)==method.ReturnType) {
var type=method.Invoke(t, null) as Type;
if(null!=type)
return new[] { type };
}
if(typeof(Type[])==method.ReturnType) {
var types=method.Invoke(t, null) as Type[];
if(types.Length>0)
return types;
}
}
catch(InvalidOperationException) {
}
catch(TargetInvocationException) {
}
catch(TargetException) {
}
return Type.EmptyTypes;
}
}
And now, to figure out the desired types. Let's construct the method step by step. The first step would be define the scope of all possible types:
partial class MartinMulderExtensions {
public static Type[] GetDesiredTypes() {
return (
from type in MartinMulderExtensions.GetMscorlibTypes()
.Select(x => x.GetRetrievableTypes())
.Aggregate(Enumerable.Union)
Then, according to what you stated basically:
Now my question is: are there more such 'exotic' types (besided the ByRef-types and type object
) which are a class (IsClass == true
) but do not have a base type (BaseType == null
)?
where null==type.BaseType
where type.IsClass
You also stated that for before answer
:
Before you answer: I am only refering to types where IsClass == true
! And my example with type int
was just an example. It could have been any type.
So please no:
where !type.IsInterface
where !type.IsValueType
where typeof(void)!=type
The final step, let's skip which are already answered and complete the method:
where !type.IsByRef
where !type.IsPointer
select type
).ToArray();
}
}
So now, you can invoke MartinMulderExtensions.GetDesiredTypes()
to get the types you desired:
public partial class TestClass {
public static void TestMethod() {
foreach(var type in MartinMulderExtensions.GetDesiredTypes())
Console.WriteLine(type);
}
}
For the complete code:
public static partial class MartinMulderExtensions {
public static IEnumerable<Type> GetMscorlibTypes() {
return
from assembly in AppDomain.CurrentDomain.GetAssemblies()
let name=assembly.ManifestModule.Name
where 0==String.Compare("mscorlib.dll", name, true)
from type in assembly.GetTypes()
select type;
}
public static IEnumerable<Type>
InvokeZeroArgumentMethodWhichReturnsTypeOrTypes(
this MethodInfo method, Type t
) {
try {
if(typeof(Type)==method.ReturnType) {
var type=method.Invoke(t, null) as Type;
if(null!=type)
return new[] { type };
}
if(typeof(Type[])==method.ReturnType) {
var types=method.Invoke(t, null) as Type[];
if(types.Length>0)
return types;
}
}
catch(InvalidOperationException) {
}
catch(TargetInvocationException) {
}
catch(TargetException) {
}
return Type.EmptyTypes;
}
public static IEnumerable<Type> GetRetrievableTypes(this Type type) {
var typesArray=(
from method in type.GetMethods()
where 0==method.GetParameters().Count()
let typeArray=
method.InvokeZeroArgumentMethodWhichReturnsTypeOrTypes(type)
where null!=typeArray
select typeArray).ToArray();
var types=
typesArray.Length>0
?typesArray.Aggregate(Enumerable.Union)
:Type.EmptyTypes;
return types.Union(new[] { type });
}
public static Type[] GetDesiredTypes() {
return (
from type in MartinMulderExtensions.GetMscorlibTypes()
.Select(x => x.GetRetrievableTypes())
.Aggregate(Enumerable.Union)
where null==type.BaseType
where type.IsClass
where !type.IsInterface
where !type.IsValueType
where typeof(void)!=type
where !type.IsByRef
where !type.IsPointer
select type
).ToArray();
}
}
void
reference" type.object
. A value type derives fromValueType
andValueType
derives fromobject
.int
is also a struct and the only thing that makesIsClass
true
is theMakeByRefType()
.