使用反射检查是否已经实现了部分方法 [英] Using reflection to check if a partial method has been implemented

查看:51
本文介绍了使用反射检查是否已经实现了部分方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在使用 Linq to SQL,因此生成了一个包含自动生成类的 DBML 文件.生成过程的一部分为某些操作创建部分方法,在我的例子中,我感兴趣的两个方法与表记录的插入和更新相关.这些部分方法是根据 DBML 设计器中创建的每个表生成的,例如:

I am using Linq to SQL and thus have a generated DBML file containing auto-generated classes. Part of the generation process creates partial methods for certain actions, and in my case the two methods I am interested in related to the Insert and Update of table records. These partial methods are generated per each Table created in the DBML designer, for example:

partial void InsertMyTable(MyTable instance);
partial void UpdateMyTable(MyTable instance);

现在,我的应用程序设计的一部分要求为每个表始终实现这两个部分方法.(它们本质上用于向正在插入/更新的记录添加时间戳).

Now part of the design of my application requires that these two partial methods are always implemented for every single table. (They are essentially used to add a timestamp to the record being inserted/updated).

要求

我有一个单元测试项目,虽然这可能不是常见的做法,但我想包括一些测试,以确保某些事情已正确实施.在这种情况下,我想确保开发人员记得实现上面提到的部分方法(我不关心实际的实现,只关心它们已经实现了).

I have a unit test project, and although this may not be common practice I want to include a few tests that ensure certain things have been implemented properly. In this case I want to ensure that the developers have remembered to implement the partial methods mentioned above (I do NOT care about the actually implementation, only that they have been implemented).

问题

我需要做的是使用反射来检查每个部分方法是否已实现,但我无法确定如何确定.

What I need to do is use reflection to check if each partial method has been implemented, but I am having trouble working out how to determine that.

尝试过的尝试

到目前为止,我已经设法获得了包含在数据上下文中的方法列表,并且能够将其与每个表预期的方法进行比较.问题是我找不到一种方法来确定给定的部分方法是否确实具有实现:

So far I have managed to get a list of methods contained within the data context, and I am able to compare that with the methods expected for each table. The problem is that I can't find a way to determine if a given partial method actually has an implementation:

var methods = (typeof(MyDataContext)).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
foreach (var method in methods) 
{
    Console.WriteLine(method.Name);
    //how to check if method is implemented, or just an unimplemented partial
}

推荐答案

您已有的工作.如果partial 方法没有实现,那么它根本不存在.没有您可能会通过反射意外找到的声明存根".部分方法要么有一个实现,要么被编译器完全删除.

What you have already works. If the partial method doesn't have an implementation, then it does not exist at all. There is no "declaration stub" that you might find by reflection accidentally. Either the partial method has an implementation, or it is removed completely by the compiler.

所以基本上:如果您找不到带有 GetMethod/GetMethods 的方法(使用适当的 BindingFlags,根据问题)- 那么它没有被实施.

So basically: if you can't find the method with GetMethod / GetMethods (with the appropriate BindingFlags, as per the question) - then it wasn't implemented.

这篇关于使用反射检查是否已经实现了部分方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆