通过AssemblyResolve event实现。然后就可以using直接调用了。
GetManifestResourceStream里面加载的资源名称字符串由"项目名.路径文件夹名.dll文件名带扩展名"构成。看资源文件路径就可以得到。
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{//=> Assembly.LoadFrom($@"{new FileInfo(args.RequestingAssembly.Location).DirectoryName}\{args.Name.Split(',')[0]}.dll");
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Qrcode.Resources.QRCoder.dll"))
{ //从嵌入的资源dll文件加载程序集
byte[] byData = new byte[stream.Length];
stream.Read(byData, 0, byData.Length);
return Assembly.Load(byData);
}
}
下面是一个我写的导出c#类给excel vba调用的代码。目的是在excel里生成二维码。我只生成X64的dll因为我的office是64位的应用。
using QRCoder;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
namespace Qrcodes
{
[ComVisible(true), ClassInterface(ClassInterfaceType.AutoDual)]
public class myqrcode
{
public void CreateIm([MarshalAs(UnmanagedType.BStr)]string s, [MarshalAs(UnmanagedType.BStr)]string filePath)