Get the type of specified assembly in C#

This program will return the list of types for specified assembly. This can be used where you need to follow a system with plugin architecture where you need to decide a type of new plugin (assembly) onto the production system before enter into the database.

Complete C# code
-------------------
public class AssemblyViewer : MarshalByRefObject
    {
        #region Constructor

        public AssemblyViewer()
        {
            _dumbAssemblies = new Dictionary<string, AssemblyBuilder>();
        }

        #endregion

        #region Methods

        public List<string> GetTypes(string asmFile)
        {
            Assembly asm = this.LoadAssemblyForViewFrom(asmFile);
            List<string> ret = new List<string>();
            // here go the types that could be loaded
            foreach (Type type in asm.GetTypes())
            {
                if (type != null)
                    ret.Add(type.FullName);
            }

            return ret;
        }

        #endregion

        #region Event handlers

        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(args.Name),

AssemblyBuilderAccess.Run);
            _dumbAssemblies.Add(args.Name, ab);

            return ab;
        }

        #endregion

        #region Supplementary

        private void GenerateDumbType(string asm, string typeName)
        {
            AssemblyBuilder ab;
            if (_dumbAssemblies.TryGetValue(asm, out ab))
            {
                ModuleBuilder module = ab.GetDynamicModule(asm);
                if (module == null)
                    module = ab.DefineDynamicModule(asm);
                TypeBuilder tb = module.DefineType(typeName, TypeAttributes.Public);
                tb.CreateType();
            }
        }

        private Assembly LoadAssemblyForViewFrom(string asmFile)
        {
            Assembly asm = Assembly.LoadFrom(asmFile);
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            try
            {
                asm.GetTypes();
            }
            catch (ReflectionTypeLoadException e)
            {
                // here are the unresolved ones
                if (e.LoaderExceptions != null)
                {
                    foreach (TypeLoadException exc in e.LoaderExceptions)
                    {
                        string missedAsmName = (string)typeof(TypeLoadException).GetField("AssemblyName",

BindingFlags.Instance | BindingFlags.NonPublic).GetValue(exc);
                        GenerateDumbType(missedAsmName, exc.TypeName);
                    }
                }
            }
            finally
            {
                AppDomain.CurrentDomain.AssemblyResolve -= new

ResolveEventHandler(CurrentDomain_AssemblyResolve);
            }

            return asm;
        }

        #endregion

        #region Fields

        private Dictionary<string, AssemblyBuilder> _dumbAssemblies;

        #endregion
    }


     Public static void Main(string[] args)
     {
        string filename = @"c:\stagingDir\MyPlugin.dll";
            AppDomain app2 = AppDomain.CreateDomain("Resolver");

            AssemblyViewer ins = (AssemblyViewer)app2.CreateInstanceAndUnwrap("ReflectingNoDependencies",

"ReflectingNoDependencies.AssemblyViewer");

            List<string> types = ins.GetTypes(filename);

            foreach (string s in types)
            {
                Console.WriteLine(s);
            }
           AppDomain.Unload(app2);
       }


Regards,
Megha
By Perry    Popularity  (1967 Views)