属性类型

有若干种用于目录对象的属性类型。在 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 接口名称的详细信息,请参阅 MSDN Library(网址为 https://go.microsoft.com/fwlink/?LinkID=27252)中针对该特定类型或 COM 接口名称的主题。

Active Directory 域服务架构类型 未解释的类型(由 Properties 返回) 解释的类型(由 ResultPropertyValueCollection 返回)

Boolean

Boolean

Boolean

Enumeration

Int32

Int32

Enumeration (Delivery-Mechanism)

Int32

Int32

Enumeration (Export-Information-Level)

Int32

Int32

Enumeration (Preferred-Delivery-Method)

Int32

Int32

Integer

Int32

Int32

Interval

可以强制转换为 IADsLargeInteger 的 COM 对象。

Int64

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

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 将 Interval 语法的属性值作为支持 IADsLargeInteger 接口的 COM 对象返回。有关这些元素的详细信息,请参阅 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 接口的信息,请参阅 MSDN Library(网址为 https://go.microsoft.com/fwlink/?LinkID=27252)中针对该特定语法类型或 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.");
    }
}

另请参见

参考

System.DirectoryServices
DirectoryEntry
ResultPropertyValueCollection
DirectorySearcher

概念

目录对象属性

Send comments about this topic to Microsoft.

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。