|
Posted on
2006-12-04 02:33
adamxx
阅读( 584)
评论()
编辑
收藏
举报
//定义一个接口,用于定义存储过程
interface INorthwindStoredProcedures
...{
//定义存储过程对应的方法
DataSet CustOrderHist(string CustomerID);
//如果存储过程名字和方法名字不同,应该用SqlAccessAttribute来进行说明
[SqlAccess("Employee Sales By Country")]
DataTable EmployeeSalesByCountry(DateTime Beginning_Date,DateTime Ending_Date);
用System.Reflection.Emit 来自动生成调用存储过程的实现
转贴,原文地址忘了
//使用的例子
namespace Lostinet.Sample
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) ...{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义一个接口,用于定义存储过程
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
interface INorthwindStoredProcedures
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//定义存储过程对应的方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet CustOrderHist(string CustomerID);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果储存过程名字和方法名字不同,应该用SqlAccessAttribute来进行说明
[SqlAccess("Employee Sales By Country")]
DataTable EmployeeSalesByCountry(DateTime Beginning_Date,DateTime Ending_Date);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//...more...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//MORE Ideas..
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//直接执行SQL语句?
//[SqlAccess(SqlAccessType.SqlQuery,"SELECT * FROM Employees WHERE EmployeeID=@EmpId")]
//DataTable SelectEmployee(int EmpId);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class ConsoleApplication
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
[STAThread]
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
using(SqlConnection conn=new SqlConnection("server=(local);trusted_connection=true;database=northwind"))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//一句话就把实现创建了!
//需要传如 SqlConnection 和 SqlTransaction
//SqlTransaction可以为null
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这个好就好在,只要能得到SqlConnection/SqlTransaction就能用这个方法了,所以兼容 Lostinet.Data.SqlScope
INorthwindStoredProcedures nsp=(INorthwindStoredProcedures)
StoredProcedure.CreateStoredProcedureInterface(typeof(INorthwindStoredProcedures),conn,null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用储存过程并且显示
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowData("CustOrderHist ALFKI",nsp.CustOrderHist("ALFKI"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowData("Employee Sales By Country",nsp.EmployeeSalesByCountry(new DateTime(1998,1,1),new DateTime(1999,1,1)));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static void ShowData(string title,object data)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
Form f=new Form();
f.Width=600;
f.Height=480;
f.Text=title;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataGrid grid=new DataGrid();
grid.Dock=DockStyle.Fill;
grid.DataSource=data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
f.Controls.Add(grid);
f.ShowDialog();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif) //实现方法(不完整)#region //实现方法(不完整)
namespace Lostinet.Sample
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
using System;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
using System.Data;
using System.Data.SqlClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这个类作为实现的基类,
//目的是提供储存 SqlConnection/SqlTransaction 和公用的一些方法
//这个类必须为public,否则无法继承
//但开发者不会显式访问这个类
public class SPInterfaceBase : IDisposable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
public SPInterfaceBase()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Dispose()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//CreateStoredProcedureInterface会把相关的值SqlConnection/SqlTransaction存到这里
public SqlConnection connection;
public SqlTransaction transaction;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建一个SqlCommand
public SqlCommand CreateCommand(string spname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
SqlCommand cmd=new SqlCommand(spname,connection,transaction);
cmd.CommandType=CommandType.StoredProcedure;
//TODO:
//cmd.Parameters.Add("@ReturnValue",...
return cmd;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//由 Type 推算出 SqlDbType , 未完成
SqlDbType GetSqlDbType(Type type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//TODO:switch(type)...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return SqlDbType.NVarChar;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义参数
public void DefineParameter(SqlCommand cmd,string name,Type type,ParameterDirection direction)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
SqlParameter param=new SqlParameter("@"+name,GetSqlDbType(type));
param.Direction=direction;
cmd.Parameters.Add(param);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//在SqlCommand执行前设置参数值
public void SetParameter(SqlCommand cmd,string name,object value)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
cmd.Parameters["@"+name].Value=(value==null?DBNull.Value:value);
}
//在SqlCommand执行后取得参数值
public object GetParameter(SqlCommand cmd,string name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return cmd.Parameters["@"+name].Value;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//根据不同的返回值执行不同的操作
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public SqlDataReader ExecuteDataReader(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return cmd.ExecuteReader();
}
public object ExecuteScalar(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return cmd.ExecuteScalar();
}
public void ExecuteNonQuery(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
cmd.ExecuteNonQuery();
}
public DataSet ExecuteDataSet(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
DataSet ds=new DataSet();
using(SqlDataAdapter sda=new SqlDataAdapter(cmd))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
sda.Fill(ds);
}
return ds;
}
public DataTable ExecuteDataTable(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
DataTable table=new DataTable();
using(SqlDataAdapter sda=new SqlDataAdapter(cmd))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
sda.Fill(table);
}
return table;
}
public DataRow ExecuteDataRow(SqlCommand cmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
DataTable table=ExecuteDataTable(cmd);
if(table.Rows.Count==0)
return null;
return table.Rows[0];
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class StoredProcedure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
static public object CreateStoredProcedureInterface(Type interfaceType,SqlConnection connection,SqlTransaction transaction)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//检查参数
if(interfaceType==null)throw(new ArgumentNullException("interfaceType"));
if(!interfaceType.IsInterface)
throw(new ArgumentException("argument is not interface","interfaceType"));
if(connection==null)throw(new ArgumentNullException("connection"));
if(transaction!=null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
if(transaction.Connection!=connection)
throw(new ArgumentException("transaction.Connection!=connection","transaction"));
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建StoredProcedure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StoredProcedure spemit=new StoredProcedure();
spemit.interfaceType=interfaceType;
spemit.connection=connection;
spemit.transaction=transaction;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建
return spemit.CreateInstance();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//用于储存已创建的类型
static Hashtable EmittedTypes=new Hashtable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Type interfaceType;
SqlConnection connection;
SqlTransaction transaction;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private StoredProcedure()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object CreateInstance()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
lock(interfaceType)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//如果没有创建具体的实现,则创建它
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(emittedType==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
emittedType=(Type)EmittedTypes[interfaceType];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(emittedType==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
CreateType();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//储存已创建类型
EmittedTypes[interfaceType]=emittedType;
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建具体的实例
SPInterfaceBase spi=(SPInterfaceBase)Activator.CreateInstance(emittedType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置SqlConnection/SqlTransaction
spi.connection=connection;
spi.transaction=transaction;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return spi;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Type emittedType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TypeBuilder typeBuilder;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建类型
void CreateType()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//创建 Assembly
//AssemblyBuilderAccess.Run-表示只用于运行,不在磁盘上保存
AssemblyName an=new AssemblyName();
an.Name="Assembly."+interfaceType.FullName+".Implementation";
AssemblyBuilder asmBuilder=AppDomain.CurrentDomain.DefineDynamicAssembly(an,AssemblyBuilderAccess.Run);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建Module
ModuleBuilder mdlBuilder=asmBuilder.DefineDynamicModule("Module."+interfaceType.FullName+".Implementation");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//创建Type,该类型继承 SPInterfaceBase
typeBuilder=mdlBuilder.DefineType(interfaceType.FullName+".Implementation",TypeAttributes.Class,typeof(SPInterfaceBase));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//实现所有的接口方法
EmitInterface(interfaceType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果interfaceType是基于其他接口的
foreach(Type subinterface in interfaceType.GetInterfaces())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//IDisposable不需要实现,由SPInterfaceBase实现了
if(subinterface==typeof(IDisposable))
continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
EmitInterface(subinterface);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
emittedType=typeBuilder.CreateType();
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void EmitInterface(Type type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//实现接口
typeBuilder.AddInterfaceImplementation(type);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//列出接口的成员
foreach(MemberInfo member in type.GetMembers(BindingFlags.Instance|BindingFlags.Public))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//约定-成员必须是方法,不能有属性啊,事件之类的
if(member.MemberType!=MemberTypes.Method)
throw(new Exception("Could Not Emit "+member.MemberType+" Automatically!"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取得接口中定义的方法
MethodInfo method=(MethodInfo)member;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//计算新方法的属性,在原来方法的属性上复制过来,并且不是Public/Abstract,加上Private
MethodAttributes methodattrs=method.Attributes;
methodattrs&=~(MethodAttributes.Public|MethodAttributes.Abstract);
methodattrs|=MethodAttributes.Private;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ParameterInfo[] paramInfos=method.GetParameters();
int paramlength=paramInfos.Length;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取得参数的类型数组
Type[] paramTypes=new Type[paramlength];
for(int i=0;i<paramlength;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
paramTypes[i]=paramInfos[i].ParameterType;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//在typeBuilder上建立新方法,参数类型与返回类型都与接口上的方法一致
MethodBuilder mthBuilder=typeBuilder.DefineMethod(method.Name,methodattrs,method.CallingConvention,method.ReturnType,paramTypes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//复制新方法上的参数的名字和属性
for(int i=0;i<paramlength;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ParameterInfo pi=paramInfos[i];
//对于Instance,参数position由1开始
mthBuilder.DefineParameter(i+1,pi.Attributes,pi.Name);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//指定新方法是实现接口的方法的。
typeBuilder.DefineMethodOverride(mthBuilder,method);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//在类型上定义一个字段,这个字段用于储存被方法使用的SqlCommand
FieldBuilder field_cmd=typeBuilder.DefineField("_cmd_"+method.Name,typeof(SqlCommand),FieldAttributes.Private);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//ILGenerator 是用于生成实现代码的对象
ILGenerator ilg=mthBuilder.GetILGenerator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义临时变量
LocalBuilder local_res=ilg.DeclareLocal(typeof(object));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义一个用于跳转的Label
Label label_cmd_ready=ilg.DefineLabel();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this._cmd_MethodName
ilg.Emit(OpCodes.Ldarg_0); //this
ilg.Emit(OpCodes.Ldfld,field_cmd);//._cmd_MethodName
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//if(this._cmd_MethodName!=null) 跳到 label_cmd_ready
ilg.Emit(OpCodes.Brtrue,label_cmd_ready);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果this._cmd_MethodName为null,则运行下面代码来创建SqlCommand
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this._cmd_MethodName=this.CreateCommand("MethodName");
ilg.Emit(OpCodes.Ldarg_0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this.CreateCommand
ilg.Emit(OpCodes.Ldarg_0);//参数0
ilg.Emit(OpCodes.Ldstr,SqlAccessAttribute.GetSPName(method));//参数1
//调用
ilg.Emit(OpCodes.Callvirt,typeof(SPInterfaceBase).GetMethod("CreateCommand",BindingFlags.Instance|BindingFlags.Public));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Stfld,field_cmd);// ._cmd_MethodName=
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this.DefineParameter(...)
if(paramlength!=0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//取得DefineParameter的引用
MethodInfo method_DefineParameter=typeof(SPInterfaceBase).GetMethod("DefineParameter",BindingFlags.Instance|BindingFlags.Public);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<paramlength;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//取得各参数
ParameterInfo pi=paramInfos[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this.DefineParameter(this._cmd_MethodName,"ParameterName",typeof(ParameterType),ParameterDirection.Xxx);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//参数0 - this
ilg.Emit(OpCodes.Ldarg_0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//参数1 - this._cmd_MethodName
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld,field_cmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//参数2 - "ParameterName"
ilg.Emit(OpCodes.Ldstr,pi.Name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//参数3 - typeof(ParameterType)
ilg.Emit(OpCodes.Ldtoken,pi.ParameterType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//参数4 - ParameterDirection.Xxx
if(pi.ParameterType.IsByRef)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ilg.Emit(OpCodes.Ldc_I4,(int)ParameterDirection.InputOutput);
}
else if(pi.IsOut)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ilg.Emit(OpCodes.Ldc_I4,(int)ParameterDirection.Output);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ilg.Emit(OpCodes.Ldc_I4,(int)ParameterDirection.Input);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用DefineParameter
ilg.Emit(OpCodes.Callvirt,method_DefineParameter);
}
}
//到这里 _cmd_CommandName 已经 OK 了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置label_cmd_ready就指这里
ilg.MarkLabel(label_cmd_ready);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//cmd!=null now.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(paramlength!=0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//现在要把方法的参数的值设置到SqlParameter上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MethodInfo method_SetParameter=typeof(SPInterfaceBase).GetMethod("SetParameter",BindingFlags.Instance|BindingFlags.Public);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<paramlength;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ParameterInfo pi=paramInfos[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果参数是 out 的,则不需要设置
if(!pi.ParameterType.IsByRef&&pi.IsOut)
continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this.SetParameter(this._cmd_MethodName,"ParameterName",ParameterName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Ldarg_0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld,field_cmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Ldstr,pi.Name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//取得参数值,如果参数为ValueType,则Box到Object
ilg.Emit(OpCodes.Ldarg,i+1);
if(pi.ParameterType.IsValueType)
ilg.Emit(OpCodes.Box,pi.ParameterType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Callvirt,method_SetParameter);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//现在要执行储存过程(执行SqlCommand)了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//这里根据返回值类型判断怎样执行SqlCommand
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Type returnType=method.ReturnType;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果是 void 的,则不需要返回值
bool nores=returnType==typeof(void);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
MethodInfo method_Execute=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(nores)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//不需要返回值
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteNonQuery",BindingFlags.Instance|BindingFlags.Public);
}
else if(returnType==typeof(object))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//返回object
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteScalar",BindingFlags.Instance|BindingFlags.Public);
}
else if(returnType==typeof(DataSet))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//返回DataSet
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteDataSet",BindingFlags.Instance|BindingFlags.Public);
}
else if(returnType==typeof(DataTable))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//返回DataTable
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteDataTable",BindingFlags.Instance|BindingFlags.Public);
}
else if(returnType==typeof(DataRow))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//返回DataRow
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteDataRow",BindingFlags.Instance|BindingFlags.Public);
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//返回其他类型
foreach(Type retInterface in returnType.GetInterfaces())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//如果是返回IDataReader
if(retInterface==typeof(IDataReader))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//只支持SqlDataReader
if(!returnType.IsAssignableFrom(typeof(SqlDataReader)))
throw(new Exception("SqlDataReader could not convert to "+returnType.FullName));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
method_Execute=typeof(SPInterfaceBase).GetMethod("ExecuteDataReader",BindingFlags.Instance|BindingFlags.Public);
break;
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果找不到适合的策略,
if(method_Execute==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//TODO:当然,这里应该有返回Int32,String,...的,不过懒得再写了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//抛出异常,提示不支持该返回类型,要作者改改:)
throw(new NotSupportedException("NotSupport ReturnType:"+returnType.FullName));
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//this.ExecuteXXX(this._cmd_MethodName)
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld,field_cmd);
ilg.Emit(OpCodes.Callvirt,method_Execute);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果有返回值的,则是
//local_res=this.ExecuteXXX(this._cmd_MethodName)
if(!nores)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
if(returnType.IsValueType)
ilg.Emit(OpCodes.Box,returnType);
ilg.Emit(OpCodes.Stloc,local_res);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(paramlength!=0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
//这里处理ref/out的参数
MethodInfo method_GetParameter=typeof(SPInterfaceBase).GetMethod("GetParameter",BindingFlags.Instance|BindingFlags.Public);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=0;i<paramlength;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ParameterInfo pi=paramInfos[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果不是ref/out则跳过
if(!pi.ParameterType.IsByRef&&!pi.IsOut)
continue;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//ParameterName=this.GetParameter(this._cmd_Methodname,"ParameterName")
ilg.Emit(OpCodes.Ldarg_0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Ldfld,field_cmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Ldstr,pi.Name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Callvirt,method_GetParameter);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果类型是值类型,则需要 Unbox
if(pi.ParameterType.IsValueType)
ilg.Emit(OpCodes.Unbox,pi.ParameterType);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ilg.Emit(OpCodes.Starg,i+1);
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果是 void , 则直接 return;
//否者是 return local_res , 如果返回值类型是ValueType,则需要Unbox
if(!nores)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
ilg.Emit(OpCodes.Ldloc,local_res);
if(returnType.IsValueType)
ilg.Emit(OpCodes.Unbox,returnType);
}
ilg.Emit(OpCodes.Ret);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// //throw(new NotImplementedException());
// ilg.Emit(OpCodes.Newobj,typeof(NotImplementedException).GetConstructor(new Type[0]));
// ilg.Emit(OpCodes.Throw);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public enum SqlAccessType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
StoredProcedure
//TODO:
//,SqlQuery
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
[AttributeUsage(AttributeTargets.Method)]
public class SqlAccessAttribute : Attribute
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
string _sp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public SqlAccessAttribute(string spname)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
_sp=spname;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public string StoreProcedure
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
return _sp;
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static public string GetSPName(MethodInfo method)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif) ...{
if(method==null)throw(new ArgumentNullException("method"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object[] attrs=method.GetCustomAttributes(typeof(SqlAccessAttribute),false);
if(attrs==null||attrs.Length==0)
return method.Name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ((SqlAccessAttribute)attrs[0]).StoreProcedure;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//TODO:
// public SqlAccessAttribute(SqlAccessType type,string text)
// {
//
// }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
#endregion
|