[困难]关于泛型在函数中的使用问题?

程序猿KL 2007-11-23 11:29:14
现在有一个CLASS 名称是: modelDao ,它是我的系统中的底层抽象数据操作类.

public void InsertObject(object obj){
//略
}

public object GetObject(string title){
//根据title提取一个对象
}
为了方便起见,我把所有的对象类型都认为是object.

看到泛方面的资料后,我有个想法,是不是能把 object 换成 "泛"

如, public T GetObject(string title){
///
}
...全文
60 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yunfeng007 2007-11-24
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Text;

namespace FinaHome.IDAL
{
public interface IGenericDao<T, U>
where T : class
where U : struct

{
/** Persist the newInstance object into database */
U create(T newInstance);

/** Retrieve an object that was previously persisted to the database using
* the indicated key as primary key
*/
T read(U key);

/** Save changes made to a persistent object. */
void update(T transientObject);

/** Remove an object from persistent storage in the database */
void delete(T persistentObject);

}
}
沅江汐水 2007-11-24
  • 打赏
  • 举报
回复
public List<object> GetList(string title)
{}

程序猿KL 2007-11-24
  • 打赏
  • 举报
回复
不成功,ChrisAK 提供方法不行!
程序猿KL 2007-11-24
  • 打赏
  • 举报
回复
如果返回的是一个是IList 又该怎么办呢!?

如:
public IList<object> GetList(string title){

}

是不是写成 这样呢!?
public T GetList<T>(string title){

}

这样返回来 T 我怎么确定它是 IList 呢!?
ChrisAK 2007-11-23
  • 打赏
  • 举报
回复
public T GetObject<T> (string title){
///
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧