我想用ResourceManager添加一个程序集我有这个代码,但它显然不起作用。请帮忙!
加载资源并尝试将其用作程序集:
static ResourceManager resourceManager = new ResourceManager("res", Assembly.GetExecutingAssembly());
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
AppDomain domain = (AppDomain)sender;
if(args.Name.Contains("System.Data.SQLite"))
{
return domain.Load(resourceManager.GetObject("System.Data.SQLite"));
}
return null;
}
将资源放在ResourceManager中:
using (ResourceWriter w = new ResourceWriter("res.resources"))
{
w.AddResource("System.Data.SQLite", File.ReadAllText("System.Data.SQLite.dll"));
}
if (CodeDom.Compile(outputValueTb.Text, Properties.Resources.src, iconValueTb.Text, "res.resources"))
{
//File.Copy("System.Data.SQLite.dll", System.IO.Path.GetDirectoryName(outputValueTb.Text) + "/System.Data.SQLite.dll");
File.Delete("res.resources");
success("Built");
}
所以我将代码更改为
w.AddResource("System.Data.SQLite", File.ReadAllBytes("System.Data.SQLite.dll"));
但我仍然不知道如何使用此代码将资源用于程序集:
AppDomain domain = (AppDomain)sender;
if(args.Name.Contains("System.Data.SQLite"))
{
return domain.Load(resourceManager.GetObject("System.Data.SQLite")); //should be a resource not bytes[]
}
return null;
答案 0 :(得分:0)
如果您已将dll添加为项目项目文件并在Build Action类型(在属性下)将其标记为“Embedded Resource”,则可以使用以下内容。解析器是您已有的事件处理程序。注意:我没有检查语法,只需在此输入;所以如果需要,请修复语法。
internal static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var name = Assembly.GetExecutingAssembly()
.GetManifestResourceNames()
.FirstOrDefault(f => f.Contains("System.Data.SQLite"));
if (!string.IsNullOrEmpty(name) && args.Name.Contains("SQLite"))
{
using(var strm = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
var bytes = new byte[strm.Length];
stream.Read(bytes, 0, strm.Length);
return Assembly.Load(bytes);
}
}
return null;
}