簡體   English   中英

哪種正確的方法知道傳遞給C#中的泛型方法的參數類型?

[英]Which is the right way to know type of the argument passed to the generic method in C#?

從我的C# 4.0代碼中,我想在SybaseMSSQL上執行一些查詢。 正在使用(或將要使用)的數據庫僅在運行時才確定/已知。

我分別使用Sybase和SQL的AseCommandSqlCommand

我決定創建這樣的通用方法:

private Department ExecuteCommand<T>(T databaseCommand) where T : class
{
            Department department = new Department ();

            dynamic command = databaseCommand;

            using (dynamic databaseReader = command.ExecuteReader())
            {
                if (databaseReader.HasRows)
                {

                    while (databaseReader.Read())
                    {
                         department.Employees.Add(this.CreateDepartmentInstance(databaseReader));
                    }
                }
            }

            command.Connection.Dispose();

            return department;
        }

問題:

  1. 除了dynamic以外,還有更好的選擇嗎?
  2. 知道傳遞給泛型方法的參數類型的通常正確方法是什么
  3. 當然,我可以為Sybase和SQL編寫兩個單獨的方法。 但是,為什么要處理諸如dynamic事情呢?

您在這里不一定需要動態的,您需要的只是一個公共接口(在這種情況下,它們都具有):

private Department ExecuteCommand<T>(T databaseCommand) where T : IDbCommand

但是,在這種情況下,由於您沒有對T類型做任何特別有趣的事情(據我所知),因此以下方法簽名就足夠了:

private Department ExecuteCommand(IDbCommand databaseCommand)

通常,如果沒有通用接口,我建議您使用Facade模式,並簡單地將方法調用委派給實際實現的等效項。

看起來AseCommandSqlCommand都實現了IDbCommand ,因此您只需要更改類型約束即可:

private Department ExecuteCommand<T>(T databaseCommand) where T : IDbCommand
{
    Department department = new Department();

    using (IDataReader databaseReader = databaseCommand.ExecuteReader())
    {
        if (databaseReader.HasRows)
        {

            while (databaseReader.Read())
            {
                department.Employees.Add(
                                 this.CreateDepartmentInstance(databaseReader));
            }
        }
    }

    command.Connection.Dispose();

    return department;
}

假設您還需要更改CreateDepartmentInstance以接受IDataReader如果尚未接受)。

盡管正如rich.okelly指出的那樣,但實際上並沒有理由首先在這里使用泛型。

回答#2:

if (typeof(T) == typeof(object) ) {
    // Check for IEnumerable
}

用您要檢查的任何類型替換對象。

在您的情況下,使用通用接口就可以了。

一般來說,如果你想找出一個對象的類型,你可以隨時使用的isas -Operators。

private Department ExecuteCommand<T>(T databaseCommand) where T : class
{
            Department department = new Department ();

            var command = databaseCommand;

            using (var databaseReader = command.ExecuteReader())
            {
                if ((databaseReader as IDataReader).HasRows)
                {

                    while ((databaseReader as IDataReader).Read())
                    {
                          department.Employees.Add(this.CreateDepartmentInstance(databaseReader));
                    }
                }
            }

            command.Connection.Dispose();

            return department;
        }

尚未在VS中驗證,但應該可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM