在C#中获取动态类型的ServiceStack.redis客户端

时间:2013-07-11 01:49:52

标签: dynamic redis servicestack typing

我是C#的新手,我想知道如何实现下面描述的功能。目前,这不是在指定的行编译。我希望代码能做的是:

通过每个KVP迭代, 使用keystring作为表名来查询db 返回列表

var dbCon = dbConnectionFactory.OpenDbConnection();
Dictionary<string, Type> ibetDic = getFromSomeWhere();
foreach (KeyValuePair<string, Type> entry in ibetDic)
    {
        Type type = entry.Value;
        var typedRedisClient = redis.GetTypedClient<type>();/*not compiling here*/
        String sql = "USE ibet SELECT * FROM " + entry.Key;
        var itemList = dbCon.SqlList<type>(sql);/*not compiling here*/
        foreach (var tableRow in itemList )
        {
            //store with redistypedclient
        }
    }

1 个答案:

答案 0 :(得分:1)

我发现答案的最接近的事情然而这意味着我必须传入类型而不是能够通过字典访问它,因为我想要上面:

public void GetAndStoreIntKey<T>(string tableName) where T : IHasId<int>
    {
        var dbCon = dbConnectionFactory.OpenDbConnection();
        String sql = "USE ibet SELECT * FROM " + tableName;
        var items = dbCon.SqlList<T>(sql);
        var typedRedisClient = redis.As<T>();

        foreach (T item in items)
        {
            typedRedisClient.SetEntry(UrnId.CreateWithParts<T>(new string[] {item.Id + ""}), item);
        }
    }

用法如:

 GetAndStoreIntKey<Sport>("Sport");