0

您好,我目前正在尝试解决有关我正在开发的软件的问题。

我想要实现的目标:从没有 WriteAllBytes 的 byte[] 将 dll 加载到内存流中(这意味着我想避免接触磁盘)。

我尝试了很多方法,但都失败了。我认为我成功地将 byte[] 加载到内存中,但是我编译的可执行文件仍在寻找从磁盘而不是内存加载它。我如何使它从内存中加载它以便能够使用?

让我们进入代码。

WebClient client = new WebClient();
string b64strbin = client.DownloadString("URL OF A FILE WHERE IT CONTAINS BYTE[]"); // this will download the string and store it to a variable.
byte[] decode = Convert.FromBase64String(b64strbin); // just decode it from base64 back to byte[]
byte[] packed = QuickLZ.decompress(decode); // decompressed dll byte[] (dont mind about this)
Assembly.Load(packed); // here i am loading the byte[] to the memory but still i get an exception

//methods that require the dll in order to run

Console.Read();

当我尝试运行时出现异常。 例外

4

1 回答 1

0

我终于能够通过处理异常来解决问题。我所要做的就是手动解析 dll 引用并使用Assembly.Load.

解决方案:

static void Main()
        {
            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly;
                
                //code that depends on the methods of the dll
                //we reflect on memory.

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Read();
            }
        }

        private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
        {
            try
            {
                WebClient client = new WebClient();
                string b64strbin = client.DownloadString("URL OF A FILE WHERE IT CONTAINS BYTE[]");
                byte[] decode = Convert.FromBase64String(b64strbin);
                return Assembly.Load(QuickLZ.decompress(decode));
            }
            catch (Exception ex)
            {
                return null;
            }
        }

结论: 这样做之后你可以实现的效果真的很棒,我的意思是你可以使用任何依赖的可执行文件并包含 dll 甚至不接触磁盘甚至将其作为嵌入式资源,只需将 dll 直接反映到内存中client.DownloadString. 我确实相信天空是在那之后的极限。

于 2020-06-24T18:11:23.383 回答