Exemplo n.º 1
0
        public static void GetFullNameOfClassThatInheritsFromApplication(string pathOfAssemblyThatContainsEntryPoint, out string applicationClassFullName, out string assemblyName, out string assemblyFullName)
        {
            //-----------------
            // Thanks to: http://www.c-sharpcorner.com/UploadFile/girish.nehte/how-to-unload-an-assembly-loaded-dynamically-using-reflection/
            //-----------------

            // Load the assembly in a new AppDomain mainly so that we are able to unload it, which is necessary when we want to delete all temporary files including this assembly.
            AppDomainSetup setupInformation = AppDomain.CurrentDomain.SetupInformation;
            AppDomain      newAppDomain     = AppDomain.CreateDomain("newAppDomain", AppDomain.CurrentDomain.Evidence, setupInformation);

            //Create an instance of the inspector class in the new domain:
            //System.Runtime.Remoting.ObjectHandle obj = newAppDomain.CreateInstance(typeof(AssemblyInspectorOnOtherDomain).Assembly.FullName, typeof(AssemblyInspectorOnOtherDomain).FullName);
            string pathOfThisVeryAssembly = PathsHelper.GetPathOfThisVeryAssembly();

            System.Runtime.Remoting.ObjectHandle obj = newAppDomain.CreateInstanceFrom(pathOfThisVeryAssembly, typeof(AssemblyInspectorOnOtherDomain).FullName);

            IAssemblyInspectorOnOtherDomain inspector = (IAssemblyInspectorOnOtherDomain)obj.Unwrap(); // As the object we are creating is from another appdomain hence we will get that object in wrapped format and hence in next step we have unwrappped it

            // Call LoadAssembly method so that the assembly will be loaded into the new appdomain amd the object will also remain in new appdomain only:
            inspector.LoadAssembly(pathOfAssemblyThatContainsEntryPoint);

            // Call the method that finds the type that inherits from Application:
            applicationClassFullName = inspector.FindApplicationClassFullName();

            // Get the assembly name and full name too:
            assemblyName     = inspector.GetAssemblyName();
            assemblyFullName = inspector.GetAssemblyFullName();

            // Unload the assembly (so that we can later delete it if necessary):
            AppDomain.Unload(newAppDomain);
            GC.Collect();                  // Collects all unused memory
            GC.WaitForPendingFinalizers(); // Waits until GC has finished its work
            GC.Collect();
        }