优化代码 string resourceName = "UpEXE.DLL.d3dcompiler_47.dll"; // 替换为你的命名空间和 DLL 文件名 using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); } resourceName = "UpEXE.DLL.ICSharpCode.SharpZipLib.dll"; // 替换为你的命名空间和 DLL 文件名 using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { byte[] assemblyData = new byte[stream.Length]; stream.Read(assemblyData, 0, assemblyData.Length); return Assembly.Load(assemblyData); }
时间: 2024-02-14 13:24:34 浏览: 133
为了优化代码,你可以将重复的代码块提取为一个方法,并根据需要进行调用。以下是优化后的代码示例:
```csharp
private Assembly LoadEmbeddedAssembly(string resourceName)
{
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
// 调用示例
Assembly assembly1 = LoadEmbeddedAssembly("UpEXE.DLL.d3dcompiler_47.dll");
Assembly assembly2 = LoadEmbeddedAssembly("UpEXE.DLL.ICSharpCode.SharpZipLib.dll");
```
通过将重复的代码块提取为一个方法,你可以避免重复编写相同的逻辑,并使代码更加简洁和易于维护。在需要加载其他嵌入的 DLL 文件时,只需调用 LoadEmbeddedAssembly 方法,并传递正确的资源名称即可。
阅读全文