使用代码 How to embed a satellite assembly into the EXE file由 csharptest.net 提供,我创建了一个自定义程序集解析器并将我的程序集嵌入到我的资源中。
我可以成功解析我在中使用的程序集,但 AppDomain.CurrentDomain.AssemblyResolve 要求一个名为“AppName.resources”的程序集,特别是“MyProgram.resources,Version=0.15.3992.31638,Culture=en-US,PublicKeyToken=null”我不知道如何解决?
我已经尝试禁用从资源加载我的自定义程序集(将我所有的程序集 dll 放在程序目录中)并且只是启用了 AppDomain.CurrentDomain.AssemblyResolve,但它仍然要求它。
我对此有点困惑,如果你能帮助我,我将不胜感激。
这是我为感兴趣的人编写的代码;
static Assembly ResolveAssemblies(object sender, ResolveEventArgs args)
{
Assembly assembly = null;
string name = args.Name.Substring(0, args.Name.IndexOf(','));
if (name == "MyProgram.resources") return null;
else name = string.Format("MyProgram.Resources.Assemblies.{0}.dll", name);
lock (_loadedAssemblies)
{
if (!_loadedAssemblies.TryGetValue(name, out assembly))
{
using (Stream io = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
{
if (io == null)
{
MessageBox.Show("MyProgram can not load one of it's dependencies. Please re-install the program", string.Format("Missing Assembly: {0}", name), MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(-1);
}
using (BinaryReader binaryReader = new BinaryReader(io))
{
assembly = Assembly.Load(binaryReader.ReadBytes((int)io.Length));
_loadedAssemblies.Add(name, assembly);
}
}
}
}
return assembly;
}
最佳答案
自己作答;
将此行添加到 AssemblyInfo.cs 可以解决问题,解析器将不再被要求提供资源。
[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.MainAssembly)]
尽管这是一种变通方法,但应仔细考虑多语言应用程序。
更多信息:
- https://connect.microsoft.com/VisualStudio/feedback/details/526836/wpf-appdomain-assemblyresolve-being-called-when-it-shouldnt
- http://blogs.msdn.com/b/kimhamil/archive/2008/11/11/what-does-the-neutralresourceslanguageattribute-do.aspx
- http://forums.devshed.com/net-development-87/c-wpf-appdomain-assemblyresolve-being-called-when-it-shouldn-t-669567.html
- http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-richter-excerpt-2-from-clr-via-c-third-edition.aspx
对于具有非 en-US 文化的机器,此方法失败。更好的方法是忽略程序集解析器上的资源;
public Assembly Resolver(object sender, ResolveEventArgs args)
{
lock (this)
{
Assembly assembly;
AssemblyName askedAssembly = new AssemblyName(args.Name);
string[] fields = args.Name.Split(',');
string name = fields[0];
string culture = fields[2];
// failing to ignore queries for satellite resource assemblies or using [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
// in AssemblyInfo.cs will crash the program on non en-US based system cultures.
if (name.EndsWith(".resources") && !culture.EndsWith("neutral")) return null;
/* the actual assembly resolver */
...
}
}
关于c# - AppDomain.CurrentDomain.AssemblyResolve 请求 <AppName>.resources 程序集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4368201/