您将不得不求助于使用反射(代码适用于 Microsoft .NET Framework,因此您必须验证私有字段在 Mono 中是否相同)。
using System.Data.Common;
// 1. Initialize the default configuration
DbProviderFactories.GetFactoryClasses();
// 2. Retrieve the configuration table.
var config = (DataTable)typeof(DbProviderFactories).GetField("_providerTable", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null);
// 3. Add a new entry in the configuration (replace the values with ones for your provider).
DataRow dataRow = config.NewRow();
dataRow["Name"] = "SqlClient Data Provider";
dataRow["InvariantName"] = typeof(SqlConnection).Namespace.ToString();
dataRow["Description"] = ".Net Framework Data Provider for SqlServer";
dataRow["AssemblyQualifiedName"] = typeof(SqlConnection).AssemblyQualifiedName;
config.Rows.Add(dataRow);