如何基于.NET Framework版本定义远code吗? [英] How to define away code based on .NET framework version?

查看:315
本文介绍了如何基于.NET Framework版本定义远code吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,将 取的的正确的的方法的优势,做的是提供了在.NET 4.5的东西

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
}

我已经能够找到,使这项工作的唯一方法是对于每个人谁使用此共享code到<一个href="http://stackoverflow.com/questions/2923210/c-sharp-conditional-compilation-and-framework-targets">alter他们的Visual Studio解决方案。

这不会发生的;它具有刚工作,或者它不会被使用在所有

Which not going to happen; it has to just work, or it's not going to be used at all.

有没有办法来定义走非功能code当解决方案面向早期版本的.NET框架?

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.

推荐答案

如果优先级是有这个最小的编译时间的设置工作,那么我会继续前进检查到运行和使用反射来检查方法是可行的,并使用此替代方法它不是时候。本作将采用更好的方法的好处运行在4.5安装一个客户端,针对.NET 4.0的应用程序。

If the priority is to have this working with minimal compile time setup then I would just move the check to runtime and use reflection to check if the method is available and use the workaround when its not. This as the added benefit that an application targeting .NET 4.0 running in a client with 4.5 installed will use the better approach.

示例:

static Func<DbConnection, DbProviderFactory> GetFactoryDelegate;

private static void Main() {
    Console.WriteLine(GetFactory(new SqlConnection()).CreateDataAdapter());
}
private static DbProviderFactory GetFactory(DbConnection connection) {
    if (GetFactoryDelegate == null) {
        var frameworkGetFactoryMethod = typeof (DbProviderFactories).GetMethod(
            "GetFactory", BindingFlags.Static | BindingFlags.Public,
            null, new[] { typeof (DbConnection) }, null);

        if (frameworkGetFactoryMethod != null) {
            GetFactoryDelegate = (Func<DbConnection, DbProviderFactory>)
                Delegate.CreateDelegate(
                    typeof(Func<DbConnection, DbProviderFactory>),
                    frameworkGetFactoryMethod);
        }
        else { GetFactoryDelegate = GetFactoryThroughWorkaround; }
    }
    return GetFactoryDelegate(connection);
}
private static DbProviderFactory GetFactoryThroughWorkaround(
    DbConnection connection) {

    if (connection is SqlConnection)
        return SqlClientFactory.Instance;
    // ... Remaining cases
    throw new NotSupportedException();
}

这个方法是非常相似的是在检查一个功能是可用,而不是执行浏览器嗅探的JavaScript的世界当前的最佳实践。在.NET版本不具有相同的风采,由于需要使用反射。然而,code可以作出prettier如果动态要求是可以接受的。

This approach is much similar to what is current best practice in the JavaScript world of checking that a feature is available instead of performing browser sniffing. The .NET counterpart does not have the same elegance due to requiring the use of reflection. However, the code could be made prettier if the requirement for dynamic is acceptable.

这篇关于如何基于.NET Framework版本定义远code吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆