调用 ADSI 属性
可以通过以下两种方式之一直接访问 ADSI COM 对象的属性。访问它的第一种方式是使用 InvokeMember 方法。访问它的第二种方式是使用 InvokeGet 和 InvokeSet 方法,这两种方法是 Microsoft .NET Framework 2.0 版 中新增的。
下面的 C# 示例说明如何使用 InvokeMember 方法从托管代码应用程序中检索 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)中的主题“描述”。
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)中的主题“描述”。
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
概念
Send comments about this topic to Microsoft.
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。