.Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");

來源:互聯網
上載者:User

標籤:沒有   服務   void   rgs   ons   .dll   例子   inf   字元   

今天就說說。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");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.