AppDomain.CurrentDomain.AssemblyResolve要求一个<&AppName的GT;的.resources组装? [英] AppDomain.CurrentDomain.AssemblyResolve asking for a <AppName>.resources assembly?

查看:263
本文介绍了AppDomain.CurrentDomain.AssemblyResolve要求一个<&AppName的GT;的.resources组装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的代码如何

嵌入卫星组装成csharptest.net提供的EXE文件,我创建了一个自定义程序集解析器和嵌入我的程序集在我的资源。



我可以成功地解决用我的组件,但不知何故AppDomain.CurrentDomain.AssemblyResolve要求称为AppName.resources专MyProgram.resources组装,版本= 0.15。 3992.31638,文化= EN-US,公钥=空,我不知道如何解决?



我试图从禁用加载资源,我的自定义组件(放在我所有的程序集DLL在程序目录)和刚启用AppDomain.CurrentDomain.AssemblyResolve,但它仍然是自讨苦吃。



对此,我感到有点困惑, 。会欣赏很多,如果你能帮助我在此



下面是我为有兴趣的人的代码;

 静态大会ResolveAssemblies(对象发件人,ResolveEventArgs参数)
{
装配装配= NULL;
字符串名称= args.Name.Substring(0,args.Name.IndexOf(,));
如果(名称==MyProgram.resources)返回NULL;
,否则名称=的String.Format(,名MyProgram.Resources.Assemblies {0} .dll文件。);

锁(_loadedAssemblies)
{
如果(!_loadedAssemblies.TryGetValue(姓名,出组装))
{
使用(流IO =大会。 GetExecutingAssembly()。GetManifestResourceStream(名))
{
如果(IO == NULL)
{
MessageBox.Show(MyProgram无法加载它的一个依赖关系。请重新-install节目的String.Format(缺少大会:{0},名称),MessageBoxButtons.OK,MessageBoxIcon.Error);
Environment.Exit(-1);
}使用
(BinaryReader binaryReader =新BinaryReader(IO))
{
总成=的Assembly.Load(binaryReader.ReadBytes((int)的io.Length));
_loadedAssemblies.Add(姓名,组装);
}
}
}
}

返回组装;
}


解决方案

在回答有关我自己;



添加此行的AssemblyInfo.cs解决它,解析器将无法得到要求的任何资源,更多的。

  [汇编:NeutralResourcesLanguageAttribute(EN-US,UltimateResourceFallbackLocation.MainAssembly)] 

虽然这是一个解决方法应慎重考虑多语言应用程序。



更多信息:





这方法未能与非EN-美国文化的机器。更好的办法是在组装解析器忽略的资源;

 公开征集解析器(对象发件人,ResolveEventArgs参数)
{
锁(本)
{
议会会议;
的AssemblyName askedAssembly =新的AssemblyName(args.Name);

的String []栏= args.Name.Split('');
字符串名称=场[0];
串文化=领域[2];
//不能忽略卫星资源集查询或使用:在AssemblyInfo.cs中[装配NeutralResourcesLanguage(EN-US,UltimateResourceFallbackLocation.MainAssembly)]
//会崩溃非恩程序美国的制度文化。
如果(name.EndsWith(资源)及和放大器;!culture.EndsWith(中性))返回NULL;

/ *实际装配解析* /

}
}


using the code How to embed a satellite assembly into the EXE file provided by csharptest.net, I've created a custom assembly resolver and embedded my assemblies in my resources.

I can successfully resolve my assemblies used in but somehow AppDomain.CurrentDomain.AssemblyResolve asks for an assembly called 'AppName.resources' specifically "MyProgram.resources, Version=0.15.3992.31638, Culture=en-US, PublicKeyToken=null" which i don't know how to resolve?

I've tried to disable loading my custom assemblies from resources (placed all my assembly dll's in program directory) and just enabled AppDomain.CurrentDomain.AssemblyResolve, but it was still asking for it.

I'm a bit confused about this, will appreciate a lot if you can help me on this.

Here's my code for interested ones;

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;
}

解决方案

Answering on my own;

Adding this line to AssemblyInfo.cs solves it and resolver will not get asked for resources any-more.

[assembly: NeutralResourcesLanguageAttribute("en-US", UltimateResourceFallbackLocation.MainAssembly)]

Though this is a work-around should be carefully considered multi-language applications.

More Info:

This approach fails for machines with non en-US cultures. A better approach is ignoring resources on assembly resolver;

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 */
                ...
            }
      }

这篇关于AppDomain.CurrentDomain.AssemblyResolve要求一个<&AppName的GT;的.resources组装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆