屬性型別
有數種屬性型別可與目錄物件搭配使用。在 Active Directory 結構描述中,這些屬性 (property) 型別稱為屬性 (attribute) 語法。如需屬性語法的相關資訊,以及可在 Active Directory 中使用之屬性語法的清單,請參閱位於 MSDN library (https://msdn.microsoft.com/libraryadschema.syntaxes) 上的 Active Directory 屬性的語法 (英文) 主題。
下列主題將提供顯示如何使用 System.DirectoryServices 來讀取及寫入屬性型別的程式碼範例:
解譯的資料型別
有兩種不同的方式可從 System.DirectoryServices 命名空間擷取屬性值。第一種方式是使用 Properties 屬性的成員。另一種方式是使用透過 DirectorySearcher 類別取得之 ResultPropertyValueCollection 集合的成員。這些方式都會傳回實際資料型別取決於屬性之結構描述資料型別的一般物件。Properties 屬性將傳回與 IADs.GetInfoEx 方法相同的物件型別。(如需 IADs.GetInfoEx 方法的相關資訊,請參閱位於 MSDN Library (https://msdn.microsoft.com/library) 中的 IADs::GetInfoEx (英文) 主題。)Item 屬性會將部分資料型別轉譯為 .NET Framework 資料型別。下表列出 Active Directory 結構描述型別及其相關的解譯與未解譯資料型別。如需下表所列之 Active Directory 結構描述型別或 COM 介面名稱的相關資訊,請參閱位於 MSDN Library (https://msdn.microsoft.com/library) 上針對該特定型別或 COM 介面名稱的主題。
Active Directory 結構描述型別 | 未解譯的型別 (如 Properties 所傳回者) | 解譯的型別 (如 ResultPropertyValueCollection) 所傳回者) |
---|---|---|
布林值 |
Boolean |
|
列舉型別 |
Int32 |
|
列舉型別 (傳送機制) |
Int32 |
Int32 |
列舉型別 (匯出資訊層次) |
Int32 |
Int32 |
列舉型別 (慣用的傳送方法) |
Int32 |
Int32 |
Integer |
Int32 |
Int32 |
Interval |
可轉換為 IADsLargeInteger 的 COM 物件。 |
|
LargeInteger |
可轉換為 IADsLargeInteger 的 COM 物件。 |
Int64 |
Object(Access-Point) |
不支援 |
不支援 |
Object(DN-Binary) |
可轉換為 IADsDNWithBinary 的 COM 物件。 |
一種 String,含有辨別名稱和以 Object(DN-Binary) 指定格式表示的二進位資料。 |
Object(DN-String) |
可轉換為 IADsDNWithString 的 COM 物件。 |
一種 String,含有辨別名稱和以 Object(DN-String) 指定格式表示的字串資料。 |
Object(DS-DN) |
String |
String |
Object(OR-Name) |
可轉換為 IADsDNWithBinary 的 COM 物件。 |
一種 String,含有辨別名稱和以 Object(DN-Binary) 指定格式表示的二進位資料。 |
Object(Presentation-Address) |
String |
String |
Object(Replica-Link) |
Byte[] |
Byte[] |
String(Generalized-Time) |
DateTime |
|
String(IA5) |
String |
String |
String(NT-Sec-Desc) |
可轉換為 IADsSecurityDescriptor 的 COM 物件。 |
Byte[] |
String(Numeric) |
String |
String |
String(Object-Identifier) |
String |
String |
String(Octet) |
Byte[] |
Byte[] |
String(Printable) |
String |
String |
String(Sid) |
Byte[] |
Byte[] |
String(Teletex) |
String |
String |
String(Unicode) |
String |
String |
String(UTC-Time) |
DateTime |
T:System.DateTime |
解譯 ADSI 物件屬性值
在某些 Active Directory 語法型別中 (如 LargeIntegeradschema.s_largeinteger),System.DirectoryServices 會以 COM 物件的形式傳回屬性值。這種 COM 物件必須轉換為適當的 ADSI 型別,才能取得實際的屬性型別。例如,lastLogon 屬性隸屬於 Interval 語法。因此 System.DirectoryServices 會以支援 IADsLargeInteger 介面的 COM 物件形式傳回 Interval 語法的屬性值。如需這些項目的相關資訊,請參閱位於 MSDN Library (https://msdn.microsoft.com/library) 上的 LargeInteger、Interval、IADsLargeInteger 和 lastLogon 等主題 (英文)。
下列 C# 範例會顯示如何將 COM 物件轉換為 ActiveDs.IADsLargeInteger 物件,以便從 COM 物件中取得 IADsLargeInteger 介面。若您正在開發的應用程式,使用了 ActiveDS 命名空間中的物件,請在編譯與連結應用程式時,參考 ActiveDS 型別程式庫與 activeds.tlb。
object obj = entry.Properties["lastLogon"];
ActiveDs.IADsLargeInteger largeIntADSI;
largeIntADSI = (ActiveDs.IADsLargeInteger)obj;
下表列出 System.DirectoryServices 將以 COM 物件的形式傳回的語法型別,以及每個語法型別的相關 ADSI 介面。如需下表所列之語法型別或 ADSI 介面的相關資訊,請參閱位於 MSDN Library (https://msdn.microsoft.com/library) 上該特定語法型別或 ADSI 介面的主題。
語法型別 | ADSI 介面 |
---|---|
Interval |
IADsLargeInteger |
LargeInteger |
IADsLargeInteger |
Object(DN-Binary) |
IADsDNWithBinary |
Object(DN-String) |
IADsDNWithString |
Object(OR-Name) |
IADsDNWithBinary |
String(NT-Sec-Desc) |
IADsSecurityDescriptor |
下列 C# 範例會顯示如何為 ADSI COM 物件取得適當的介面。此範例會使用 InvalidCastException 例外狀況,來判斷轉換是否有效。
static string GetADSIComObjectValue(object obj)
{
if(obj.GetType().Name.Equals("__ComObject"))
{
/*
Try IADsSecurityDescriptor. This is returned for the following AD
syntax type:
String(NT-Sec-Desc)
*/
try
{
ActiveDs.IADsSecurityDescriptor secDesc;
secDesc = (ActiveDs.IADsSecurityDescriptor)obj;
return "IADsSecurityDescriptor:" + secDesc.Owner.ToString();
}
catch (System.InvalidCastException)
{
}
/*
Try IADsLargeInteger. This is returned for the following AD syntax
types:
Interval
LargeInteger
*/
try
{
ActiveDs.IADsLargeInteger largeIntADSI;
largeIntADSI = (ActiveDs.IADsLargeInteger)obj;
Int64 largeInt = largeIntADSI.HighPart * 0x100000000;
largeInt += largeIntADSI.LowPart;
return "IADsLargeInteger:" + largeInt.ToString();
}
catch (System.InvalidCastException)
{
}
/*
Try IADsDNWithBinary. This is returned for the following AD syntax
types:
Object(DN-Binary)
Object(OR-Name)
*/
try
{
ActiveDs.IADsDNWithBinary dnWithBinary;
dnWithBinary = (ActiveDs.IADsDNWithBinary)obj;
return "IADsDNWithBinary:" +
dnWithBinary.DNString + ":" +
dnWithBinary.BinaryValue.ToString();
}
catch (System.InvalidCastException)
{
}
/*
Try IADsDNWithString. This is returned for the following AD syntax
type:
Object(DN-String)
*/
try
{
ActiveDs.IADsDNWithString dnWithString;
dnWithString = (ActiveDs.IADsDNWithString)obj;
return "IADsDNWithString:" +
dnWithString.DNString + ":" +
dnWithString.StringValue;
}
catch (System.InvalidCastException)
{
}
throw new System.ArgumentException("Unknown COM Object type.");
}
else
{
throw new System.ArgumentException("Object is not a COM Object.");
}
}