Loading Assembly’s exported Types

by fkollmann 8/6/2009 3:04:35 PM

Simple and clean…

public class ExportedTypesLoader : IDisposable
{
    private AppDomain _domain;
    private IEnumerable<Type> _types;

    public IEnumerable<Type> ExportedTypes
    {
        get { return _types; }
    }

    public Type FindType(string fullName)
    {

        if (string.IsNullOrEmpty(fullName))
            throw new ArgumentException("null or empty", "fullName");

        return (
            from t in _types
            where t.FullName == fullName
            select t
            ).FirstOrDefault();
    }

    public void Dispose()
    {
        if (_domain != null)
        {
            AppDomain.Unload(_domain);
        }
    }

    public ExportedTypesLoader(string asmPath)
    {
        if (string.IsNullOrEmpty(asmPath))
            throw new ArgumentException("null or empty", "asmPath");

        // file exists?
        var asmFile = new FileInfo(asmPath);

        if (!asmFile.Exists)
            throw new ArgumentException("assembly file does not exist: " + asmFile.FullName, "asmPath");

        // create domain
        _domain = AppDomain.CreateDomain("ExportedTypesLoaderDomain-" + Guid.NewGuid().ToString("N"));

        // load assembly and exported types
        var asmData = File.ReadAllBytes(asmFile.FullName);

        var asm = _domain.Load(asmData);

        _types = asm.GetExportedTypes();

    }
}