0

Based on this question How to get Namespace of an Assembly?:

I can retrieve the namespace(s) from assembly with Assembly.GetTypes() but how can I provide this retrieved namespace file wide like using Namespace in first lines of C# file when loaded with "Add Reference" in project settings?

What is with other C# files? How can I provide this loaded namespace to them?

My approach:

// using NameSpaceFromLib; ??? how to do this with Assembly.LoadFrom

namespace Test
{
    public class ClassA : DisposableObject
    {
        static ClassA()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            // switch between 32 and 64 bit version
            currentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomainAssemblyResolve);
        }

        private const string LibraryName = "LibToLoad";
        private static Assembly CurrentDomainAssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith(LibraryName, StringComparison.InvariantCultureIgnoreCase))
            {
                return LoadBitDepthDependentAssembly(LibraryName + ".dll");
            }
            return null;
        }

        private static Assembly LoadBitDepthDependentAssembly(string assemblyname)
        {
            var currentDirectory = Environment.CurrentDirectory;
            try
            {
                var platformDirectory = Environment.Is64BitProcess ? "x64" : "x86";
                var newPath = Helper.GetApplicationPath(platformDirectory);
                Environment.CurrentDirectory = newPath;
                var p = Path.Combine(newPath, assemblyname);
                var assembly = Assembly.LoadFrom(p);                
                return assembly;
            }
            finally
            {
                Environment.CurrentDirectory = currentDirectory;
            }
        }
    }
}

Thank you!

PS: I need this to distinguish during runtime if x86 or x64 variant of C++/CLI dll to load

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
leon22
  • 5,280
  • 19
  • 62
  • 100
  • I don't understand your question, but if the problem is to choose the correct version of a dll that can only be determined at runtime, you could consider using `AppDomain.AssemblyResolve` and `Environment.Is64BitProcess` to load the correct version of a dll. – steve16351 May 28 '19 at 09:11
  • @steve16351 Yes I know, but how to use the namespace like using Namespace; if I load the assembly with Add Reference in the project settings and not manually with LoadFrom? – leon22 May 28 '19 at 09:17
  • @steve16351 See also my edit! – leon22 May 28 '19 at 09:22
  • Just add a project reference. The compiler doesn't care whether the DLL was built for x86 or x64, it only uses the metadata in the file. The C# project needs to have its "Prefer 32-bit" checkbox turned off. Although you probably want to tinker with it to test this code, a 32-bit OS is pretty hard to find these days. – Hans Passant May 28 '19 at 11:44

0 Answers0