Applying Object values to another object dynamically
-
-I am querying MS AD Objects and want to transfer the properties to my own object with as little code as possible. -I know that there are other ways around this like creating an XLM table to store values but think that might be too much overhead. -If I cannot do something like what is below I will have to write a line for each propertry I want to retrieve. -If I simply copy(not sure how to do this, I dont want to keep running the query) the object I will not be able to see a list of available properties in my IDE.: Any comments or help?
Class MyClass { private string cn; private string sn; public void GetADObjectInfo(string dn) { DirectoryEntry entry = new DirectoryEntry("LDAP://"+ dn); foreach (string strAttrName in entry.Properties.PropertyNames) { try { //this is the part I want done dynamically this.strAttrName = (string)result.Properties[strAttName][0]; } catch { } } } }
with Regards, shwa guy -
-I am querying MS AD Objects and want to transfer the properties to my own object with as little code as possible. -I know that there are other ways around this like creating an XLM table to store values but think that might be too much overhead. -If I cannot do something like what is below I will have to write a line for each propertry I want to retrieve. -If I simply copy(not sure how to do this, I dont want to keep running the query) the object I will not be able to see a list of available properties in my IDE.: Any comments or help?
Class MyClass { private string cn; private string sn; public void GetADObjectInfo(string dn) { DirectoryEntry entry = new DirectoryEntry("LDAP://"+ dn); foreach (string strAttrName in entry.Properties.PropertyNames) { try { //this is the part I want done dynamically this.strAttrName = (string)result.Properties[strAttName][0]; } catch { } } } }
with Regards, shwa guypublic class MyClass { private readonly Dictionary properties = new Dictionary(); public string this[string name] { get { return properties[name]; } set { if (properties.ContainsKey(name)) properties[name] = value; else properties.Add(name, value); } } public void GetADObjectInfo(string dn) { DirectoryEntry entry = new DirectoryEntry("LDAP://" + dn); foreach (string strAttrName in entry.Properties.PropertyNames) { this[strAttrName] = entry.Properties[strAttrName][0] as string; } } }
Then access it like:string myString = MyClass["PropertyName"];
But you can't get it strongly typed (MyClass.PropertyName) without doing it one at a time I think.