Load a dll from shared network drive in C#

后端 未结 3 1763
日久生厌
日久生厌 2020-12-19 17:58

I am able to load the dll using this form -

System.Reflection.Assembly assembly =
System.Reflection.Assembly.LoadFile(@\"C:\\Users\\amit.pandey\\Documents\\         


        
相关标签:
3条回答
  • 2020-12-19 18:08

    Alternatively, loading the file from disk and calling Assembly.Load on the byte array seems to work (Framework 4):

            string Share = @"R:\Test\TestAssembly.dll";
            byte[] AssmBytes = File.ReadAllBytes(Share);
            Assembly a = Assembly.Load(AssmBytes);
    
            //Invoking a method from the loaded assembly
            var obj = a.CreateInstance("TestAssembly.Class1");
            obj.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, null, obj, null);
    

    Since MS blocks network loading in LoadFrom(), it would indicate that doing so is probably not best practices. However, this is similar to the process that is often used with embedding assemblies as resources in an EXE.

    0 讨论(0)
  • 2020-12-19 18:11

    You need to tell the machine to trust that location.

    You can do this with the caspol utility.

    0 讨论(0)
  • 2020-12-19 18:19

    Instead of loading the binary content you can use Assembly.UnsafeLoadFrom. See http://msdn.microsoft.com/en-us/library/system.reflection.assembly.unsafeloadfrom%28v=vs.110%29.aspx

    This worked fine for me in all purposes.

    0 讨论(0)
提交回复
热议问题