动态调用类里的方法的示例(wjx)

 

//下面是需要被调用的类和方法,其中T要是传过去的类型

public class InsertToDataBaseOnBack
{ 
public string TableName{get;set;}
public void AddDataFromFileAndTemplate<T>() where T : new()
{
List<T> lstDataForAdd = new List<T>();
dvaContext CurrentContext = new CurrentContext();
Type type = CurrentContext.GetType(); //获取类型
PropertyInfo propertyInfo = type.GetProperty(TableName);//获取指定名称的属性
MethodInfo method = propertyInfo.GetMethod.ReturnType.GetMethod("AddRange");//得到Add数组的方法
object dbClass = propertyInfo.GetValue(CurrentContext, null); //拿到这个属性的值
method.Invoke(dbClass, new object[] { lstDataForAdd });
}
}

 


//下面是调用方要做的工作

Type classType = Type.GetType("Models.call_detail_records, Models");Models是dll的命名空间,call_detail_records是一个实体类
InsertToDataBaseOnBack logic = new InsertToDataBaseOnBack();
logic.TableName="call_detail_records";
MethodInfo method = typeof(InsertToDataBaseOnBack).GetMethod("AddDataFromFileAndTemplate");//定义被调用的方法
MethodInfo generic = method.MakeGenericMethod(classType);//这一步是传入一个类型
generic.Invoke(logic, null);

 

转载于:https://www.cnblogs.com/wjx-blog/p/8547418.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值