.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>

posted @ 2016-12-01 11:54  ChineseMoonGod  阅读(829)  评论(0编辑  收藏  举报