Ejemplo n.º 1
0
        /// <summary>
        /// Build connection string by combining all public properties in string in format "name=value;..."
        /// If property name needs to be different you need to use the DisplayName Attribute
        /// </summary>
        /// <returns>Connection String</returns>
        public static string BuildConnectionString(this IConnectionSettings connectionSettings)
        {
            _connectionString = new Lazy <string>(() =>
            {
                var connectionString = new StringBuilder();
                foreach (var propertyInfo in connectionSettings.GetType().GetProperties())
                {
                    var value = propertyInfo.GetValue(connectionSettings);
                    if (value != null)
                    {
                        var name = propertyInfo.GetCustomAttribute <DisplayNameAttribute>() != null
                            ? propertyInfo.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName
                            : propertyInfo.Name;
                        connectionString.Append($"{name}={value};");
                    }
                }

                return(connectionString.ToString());
            });

            return(_connectionString.Value);
        }