public abstract class AbstractService
{
public abstract void Load();
public abstract Int32 Save();
public abstract object DataSource{ get; }
}
/// 数据服务的抽象化(数据服务的基类)
public abstract class AbstractServiceDS<TDataTable, TAdapter> : AbstractService
where TDataTable : DataTable, new()
where TAdapter : class, new ()
{
private TDataTable _table = null;
private TAdapter _adapter = null; private MethodInfo _adMethodFill = null;
private MethodInfo _adMethodUpdate = null; //
// 无参数构造函数
//
public AbstractServiceDS()
{
this._table = new TDataTable();
this._adapter = new TAdapter();
//初始化反射方法
this.InitReflectionMethods();
}
//
// 外部数据集的构造函数
//
public AbstractServiceDS(TDataTable table, TAdapter adapter)
{
this._table = table;
this._adapter = adapter;
//初始化反射方法
this.InitReflectionMethods();
}
//初始化反射方法
private void InitReflectionMethods()
{
try
{
Type typeAdapter = typeof(TAdapter);
this._adMethodFill = typeAdapter.GetMethod("Fill");
this._adMethodUpdate = typeAdapter.GetMethod("Update", new Type[]{ typeof(TDataTable) });
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
} public override void Load()
{
object[] args = new object[] { this._table };
_adMethodFill.Invoke(this._adapter, args);
}
//重写保存的虚方法
public override int Save()
{
int nRows = 0;
try
{
object[] args = new object[] { this._table };
nRows = (int)_adMethodUpdate.Invoke(this._adapter, args);//在这就跑出“未将对象引用设置到对象的实例? ”
this._table.AcceptChanges();
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
return nRows; }
public override object DataSource
{
get { return this._table; }
}
public TAdapter Adapter
{
get { return this._adapter; }
protected set { this._adapter = value; }
}
}
}
我刚刚学c# 请前辈赐教
解决方案 »
- 在C#中通Mapinfo OLE来操作地图。如何使用回调呢
- vs2008 打开网站提示恶意软件
- datagrid的checkbox根据数据库的字段显示是否选中并且添加单击事件
- 加密与解密
- 急!,谁可以找点C# .NET的资料给我啊?
- 怎样将XmlDocument转成FileStream
- 嵌入式窗体无法控制DataGridView控件的行只读属性
- Framework版本问题,连接MYSQL
- 用Dotnet做安装项目,如何在安装过程中取消安装!
- System.Text.Encoding和Encoding的区别?
- 如何循环winform界面的所有控件?
- C# 读取并下载文件时(是用txt文件试的),把页面源文件内容也写到文件里了,请问这是怎么回事?
http://blog.csdn.net/handsome1234/article/details/7291809