Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to check if a partial method has been implemented

Background

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).

Requirement

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).

Problem

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.

Attempted Efforts

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
}
like image 364
musefan Avatar asked Nov 13 '13 13:11

musefan


People also ask

What will happen if there is an implementation of partial method without defining partial?

Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of the partial method then the method and its calls are removed at compile time. Compiler compiles all parts of a partial method to a single method.

What is partial class how do you use it and its benefits?

Partial classes help split the methods into two or more source(. cs) files. All the partial classes will be combined when the whole program is compiled. Partial Class is a unique feature of C#. It can break the functionality of a single class into many files.

What do you know about partial types and partial methods in C#?

A partial class or struct may contain a partial method. One part of the class contains the signature of the method. An implementation can be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.


1 Answers

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.

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.

like image 160
Marc Gravell Avatar answered Sep 28 '22 04:09

Marc Gravell