.Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");
今天就说说。Net中通过反射取得某个类型时,我们怎么知道这个类型在硬盘上的哪个角落?比如说,假如我们需要要求服务端动态载入某个数据源,那服务端怎么知道数据源在哪?
网上大部分的教程都写着,可以使用Assembly.Load方法来先加载程序集,然后再用Assembly.GetType或者Assembly.GetTypes方法处理。
这个方法很好很实用,基本上也就够了。不过如果这么无聊,也就算不上冷知识,更没有必要写这些了。
如果有办法自动搜索程序集里面有没有暴露对应的类型,我们凭啥还要自行载入程序集?难道小又软的那群人也这么无聊?其实还真是有办法解决这个问题的。
Type.GetType,就是你了。
那么,这个方法有什么神奇的呢?Type.GetType有多个重载,其中除了一个没有参数的以外,剩下的几个重载要求至少一个字符串类型的typeName进行搜索,具体参见MSDN.比如下面这个例子:
using System;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
Type addedInRuntimeType = Type.GetType("LibA.TestClass, LibA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
foreach (var propertyInfo in addedInRuntimeType.GetProperties())
{
Console.WriteLine(propertyInfo.Name);
}
}
}
}
假设目录下我们有一个LibA.dll,LibA.dll里面包含了一个类LibA.TestClass,以上代码就能取得里面的全部属性名,接下来要对这个类型做什么羞羞的事情那就各位看官自行决定咯。
但是,目前还是有一个限制没有解决。GetType方法的参数中和文件有关的就只有typeName了。可是这货并没有指定路径。如果要加载的类型所在的程序集在GAC中或者在当前程序集路径下那还好,如果因为各(xian)种(de)原(dan)因(teng)需要放到子目录该怎么办呢?比如说要在子目录"runtime"以及"runtme2"下进行搜索又该怎么办呢?还是直接放代码托福答案 www.jamo123.com
using System;
namespace ConsoleApplication2
{
internal class Program
{
private static void Main(string[] args)
{
AppDomain.CurrentDomain.AppendPrivatePath("runtime");
AppDomain.CurrentDomain.AppendPrivatePath("runtime2");
Type addedInRuntimeType = Type.GetType("LibA.TestClass, LibA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
foreach (var propertyInfo in addedInRuntimeType.GetProperties())
{
Console.WriteLine(propertyInfo.Name);
}
}
}
}
AppDomain.CurrentDomain.AppendPrivatePath可以增加CLR搜索的路径,不过这个方法已经被标记为obsolete了。请自行无视这个警告吧。或者按如下代码处理:
#pragma warning disable 618
AppDomain.CurrentDomain.AppendPrivatePath("runtime");
#pragma warning restore 618
继续说点,其实这个路径也可以写在配置文件中的。MSDN说明在此,例子如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="runtime;runtime2" />
</assemblyBinding>
</runtime>
</configuration>
.Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");的更多相关文章
- C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法
// 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径,包含文件名System.Diagnostics.Proces ...
- 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory
关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...
- AppDomain.CurrentDomain.GetAssemblies()
AppDomain.CurrentDomain.GetAssemblies() ,获取已加载到此应用程序域的执行上下文中的程序集 解释地址 从微软的解释也可以得知,这个方法只能获取已经加载到此应用程序 ...
- C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别
// 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...
- AppDomain.CurrentDomain.BaseDirectory是什么
AppDomain.CurrentDomain.BaseDirectory 是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的属性 ...
- AppDomain.CurrentDomain.AssemblyResolve
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); 参 ...
- AppDomain.CurrentDomain.BaseDirectory
在winform中的OnPaint事件中,AppDomain.CurrentDomain.BaseDirectory得到的是下面这个路径 C:\Program Files (x86)\Microsof ...
- WinForm 捕获异常 Application.ThreadException + AppDomain.CurrentDomain.UnhandledException
WinForm 捕获未处理的异常,可以使用Application.ThreadException 和AppDomain.CurrentDomain.UnhandledException事件 WinF ...
- C# AppDomain.CurrentDomain.BaseDirectory
AppDomain.CurrentDomain.BaseDirectory 是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的 ...
随机推荐
- zstu.4022.旋转数阵(模拟)
旋转数阵 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1477 Solved: 102 Description 把1到n2的正整数从左上角开始由外层 ...
- Hadoop之 hdfs 系统
一.NameNode维护着2张表: 1.文件系统的目录结构,以及元数据信息 2.文件与数据块列表的对应关系 存放在fsimage中,在运行的时候加载到内存中的. 操作日志写到edits中 二.Da ...
- Vector3.Lerp 插值
Vector3.Lerp 插值 static function Lerp (from : Vector3, to : Vector3, t : float) : Vector3 Description ...
- Unity3D中定时器的使用
源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...
- linux /etc/rc.d/目录的详解
rc.d的内容如下: init.d/ :各种服务器和程序的二进制文件存放目录. rcx.d/: 各个启动级别的执行程序连接目录.里头的东西都是指向init.d/的一些软连接.具体的后边叙述. 还有三个 ...
- 使用Discuz关键词服务器实现PHP中文分词
不同于使用自己的服务器进行分词,Discuz!在线中文分词服务是基于API返回分词结果的.在项目中,我们只需要一个函数即可方便地进行分词.关键词提取.以下是根据Discuz!在线分词服务API写的函数 ...
- http://www.zhihu.com/question/24896283
http://www.zhihu.com/question/24896283 Rix Tox,太不專業了 三百.知乎用户.raintorr 等人赞同 1. 更改变量名的几种方法这种情况下该如何快速选中 ...
- PHP 的__call()
PHP5 的对象新增了一个专用方法 __call(),这个方法用来监视一个对象中的其它方法.如果你试着调用一个对象中不存在或被权限控制中的方法,__call 方法将会被自动调用. 例七:__call ...
- 【GoLang】GoLang fmt 占位符详解
golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. # 定义示例类型和变量 type Human struct { Name string } var peo ...
- 【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...