首页 > 编程语言 >C#设置程序集PrivatePath的几种方法

C#设置程序集PrivatePath的几种方法

时间:2023-01-09 08:44:46浏览次数:48  
标签:C# AppDomain System 几种 BindingFlags CurrentDomain path AssemblyResolve PrivatePa

为了使用程序目录更加整洁和美观,我们会建立不同文件夹,分门另类地管理软件目录下的程序集和配置文件,要想使主程序能够读取它们,必须要设置PrivatePath路径,常用的方法包括:

方法一 config配置

在app.config中配置中,添加如下代码,操作简单,配置灵活。

<runtime>
    <gcConcurrent enabled="true" />
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <publisherPolicy apply="yes" />
      <probing privatePath="Lib;Bin" />
    </assemblyBinding>
  </runtime>

方法二 代码设置

有时,我们不想让这个配置暴露出来,防止用户随意改动,也可以写在Main构造函数中。

static Program()
{
    //设置程序集引用路径
    string privatePath = "Lib;zh-Hans;AutoBase";
    AppDomain.CurrentDomain.SetData("PRIVATE_BINPATH", privatePath);
    AppDomain.CurrentDomain.SetData("BINPATH_PROBE_ONLY", privatePath);
    MethodInfo updateContextMethod = typeof(AppDomainSetup).GetMethod("UpdateContextProperty", BindingFlags.NonPublic | BindingFlags.Static);
    MethodInfo getFusionContextMethod = typeof(AppDomain).GetMethod("GetFusionContext", BindingFlags.NonPublic | BindingFlags.Instance);
    if (updateContextMethod != null && getFusionContextMethod != null)
    {
        updateContextMethod.Invoke(null, new[] { getFusionContextMethod.Invoke(AppDomain.CurrentDomain, null), "PRIVATE_BINPATH", privatePath });
    }
}

方法三 Resolve事件

如果主程序未能读取加载到想要的程序集,将触发AppDomain的AssemblyResolve事件。

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

在这个事件里处理即可。

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Libs\");
    path = System.IO.Path.Combine(path, args.Name.Split(',')[0]);
    path = String.Format(@"{0}.dll", path);
    return System.Reflection.Assembly.LoadFrom(path);
}

标签:C#,AppDomain,System,几种,BindingFlags,CurrentDomain,path,AssemblyResolve,PrivatePa
From: https://www.cnblogs.com/liweis/p/17035963.html

相关文章

  • Educational Codeforces Round 141
    A.MakeitBeautiful他想要变美,我们按照题目说的做就行,通过判断我们发现如果在sort一遍后sort(a+1,a+1+n);if(a[1]==a[n]){cout<<"NO"<<"\n";......
  • [email protected]: The engine "node" is incompatible with this module. Expected vers
    [email protected]:Theengine"node"isincompatiblewiththismodule.Expectedversion"^14.15.0||^16.10.0||>=18.0.0".Got"16.6.1"直接忽略[root@localhostw......
  • Atcoder ABC112D Partition
    链接难度:\(\texttt{1025}\)找到最大的正整数\(x\)使得\(m\modx=0\)且\(\frac{m}{x}\gen\)。难度在于读题,简化后就简单的一批了。暴力都能过。枚举\(m\)的......
  • AttributeError: 'NoneType' object has no attribute 'append'
    在写python脚本时遇到AttributeError:'NoneType'objecthasnoattribute'append'a=[]b=[1,2,3,4]a=a.append(b)执行一次后发现a的类型变为了NoneType。下次执......
  • 一文看懂Scrum
     上文讨论了敏捷的内涵,以及敏捷开发在实践过程中的常见误区。本篇,我们由迭代和增量的区别入题,再梳理一下最流行的敏捷开发方法-Scrum,并看下Scrum模型中如何对迭代和增量......
  • OpenCL神经网络FPGA加速器与DeepCL
    OpenCL神经网络FPGA加速器与DeepCL概述PipeCNN是一种基于OpenCL的大规模卷积神经网络FPGA加速器。在FPGA界中,利用高级合成(HLS)工具来设计和实现FPGA上的定制电路的趋势越来......
  • readthedocs | 为工具撰写使用文档
     之前有教程:bookdown-撰写和发表自己的网络书籍/文档 现在发现readthedocs可能更简单,更适合页数不多的文档,可以适配JupyternotebooksinSphinx,分析做好,注释好,文档......
  • Engineering Fundamentals Checklist(微软软件工程基础检查表)
    微软的软件工程基础检查表对于了解大公司的工作流程有一定的帮助。(翻译自)[https://microsoft.github.io/code-with-engineering-playbook/ENG-FUNDAMENTALS-CHECKLIST/......
  • css
    /*版心的公共类*/.container{width:1240px;margin:0auto;}/*---------------快捷菜单模块:xtx-shortcut*/.xtx-shortcut{height:52px;background-color:......
  • ACWING 4366. 上课睡觉
    url:4366.上课睡觉-AcWing题库题意:给n个石堆,相邻石堆可以合并现在要求每个石堆都相等,问最少合并多少次思路:由于不管咋个合并,石子数是不会变的那么就可以枚举......