From 24b86c005120bfc30640cb90833be6ef9f4df70f Mon Sep 17 00:00:00 2001 From: Yvan Duhamel Date: Wed, 10 Apr 2019 18:40:20 +0200 Subject: [PATCH] Use reflection instead of hardcoded names to copy object LDAPConnection, to avoid misses/errors --- LDAPCP/LDAPCPConfig.cs | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/LDAPCP/LDAPCPConfig.cs b/LDAPCP/LDAPCPConfig.cs index 84b17c4..cbc2785 100644 --- a/LDAPCP/LDAPCPConfig.cs +++ b/LDAPCP/LDAPCPConfig.cs @@ -796,24 +796,31 @@ public LDAPConnection() internal LDAPConnection CopyPublicProperties() { - return new LDAPConnection() + LDAPConnection copy = new LDAPConnection(); + FieldInfo[] publicFields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); + foreach (FieldInfo field in publicFields) { - Id = this.Id, - Path = this.Path, - Username = this.Username, - Password = this.Password, - Metadata = this.Metadata, - AuthenticationTypes = this.AuthenticationTypes, - UserServerDirectoryEntry = this.UserServerDirectoryEntry, - AugmentationEnabled = this.AugmentationEnabled, - GetGroupMembershipAsADDomain = this.GetGroupMembershipAsADDomain, - GroupMembershipAttributes = this.GroupMembershipAttributes, - Directory = this.Directory, - Filter = this.Filter, - DomainName = this.DomainName, - DomainFQDN = this.DomainFQDN, - RootContainer = this.RootContainer, - }; + field.SetValue(copy, field.GetValue(this)); + } + return copy; + //return new LDAPConnection() + //{ + // Id = this.Id, + // Path = this.Path, + // Username = this.Username, + // Password = this.Password, + // Metadata = this.Metadata, + // AuthenticationTypes = this.AuthenticationTypes, + // UserServerDirectoryEntry = this.UserServerDirectoryEntry, + // AugmentationEnabled = this.AugmentationEnabled, + // GetGroupMembershipAsADDomain = this.GetGroupMembershipAsADDomain, + // GroupMembershipAttributes = this.GroupMembershipAttributes, + // Directory = this.Directory, + // Filter = this.Filter, + // DomainName = this.DomainName, + // DomainFQDN = this.DomainFQDN, + // RootContainer = this.RootContainer, + //}; } }