Gets a collection of loadable types from the given assembly. Adapted from - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Gets a collection of loadable types from the given assembly. Adapted from

Demo Code

//   Licensed under the Apache License, Version 2.0.
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//w  w w  .  j a  va 2  s.  c om

public class Main{
        /// <summary>
        /// Gets a collection of loadable types from the given assembly.
        /// Adapted from <see href="http://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes"/>
        /// </summary>
        /// <param name="assembly">
        /// The <see cref="System.Reflection.Assembly"/> to load the types from.
        /// </param>
        /// <returns>
        /// The loadable <see cref="System.Collections.Generic.IEnumerable{Type}"/>.
        /// </returns>
        public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }

            try
            {
                return assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException ex)
            {
                return ex.Types.Where(t => t != null);
            }
        }
}

Related Tutorials