今天就说说。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. malloc 函数到底做了什么?

    请看下面的代码. 猜测结果是什么?编译通过吗? #include <stdio.h> #include <stdlib.h> int main() { ; char *ptr ...

  2. Java EE学习——Quartz的Cron表达式

    经历过低谷后,还是要好好学习,越失落会越来越落后. 今天写一下Cron表达式的用法,虽然是之前自己写的,也过了挺长一段时间,这次就拿出来作为回顾吧. Cron表达式是Quartz的精髓(个人觉得),比 ...

  3. HDOJ 1907 John

    对于任意一个 Anti-SG 游戏,如果我们规定当局面中所有的单一游戏的 SG 值为 0 时,游戏结束,则先手必胜当且仅当:  (1)游戏的 SG 函数不为 0 且游戏中某个单一游戏的 SG 函数大于 ...

  4. 练习英语ing——[POJ1004]Financial Management

    [POJ1004]Financial Management 试题描述 Larry graduated this year and finally has a job. He's making a lo ...

  5. [BZOJ4632]树的编码

    [BZOJ4632]树的编码 试题描述 SHUXK 正在对一棵N个结点的有根树进行研究,首要的一件事就是对这棵树进行编码. lz 说:“这还不容易吗?我令根节点的编号为 1,然后保证每个结点的编号都比 ...

  6. 转载一篇ios7的新API文章

    不看不知道,一看吓一跳啊!请看看吧,http://leafduo.com/articles/2013/09/28/ios7/

  7. Java for LeetCode 162 Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. IOS多线程(NSThread)

    1.创建方法 使用NSThread创建线程主要有两个个方法,分别如下 NSThread* myThread = [[NSThread alloc] initWithTarget:self   sele ...

  9. codeforces A. Rook, Bishop and King 解题报告

    题目链接:http://codeforces.com/problemset/problem/370/A 题目意思:根据rook(每次可以移动垂直或水平的任意步数(>=1)),bishop(每次可 ...

  10. Codeforces7C 扩展欧几里得

    Line Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...