C# 4.0 实现 Method Missing
Method Missing 指在我们调用一个不存在的函数时,系统将此调用转给一个我们定义的函数,一个比较典型的应用是 RoR 中的 find_by 语法:
C# 4.0 并没有像 Boo 那样直接支持 Method Missing,但是通过动态对象,确实可以做到。我们通过继承 DynamicObject 并 override TryInvokeMember 方法,就可以创建出一个处理不存在的函数的类。以下的代码展示了给 DbEntry 增加动态 find_by 支持的方法:
![](/assets/blank.gif)
![](/assets/blank.gif)
{
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var ss = binder.Name.Split(new[] { "And" }, StringSplitOptions.None);
if (ss.Length != args.Length)
{
throw new ApplicationException("The args count doesn't match method call " + binder.Name + "");
}
Condition c = null;
for (int i = 0; i < ss.Length; i++)
{
c &= CK.Column[ss[i]] == args[i];
}
result = DbEntry.Context.GetObject<T>(c);
return true;
}
}
上面的代码不是很严谨,比如使用 And 进行 Split,如果字段名是 Andriod,就会出异常,不过,只为测试是够了,下面是测试代码:
![](/assets/blank.gif)
![](/assets/blank.gif)
{
public abstract string Name { get; set; }
public abstract int Age { get; set; }
public abstract User Init(string name, int age);
public static dynamic FindBy
{
get { return new DynamicQuery<User>(); }
}
}
class Program
{
static void Main(string[] args)
{
DbEntry.Context.DropAndCreate(typeof(User));
User.New.Init("tom", 18).Save();
User.New.Init("jerry", 99).Save();
User.New.Init("mike", 34).Save();
var u = User.FindBy.Name("tom");
Show(u);
u = User.FindBy.Age(99);
Show(u);
u = User.FindBy.NameAndAge("jerry", 27);
Show(u);
u = User.FindBy.NameAndAge("mike", 34);
Show(u);
Console.WriteLine("The End");
Console.ReadLine();
}
static void Show(dynamic u)
{
if (u == null)
{
Console.WriteLine("<NULL>");
}
else
{
Console.WriteLine("Item: {0},{1},{2}", u.Id, u.Name, u.Age);
}
Console.WriteLine("-------------------------------------");
}
}
运行结果:
Item: 1,tom,18
-------------------------------------
Item: 2,jerry,99
-------------------------------------
<NULL>
-------------------------------------
Item: 3,mike,34
-------------------------------------
The End
和 Boo 的 Method Missing 支持相比,这种方式只能工作在动态对象上,但是我并不希望全盘动态化,所以,这里实现的 FindBy 和字段名之间有一个点,使之看起来不像 Method Missing,倒是有点像连贯接口,这一点有些不爽,但是毕竟还是提供了以前不可能实现的应用,多了更多的可能性。
转载于:https://www.cnblogs.com/lephone/archive/2010/03/07/cs4_implements_method_missing.html
C# 4.0 实现 Method Missing相关推荐
- The POM for com.ruifeng.tjtaxiqy:shiro:jar:0.0.1-SNAPSHOT is missing, no dependency information avai
spring boot多模块项目打包时报错The POM for com.ruifeng.tjtaxiqy:shiro:jar:0.0.1-SNAPSHOT is missing, no depend ...
- Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration
今天用SpringBoot2.3.0整合swagger2.2.2版本的时候,启动SpringBoot报如下错: Error starting ApplicationContext. To displa ...
- 错误:Parameter 0 of method sqlSessionFactory in MyBatisConfig required a bean of type ‘javax.sql.DataS
SpringBoot启动错误: *************************** APPLICATION FAILED TO START ***************************D ...
- Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration req
项目场景: 整合springboot项目并对springboot项目进行版本升级 问题描述: 错误信息: Description:Parameter 0 of method linkDiscovere ...
- 构建maven项目:The POM for XXX:jar:1.0.0.RELEASE is missing, no dependency information available
问题描述: 构建maven项目打包时,报:The POM for org.springblade:blade-pay-util-api:jar:1.0.0.RELEASE is missing, no ...
- Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration required a bean of type 'xxx. ...
- springboot启动报错Parameter 0 of method a in com.* required a bean of type 'java.lang.String' that could
springboot服务启动报错,报错信息如下 *************************** APPLICATION FAILED TO START ******************** ...
- Parameter 0 of method redisTemplate in org
服务提供方在使用这个redis的时候,因为引入错误的依赖,所以导致出现下面的报错.以及消费方也报了一堆错误 Error starting ApplicationContext. To display ...
- The POM for oracle:ojdbc6:jar:11.2.0.3 is missing, no dependency information available
The POM for oracle:ojdbc6:jar:11.2.0.3 is missing, no dependency information available 项目编译时报错: [ERR ...
最新文章
- 为提高能量利用效率,大脑会对感官进行预测
- jQuery DateTimePicker 日期和时间插件
- 【实用】CTS请求号传输报错处理
- python类型提示包 检查静态类型_Pyright:微软提供的Python静态类型检查器
- 应用传送网络(ADN):率先架起“东数西算”的“高速公路”
- Intel超线程技术 Hyper-Threading Technology (7) - 最早的一篇超线程论文介绍
- Java SSM框架之MyBatis3(六)MyBatis之参数传递
- 太原理工大学ICPC队介绍(2020版)
- 多线程编程核心技术日记
- 如何在 Mac 中对文档进行签名?
- 平衡二叉树---- AVL树
- 最新版QQ拼音打字法为什么打出来的字中间有竖线?
- 【网络信息安全】网络信息安全基础
- 区块链核心技术演进之路-共识机制演进
- ps照片人脸合成教程-----------------------------------
- 索引导致大量写入数据时效率低下
- 综述论文要写英文摘要吗_攻略|一些小论文撰写的建议
- 什么是RSS?RSS是什么
- Machine learning system design - Error analysis
- 漫谈微信libco协程设计及实现(万字长文)