该类型不能用作泛型类型或方法中的类型参数

时间:2018-02-14 20:47:28

标签: c# generics

我有一个DBHelper类,我从中触发回调以将IDataReader转换为Generic Type。我已经完成并运行了整个代码。但是,当我尝试在回调中创建该类型的实例时,我遇到了麻烦,并且应用了where子句[请参阅 - > TestOneCallbacks类中的DataReaderToResultItemConverterCallback]。

where子句使其完美。 TResult将是 TestOne ResultItem类型。但是,我无法使用where子句进行此操作。或者,直接创建 TestTwo ResultItem [请参阅 - >类TestTwoCallbacks]和强制转换使它工作,但不优雅。

这是TestOneCallbacks的以下错误,

  

类型'TResult'不能用作类型参数'TResult'   通用类型或方法   'DataAccess.TestOneCallbacks.DataReaderToResultItemConverterCallback(System.Data.IDataReader)'。   没有装箱转换或类型参数转换   'TResult'到'Service.Interface.TestFilterResultItem'。

这是代码。

public static class DBHelper
{
    public static List<TResult> ExecuteQuery<TResult>(DataQueryArgs<TResult> dataQueryArgs)
    {
        List<TResult> result = null;
        {
            IDataReader reader = null;

            //Get the input parameters for the query
            Dictionary<string, object> inputParameters = dataQueryArgs.InputParameters;

            //Fire callback to Get the DbCommand for the query
            DbCommand command = dataQueryArgs.GetCommandCallback();

            //Fire callback to Get the list of Sql input Parameters for the query
            List<SqlParameter> sqlParameters = dataQueryArgs.CreateSQLParametersCallback(inputParameters);

            //Execute the query and get an IDataReader object
            reader = ExecuteQuery(command, sqlParameters);

            //Fire callback Convert the IReader to ResultItem
            result = dataQueryArgs.DataReaderToResultItemConverterCallback(reader);
        }

        return result;
    }

    private static IDataReader ExecuteQuery(DbCommand command, List<SqlParameter> sqlParameters)
    {
        IDataReader reader = null;
        {
            //TODO:
        }

        return reader;
    }
}

class TestOneResultItem
{
    public TestOneResultItem()
    {
    }

    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime DOB { get; set; }
}

class TestTwoResultItem
{
    public TestTwoResultItem()
    {
    }

    public string Description { get; set; }
    public int Size { get; set; }
}

public class TestOneCallbacks
{
    public DataQueryArgs<TResult> InitializeDataQueryCallbacks<TResult>(Dictionary<string, object> dataQueryArgs_inputParameters)
    {
        DataQueryArgs<TResult> args = null;
        DataQueryArgs<TResult>.DataReaderToResultItemConverter<TResult> dataReaderToResultItemConverterCallback = this.DataReaderToResultItemConverterCallback<TResult>;
        args = new DataQueryArgs<TResult>(dataReaderToResultItemConverterCallback);

        return args;
    }

    public List<TResult> DataReaderToResultItemConverterCallback<TResult>(IDataReader reader) where TResult : TestOneResultItem
    {
        List<TResult> results = new List<TResult>();
        TResult t1 = new TResult();     //Activator.CreateInstance ???
        {
            t1.Name = "ShaQ";
            t1.Age = 99;
            t1.DOB = DateTime.Now;
        }
        results.Add(t1);
        return results;
    }
}

public class TestTwoCallbacks
{
    public DataQueryArgs<TResult> InitializeDataQueryCallbacks<TResult>(Dictionary<string, object> dataQueryArgs_inputParameters)
    {
        DataQueryArgs<TResult> args = null;
        DataQueryArgs<TResult>.DataReaderToResultItemConverter<TResult> dataReaderToResultItemConverterCallback = this.DataReaderToResultItemConverterCallback<TResult>;
        args = new DataQueryArgs<TResult>(dataReaderToResultItemConverterCallback);

        return args;
    }

    public List<TResult> DataReaderToResultItemConverterCallback<TResult>(IDataReader reader) where TResult : TestTwoResultItem
    {
        var results = new List<TestTwoResultItem>();
        TestTwoResultItem t1 = new TestTwoResultItem();     //Activator.CreateInstance ???
        {
            t1.Description = "This works...";
            t1.Size = 345;
        }
        results.Add(t1);
        return results as List<TestTwoResultItem>;      //Ugly, but I know exactly what I am doing. This cast will succeed!
    }
}

1 个答案:

答案 0 :(得分:4)

如果要将TResult传递给该方法,则从调用方法派生的TResult应与调用方法具有相同的类型约束。因此,您必须将类型约束where TResult : TestOneResultItem添加到调用方法:InitializeDataQueryCallbacks

public DataQueryArgs<TResult> InitializeDataQueryCallbacks<TResult>
       (Dictionary<string, object> dataQueryArgs_inputParameters)
       where TResult : TestOneResultItem

顺便说一句,为了调用TResult构造函数,您需要new()约束:

       where TResult : TestOneResultItem, new()