C# 动态调用泛型方法
2024-09-02 13:41:21
static void Main(string[] args)
{
#region 具体类型可传递。
Personal specifiedPersonal = new Personal(); Employee<Personal> employee = new Employee<Personal>();
employee.Create(specifiedPersonal);
#endregion #region 动态类型传递到TModel。
//假设传递一个a进来
Personal a = new Personal();
var type = typeof(Employee<>).MakeGenericType(a.GetType());
dynamic a_Context = Activator.CreateInstance(type);
var q = a_Context.Create(a);
Console.WriteLine(q); #endregion Console.ReadLine();
} public class Personal
{
public string FirstName { get; internal set; }
public string LastName { get; internal set; }
}
public interface IEmployee<TModel>
{
Guid Create(TModel model);
bool Update(TModel model);
} public class Employee<TModel> : IEmployee<TModel>
{
public Guid Create(TModel model)
{
// TODO :
return Guid.NewGuid();
}
public bool Update(TModel model)
{
// TODO :
return true;
}
}
---------------------
作者:正怒月神
来源:CSDN
原文:https://blog.csdn.net/hanjun0612/article/details/84954340
版权声明:本文为博主原创文章,转载请附上博文链接!
最新文章
- 学习之路~sqh
- 【CityHunter】通过Unity3D来制作游戏中AR部分的内容
- java生产者/消费者模式实现——一生产者一消费者(操作值)
- Stanford NLP 学习笔记2:文本处理基础(text processing)
- jquery ajax json简单的分页,模拟数据,没有封装,只显示原理
- cxx-generator JS绑定工具
- BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理
- mysql存储过程及经常使用函数
- Android 调试工具集【转】
- hdu_1429_胜利大逃亡(续)(BFS状压)
- 【知了堂学习笔记】_String、StringBuffer与StringBuilder的区别
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
- 分布式系统消息中间件——RabbitMQ的使用基础篇
- Linq中比较字符串类型的日期
- HDU 1017(** **)
- 转 Multiple outputs from T4 made easy t4生成多文件
- InfluxDB 的UTC时间问题与简单的持续查询语句
- CentOS中用Nexus搭建maven私服,为Hadoop编译提供本地镜像
- 学会使用Fidder抓取app的http请求(转)
- VS提示无法连接到已配置的开发web服务器的解决方法