我看过几篇文章,详细介绍了如何在将连接字符串添加到 ConfigurationManager.ConnectionStrings 时绕过 ReadOnly 障碍。请参阅下面一篇此类帖子的示例。这些例子中的每一个都有一个可怕但含糊的警告,即所采用的技术是“危险的”。有什么危险?
Dim cssc As ConnectionStringSettingsCollection = WebConfigurationManager.ConnectionStrings
Dim t As Type = cssc.GetType().BaseType ' System.Configuration.ConfigurationElementCollection
Dim fi As FieldInfo = t.GetField("bReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)
fi.SetValue(cssc, False)