今天就说说。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");的更多相关文章

  1. C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法

    // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径,包含文件名System.Diagnostics.Proces ...

  2. 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory

    关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...

  3. AppDomain.CurrentDomain.GetAssemblies()

    AppDomain.CurrentDomain.GetAssemblies() ,获取已加载到此应用程序域的执行上下文中的程序集 解释地址 从微软的解释也可以得知,这个方法只能获取已经加载到此应用程序 ...

  4. C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  5. AppDomain.CurrentDomain.BaseDirectory是什么

    AppDomain.CurrentDomain.BaseDirectory 是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的属性 ...

  6. AppDomain.CurrentDomain.AssemblyResolve

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); 参 ...

  7. AppDomain.CurrentDomain.BaseDirectory

    在winform中的OnPaint事件中,AppDomain.CurrentDomain.BaseDirectory得到的是下面这个路径 C:\Program Files (x86)\Microsof ...

  8. WinForm 捕获异常 Application.ThreadException + AppDomain.CurrentDomain.UnhandledException

     WinForm 捕获未处理的异常,可以使用Application.ThreadException 和AppDomain.CurrentDomain.UnhandledException事件 WinF ...

  9. C# AppDomain.CurrentDomain.BaseDirectory

    AppDomain.CurrentDomain.BaseDirectory   是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的 ...

随机推荐

  1. this prototype 闭包 总结

    this对象 整理下思路: 一般用到this中的情景: 1.构造方法中 function A(){ this.name="yinshen"; } var a=new A(); co ...

  2. 读>>>>白帽子讲Web安全<<<<摘要→我推荐的一本书→1

      <白帽子讲Web安全>吴翰清著 刚开始看这本书就被这本书吸引,感觉挺不错,给大家推荐下,最近读这本书,感觉不错的精华就记录下, 俗话说>>>好脑袋不如一个烂笔头< ...

  3. 解决win8下chrome浏览器打开提示没有注册类的方法

    今天又把win8装回来了,继续装了个chrome浏览器,但是发现只能从安装的文件打开,从快捷方式或者快速启动栏都会提示没有注册类.找到一种解决的办法是删除注册表中的相关键值,具体如下: 1.打开注册表 ...

  4. [BZOJ2502]清理雪道

    [BZOJ2502]清理雪道 试题描述 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定 ...

  5. 计蒜客 X的平方根

    X的平方根 设计函数int sqrt(int x),计算x的平方根. 格式: 输入一个数x,输出它的平方根.直到碰到结束符号为止. 千万注意:是int类型哦- 输入可以如下操作: while(cin& ...

  6. Python 脚本之获取CPU信息

    #!/usr/bin/env Python from __future__ import print_function from collections import OrderedDict impo ...

  7. catalog、scheme区别

    按照SQL标准的解释,在SQL环境下Catalog和Schema都属于抽象概念,可以把它们理解为一个容器或者数据库对象命名空间中的一个层次,主要用来解决命名冲突问题.从概念上说,一个数据库系统包含多个 ...

  8. Intersection of Two Arrays | & ||

    Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...

  9. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  10. MyBatis3: Could not find SQL statement to include with refid ‘

    错误: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.Incompl ...