wuvist

C#范型不会用,求助~

using Dapper;
using Dapper.Contrib.Extensions;
using System.Data.SqlClient;
using System.Configuration;

namespace Orm
{
    public class RecordBase<T> where T : class
    {

        public static SqlConnection OpenConnection()
        {
            var ConnStr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection connection = new SqlConnection(ConnStr);
            connection.Open();
            return connection;
        }

        public static T Get(string id)
        {
            using (var conn = OpenConnection())
            {
                return conn.Get<T>(id);
            }
        }

        public bool Update()
        {
            using (var conn = OpenConnection())
            {
                //Beblow compile error, because conn.Update<T> expect parameter to be T, i.e. the sub-class, but "this" is parent class.
                return conn.Update<T>(this);
            }
        }
    }
}

 

posted on 2015-02-22 15:55  问天  阅读(271)  评论(0编辑  收藏  举报

导航