共用方式為


叫用 ADSI 屬性

有兩種方法可直接存取 ADSI COM 物件的屬性。第一種存取方法是使用 InvokeMember 方法。第二種存取方法是使用 InvokeGetInvokeSet 方法,這是 Microsoft .NET Framework 2.0 版 的新方法。

下列 C# 範例示範如何使用 InvokeMember 方法從 Managed 程式碼應用程式擷取 IADSUser 屬性、FirstName 屬性以及 LastName 屬性。如需有關這些屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>、<FirstName>與<LastName>等主題。

using System.Reflection;
using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
Object ads = ent.NativeObject;
Type type = ads.GetType();
String firstName = (string)type.InvokeMember(
    "FirstName", 
    BindingFlags.GetProperty, 
    null, 
    ads, 
    null);
String lastName = (string)type.InvokeMember(
    "LastName", 
    BindingFlags.GetProperty, 
    null, 
    ads, 
    null);

下列 Visual Basic 範例示範如何使用 InvokeMember 方法從管理的程式碼應用程式擷取 IADSUser 屬性、FirstName 屬性以及 LastName 屬性。如需有關這些屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>、<FirstName>與<LastName>等主題。

Imports System.Reflection
Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim ads As [Object] = ent.NativeObject
Dim type As Type = ads.GetType()
Dim firstName As String = CStr(type.InvokeMember( _
    "FirstName", _
    BindingFlags.GetProperty, _
    Nothing, _
    ads, _
    Nothing))
Dim lastName As String = CStr(type.InvokeMember( _
    "LastName", _
    BindingFlags.GetProperty, _
    Nothing, _
    ads, _
    Nothing))

下列 C# 範例示範如何使用 InvokeGet 方法從管理的程式碼應用程式擷取 IADSUser 屬性、FirstName 屬性以及 LastName 屬性。如需有關這些屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>、<FirstName>與<LastName>等主題。

.

using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
String firstName = (string)ent.InvokeGet("FirstName");
String lastName = (string)ent.InvokeGet("LastName");

下列 Visual Basic 範例示範如何使用 InvokeGet 方法從管理的程式碼應用程式擷取 IADSUser 屬性、FirstName 屬性以及 LastName 屬性。如需有關這些屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<IADsUser>、<FirstName>與<LastName>等主題。

Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim firstName As String = CStr(ent.InvokeGet("FirstName"))
Dim lastName As String = CStr(ent.InvokeGet("LastName"))

下列 C# 範例示範如何使用 InvokeMember 方法來設定物件的 Description 屬性。

using System.Reflection;
using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
Object ads = ent.NativeObject;
Type type = ads.GetType();
type.InvokeMember("Description", 
    BindingFlags.SetProperty, 
    null, 
    ads, 
    new object[] {"some description"});

// The changes to the object must always be committed or else they 
// will be lost.
ent.CommitChanges(); 

下列 Visual Basic 範例示範如何使用 InvokeMember 方法來設定物件的 Description 屬性。

Imports System.Reflection
Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
Dim ads As [Object] = ent.NativeObject
Dim type As Type = ads.GetType()
type.InvokeMember("Description", _
    BindingFlags.SetProperty, _
    Nothing, _
    ads, _
    New Object() {"some description"})

' The changes to the object must always be committed or else they 
' will be lost.
ent.CommitChanges()

下列 C# 範例示範如何使用 InvokeSet 方法來設定目錄項目的 Description 屬性。如需有關 Description 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Description>主題。

using System.DirectoryServices;

DirectoryEntry ent = new DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com");
ent.InvokeSet("Description", new object[] {"some description"});

// The changes to the object must always be committed or else they 
// will be lost.
ent.CommitChanges();

下列 Visual Basic 範例示範如何使用 InvokeSet 方法來設定目錄項目的 Description 屬性。如需有關 Description 屬性的詳細資訊,請參閱 MSDN Library (https://go.microsoft.com/fwlink/?LinkID=27252 (本頁面可能為英文)) 中的<Description>主題。

Imports System.DirectoryServices

Dim ent As New DirectoryEntry("LDAP://CN=My User,DC=Fabrikam,DC=com")
ent.InvokeSet("Description", New Object() {"some description"})

' The changes to the object must always be committed or else they 
' will be lost.
ent.CommitChanges()

請參閱

參考

System.DirectoryServices
DirectoryEntry
Type

概念

叫用 ADSI

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation.All rights reserved.