泛型方法操作,根据传递参数查询数据,赋值到T返回对象

public class Student

{

    public int ID{get;set;}

    public string Name{get;set;}

}

public T GetModule(T obj,string where)

{

    //怎么通过传递的类型赋值

             //要求:1.根据传递的SQL语句查询一条记录

             //2.把查询的数据赋值到T obj中

             //3.最后返回已赋值的obj;

}

//调用

public void Main()

{

   Student st=GetModule<Student>(new Student(),"select * from Student where ID=1");

   

}


慕桂英546537
浏览 663回答 2
2回答

红颜莎娜

public&nbsp;T&nbsp;GetModule<T>(T&nbsp;obj,string&nbsp;where) {&nbsp;&nbsp;&nbsp;&nbsp;//怎么通过传递的类型赋值&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 要求:1.根据传递的SQL语句查询一条记录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;//2.把查询的数据赋值到T&nbsp;obj中&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//3.最后返回已赋值的obj;}
打开App,查看更多内容
随时随地看视频慕课网APP