如何基于.NET框架版本定义代码?(How to define away code based on .NET framework version?)
我有一个功能,希望利用“正确”的方式来做一些在.NET 4.5中提供的东西:
public DbDataAdapater CreateDataAdapter(DbConnection connection) { #IFDEF (NET45) return DbProviderFactories.GetFactory(connection).CreateDataAdapter(); #ELSE //We can't construct an adapter directly //So let's run around the block 3 times, before potentially crashing DbDataAdapter adapter; if (connection is System.Data.SqlClient.SqlConnection) return new System.Data.SqlClient.SqlDataAdapter(); if (connection is System.Data.OleDb.OleDbConnection) return new System.Data.OleDb.OleDbDataAdapter(); if (connection is System.Data.Odbc.OdbcConnection) return new System.Data.Odbc.OdbcDataAdapter(); //Add more DbConnection kinds as they become invented if (connection is SqlCeConnection) return new SqlCeDataAdapter(); if (connection is MySqlConnection) return new MySqlDataAdapter(); if (connection is DB2Connection) return new DB2DataAdapter(); throw new Exception("[CreateDataAdapter] Unknown DbConnection type: " + connection.GetType().FullName); #END }
我能够找到完成这项工作的唯一方法是使用此共享代码来改变其Visual Studio解决方案的 每个人。
哪个不会发生; 它只需要工作,或者它根本不会被使用。
当解决方案针对早期版本的.NET框架时,有没有办法定义非功能性代码?
换句话说,如果编译得很好:
public DbDataAdapter CreateDataAdapter(DbConnection conn) { if (System.Runtime.Version >= 45) return DbProviderFactories.GetFactor(connection).CreateDataAdapter(); else { //...snip the hack... } }
但如果目标框架太低,它就无法编译。
I have a function that would love to take advantage of the "correct" way to do something that was provided in .NET 4.5:
public DbDataAdapater CreateDataAdapter(DbConnection connection) { #IFDEF (NET45) return DbProviderFactories.GetFactory(connection).CreateDataAdapter(); #ELSE //We can't construct an adapter directly //So let's run around the block 3 times, before potentially crashing DbDataAdapter adapter; if (connection is System.Data.SqlClient.SqlConnection) return new System.Data.SqlClient.SqlDataAdapter(); if (connection is System.Data.OleDb.OleDbConnection) return new System.Data.OleDb.OleDbDataAdapter(); if (connection is System.Data.Odbc.OdbcConnection) return new System.Data.Odbc.OdbcDataAdapter(); //Add more DbConnection kinds as they become invented if (connection is SqlCeConnection) return new SqlCeDataAdapter(); if (connection is MySqlConnection) return new MySqlDataAdapter(); if (connection is DB2Connection) return new DB2DataAdapter(); throw new Exception("[CreateDataAdapter] Unknown DbConnection type: " + connection.GetType().FullName); #END }
The only way i've been able to find to make this work is for everyone who uses this shared code to alter their Visual Studio solution.
Which not going to happen; it has to just work, or it's not going to be used at all.
Is there a ways to define away non-functional code when the solution targets earlier versions of the .NET framework?
In other words, it would be great if this compiled:
public DbDataAdapter CreateDataAdapter(DbConnection conn) { if (System.Runtime.Version >= 45) return DbProviderFactories.GetFactor(connection).CreateDataAdapter(); else { //...snip the hack... } }
But it doesn't compile if the targeted framework is too low.
原文:https://stackoverflow.com/questions/19084981