屬性型別
有幾種屬性可搭配目錄物件使用。在 Active Directory 網域服務結構描述中,這些屬性類型稱為屬性語法。如需有關屬性語法的詳細資訊,以及可在 Active Directory 網域服務中使用的屬性語法清單,請參閱 MSDN library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Active Directory 屬性的語法>主題。
下列主題提供的程式碼範例示範如何使用 System.DirectoryServices 讀取和寫入屬性類型:
解譯的資料型別
有兩種不同的方法可以從 System.DirectoryServices 命名空間擷取屬性值。第一種方式是使用 Properties 屬性的成員。另一種方式是使用 ResultPropertyValueCollection 集合的成員,其中包含 DirectorySearcher 類別。其中每個都會傳回一般物件,其實際資料型別依屬性的結構描述資料型別而定。Properties 屬性將傳回與 IADs.GetInfoEx 方法相同的物件類型。(如需有關 IADs.GetInfoEx 方法的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADs::GetInfoEx>主題。)Item 屬性將一些資料型別轉譯成 .NET Framework 資料型別。下表顯示 Active Directory 網域服務結構描述型別及其相關已解譯與未解譯的資料型別。如需有關下表所列 Active Directory 網域服務結構描述類型或 COM 介面名稱的詳細資訊,請參閱該特定類型或 COM 介面名稱的主題,此主題位於 MSDN Library,網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)。
Active Directory 網域服務結構描述類型 | 未解譯的類型 (如 Properties 所傳回) | 已解譯的類型 (如 ResultPropertyValueCollection 所傳回) |
---|---|---|
布林值 |
Boolean |
|
列舉型別 (Enumeration) |
Int32 |
|
列舉 (Delivery-Mechanism) |
Int32 |
Int32 |
列舉 (Export-Information-Level) |
Int32 |
Int32 |
列舉 (Preferred-Delivery-Method) |
Int32 |
Int32 |
整數 |
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 |
DateTime |
解譯 ADSI 物件屬性值
對於一些 Active Directory 網域服務語法類型,例如 LargeInteger,System.DirectoryServices 將以 COM 物件傳回屬性值。COM 物件必須轉型為適當的 ADSI 類型以取得實際屬性類型。例如,lastLogon 屬性屬於 Interval 語法。System.DirectoryServices 將以 COM 物件傳回 Interval 語法的屬性值,此物件支援 IADsLargeInteger 介面。如需有關這些項目的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<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 介面的詳細資訊,請參閱該特定語法類型或 ADSI 介面的主題,此主題位於 MSDN Library,網址為 https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)。
語法類型 | 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.");
}
}
請參閱
參考
System.DirectoryServices
DirectoryEntry
ResultPropertyValueCollection
DirectorySearcher
概念
Send comments about this topic to Microsoft.
Copyright © 2007 by Microsoft Corporation.All rights reserved.