简体   繁体   English

C#动态返回类型

[英]C# Dynamic return type

I've solved my problem 我解决了我的问题

    public virtual T GenerateCommand()
    {
        Type typeParameterType = typeof(T);
        if (typeParameterType == typeof(SqlCommand))
        {
            //SqlCommand
        }
        else if (typeParameterType == typeof(MySqlCommand))
        {
            //MySqlCommand
        }

        return new T()
        {

        };
    }

I'm building a class that generates Queries/Commands, the class has the ability to generate various DbCommands (MsSqlServer & MySql) when the type of the database is defined in field of the class. 我正在建立一个生成查询/命令的类,当在类的字段中定义数据库的类型时,该类具有生成各种DbCommands(MsSqlServer&MySql)的能力。 How can I return the correct type? 如何返回正确的类型?

I tried this: 我尝试了这个:

public virtual DbCommand GenerateCommand()
{
    //.....
}

but I always need to convert the return value: 但是我总是需要转换返回值:

MySqlCommand s = this.GenerateCommand() as MySqlCommand;

I also tried this: 我也试过这个:

public virtual T GenerateCommand<T>()
{
    //.....
}

But it's not what I'm looking for because I cannot create an instance of DbCommand so I cannot return nothing. 但这不是我想要的,因为我无法创建DbCommand的实例,因此无法返回任何内容。

Is there some way that T will be based on stored type on my class? T是否有某种方式可以基于我的课堂上的存储类型?

Edit: the using of generic class throw two exceptions at least: 编辑:泛型类的使用至少引发两个异常:

    if (T is MySqlCommand)
    {
        return new MySqlCommand();
    }
    else
    {
        return new SqlCommand();
    }

'T' is a 'type parameter' but is used like a 'variable' “ T”是“类型参数”,但其用法类似于“变量”

Cannot implicitly convert type 'MySql.Data.MySqlClient.MySqlCommand' to 'T' 无法将类型'MySql.Data.MySqlClient.MySqlCommand'隐式转换为'T'

(T)MySqlCommand()

also do not work 也行不通

Edit 2: can I do something like that? 编辑2:我可以这样做吗?

public virtual T GenerateCommand(SqlDatabaseKind dbKind)
{
    T = (dbKind == SqlDatabaseKind.MicrosoftSqlServer) ? SqlCommand :
        MySqlCommand;
}

You can have generic classes. 您可以具有通用类。 It sounds like this is more what you are looking for. 听起来这是您正在寻找的更多东西。 Then you can just return the type that the class was created as: 然后,您可以返回创建类的类型为:

public class MyDatabaseClass<T> where T : DbCommand
{

    public virtual T GenerateCommand()
    {
        //.....
    }
}

I have used a generic constraint to make sure that the T is of type DbCommand, also. 我使用通用约束来确保T的类型也为DbCommand。

You make 'GenerateCommand()' as a void, with two overloads: one, which receives an 'out MySqlCommand', and another which receives an 'out SqlCommand', and change the in the function. 您将'GenerateCommand()'设为void,有两个重载:一个重载,它接收到一个'out MySqlCommand',另一个重载了'输出SqlCommand,然后在函数中进行更改。

void GenerateCommand(out MySqlCommand com)
{
.
.
.
}

void GenerateCommand(out SqlCommand com)
{
.
.
.
}

out: http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.71).aspx 出: http : //msdn.microsoft.com/zh-cn/library/t3c3bfhx(v=vs.71).aspx

If you want to move between database types, then your code should depend on abstraction (DbCommand, DbConnection, DbParameter etc) instead of concrete implementations (SqlConnection, SqlCommand). 如果要在数据库类型之间移动,则代码应取决于抽象(DbCommand,DbConnection,DbParameter等),而不是具体的实现(SqlConnection,SqlCommand)。 Otherwise you will have to change all database-related code when new database provider added. 否则,在添加新的数据库提供程序时,您将必须更改所有与数据库相关的代码。 And you will duplicate all database-related code for every provider. 然后,您将为每个提供程序复制所有与数据库相关的代码。

.Net already has abstract factory for creating all this database-related abstractions. .Net已经有了抽象工厂来创建所有这些与数据库相关的抽象。 It's called DbProviderFactory . 它称为DbProviderFactory And looks like it's what you are trying to implement: 看起来这就是您要实现的目标:

public virtual DbCommand CreateCommand()
public virtual DbConnection CreateConnection()

.Net has some built-in provider-specific implementations of this factory (SqlClientFactory, OleDbFactory, OdbcFactory). .Net具有此工厂的某些内置提供程序特定的实现(SqlClientFactory,OleDbFactory,OdbcFactory)。 Here you can find how to use DbProviderFactories. 在这里,您可以找到如何使用DbProviderFactories。 And here you can find DbProviderFactory for MySql. 在这里,您可以找到MySql的DbProviderFactory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM