Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set property value through an attribute tag name using reflection?
    text
    copied!<p>I wish to write a reusable library for querying against AD with LDAP. I'm using both ActiveDs COM objects and System.DirectoryServices.</p> <p>Greatly inspired by Bart de Smet LINQ to AD, I have written a SchemaAttribute and an DirectoryAttributeAttribute classes to use with a DirectorySource(Of T) class (Yes, it's VBNET, but any C# code will help as I'm fluent in both languages).</p> <p>Now, when querying against AD using LDAP (System.DirectoryServices), you may choose what property/attribute you wish to get loaded by the DirectorySearcher class. Then, I've written myself a method that takes a ParramArray of String as its parameter, so that I add the LDAP properties to the DirectorySearcher.PropertiesToLoad() method within a foreach() statement. Here's a piece of code to make it clear (Assuming that ldapProps parameter will always contain value(s)):</p> <pre><code>Public Function GetUsers(ByVal ParamArray ldapProps() As String) As IList(Of IUser) Dim users As IList(Of IUser) = New List(Of IUser) Dim user As IUser Dim de As DirectoryEntry = New DirectoryEntry(Environment.UserDomainName) Dim ds As DirectorySearcher = New DirectorySearcher(de, "(objectClass=user)") For Each p As String In ldapProps ds.PropertiesToLoad(p) Next Try Dim src As SearchResultCollection = ds.FindAll() For Each sr As SearchResult In src user = New User() // This is where I'm stuck... Depending on the ldapProps required, I will fill only these in my User object. Next End Function </code></pre> <p>Here's a piece of my User class:</p> <pre><code>Friend NotInheritable Class User Implements IUser Private _accountName As String Private _firstName As String &lt;DirectoryAttributeAttribute("SAMAccountName")&gt; _ Public Property AccountName As String Get Return _accountName End Get Set (ByVal value As String) If (String.Equals(_accountName, value)) Then Return _accountName = value End Set End Property &lt;DirectoryAttributeAttribute("givenName")&gt; _ Public Property FirstName As String Get Return _firstName End Get Set (ByVal value As String) If (String.Equals(_firstName, value)) Then Return _firstName = value End Set End Property End Class </code></pre> <p>Now, I would like to benefit of those attributes that I put on top of my User class properties. I know how to get these attributes, and I know how to get my properties. What I am unsure is how to make sure that the right property will be set the right value from the SearchResult class to my User class.</p> <p><strong>EDIT</strong> As time plays against me, I can't wait to get acquainted with the concept of DirectorySource(Of T), as it requires a bit more coding for me to write to get it working. As a workaround, I'm writing a UserFactory class which will be called through my ActiveDirectoryFacade.</p> <p><strong>EDIT</strong> This SO question seems to be very close to what I wish to accomplish:<br> <a href="https://stackoverflow.com/questions/1359484/reflection-attributes-and-property-selection">Reflection, Attributes and Property Selection</a></p> <p><strong>EDIT</strong> This looks like what I want: <a href="https://stackoverflow.com/questions/390594/c-setting-property-values-through-reflection-with-attributes">C# setting property values through reflection with attributes</a><br> Anyone has another idea or can confirm this is right?</p> <p>I shall also mention that I'm stuck in .NET Framework 2.0 and VBNET2005. Otherwise, I would have used Bart de Smet's LINQ to AD library.</p> <p>Thanks for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload