public static IEnumerable<Type> GetAccessibleTypes(this Assembly assembly)
{
try
{
#if NET40
return assembly.GetTypes();
#else
return assembly.DefinedTypes.Select(t => t.AsType());
#endif
}
catch (ReflectionTypeLoadException ex)
{
// The exception is thrown if some types cannot be loaded in partial trust.
// For our purposes we just want to get the types that are loaded, which are
// provided in the Types property of the exception.
return ex.Types.Where(t => t != null);
}
}
What are differences between assembly.GetTypes()
and assembly.DefinedTypes.Select(t => t.AsType())
?
Original code:
https://entityframework.codeplex.com/SourceControl/latest#src/Common/AssemblyExtensions.cs
0
From the MSDN documentation: “The DefinedTypes
property is comparable to the Assembly.GetTypes
method, except that the DefinedTypes
property returns a collection of TypeInfo
objects, and the Assembly.GetTypes
method returns an array of Type
objects.”
Not terribly useful at first sight. Looking further into the documentation on TypeInfo
is much more informative: “A TypeInfo
object represents the type definition itself, whereas a Type
object represents a reference to the type definition. Getting a TypeInfo
object forces the assembly that contains that type to load. In comparison, you can manipulate Type
objects without necessarily requiring the runtime to load the assembly they reference.”
The reason for this is outlined in a microsoft blog post, outlining the changes to the reflection API introduced in .NET 4.5. The main difference is that the Type
now no longer necessarily forces the loading of the type, and works much more as a slim handle into the type metadata, hence making introspection much lighter.
At a minimum, the returned objects are different types, with one being an array of Type
s and the other being a generic IQueryable<Type>
.
Here is the code from Assembly.GetTypes()
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine | ResourceScope.Assembly, ResourceScope.Machine | ResourceScope.Assembly)]
public virtual Type[] GetTypes()
{
Module[] m = GetModules(false);
int iNumModules = m.Length;
int iFinalLength = 0;
Type[][] ModuleTypes = new Type[iNumModules][];
for (int i = 0; i < iNumModules; i++)
{
ModuleTypes[i] = m[i].GetTypes();
iFinalLength += ModuleTypes[i].Length;
}
int iCurrent = 0;
Type[] ret = new Type[iFinalLength];
for (int i = 0; i < iNumModules; i++)
{
int iLength = ModuleTypes[i].Length;
Array.Copy(ModuleTypes[i], 0, ret, iCurrent, iLength);
iCurrent += iLength;
}
return ret;
}
1