Continuing on my series of “Exploring relational schema for .NET assemblies in SQL Server Modeling Services”, in this post I will drill-into entities related to ‘methods’.
Type definitions are made up of methods. A method is a subroutine defined within a type that contains a sequence of statements to perform an action. It is analogous to an “operation”. It contains a set of input and output parameters and possibly a return value. Some of the types of methods are: abstract method, constructors, destructors, accessor method and static method. Methods can be overridden by another type to provide a specific implementation for the method.
The following ER diagram shows the relationship between different entities related to the definition of a CLR method.
The MethodDefinitions entity represents details for a method definition for a type in an assembly. Let’s say you have a method “int add (int a, int b)” defined in Calculator type in Calculator assembly. When the Calculator assembly is loaded into a SQL Server Modeling Services System.Runtime database, the MethodDefinitions table is populated with the add method details. If a Calculator type was calling a method that’s defined in another assembly, that knowledge is stored in CalledMethods and MethodReferences tables. Check out the MethodDefinitions.m and Methods.m for attribute details.
Note: You can further scope these queries by assemblies and folders.
Show all abstract methods in the repository
SELECT * FROM [Repository].System_Runtime.MethodDefinitions
WHERE IsAbstract = 1
Show all methods that return ‘System.Exception’
SELECT MD.* FROM [Repository].System_Runtime.MethodDefinitions AS MD
INNER JOIN [Repository].System_Runtime.Types AS T ON MD.ReturnType = T.Id
WHERE T.QualifiedName = 'System.Exception'
Show the count of methods grouped by its access (public, private, etc.)
SELECT Access, COUNT(*) FROM [Repository].System_Runtime.MethodDefinitions
GROUP BY Access
As the name suggests, the Parameters entity captures the parameters for the methods. Check out Parameters.m, MethodDefinitions.m and Method.m for attribute details.
Show all method parameters in the repository that have type beginning with ‘System.’
SELECT T.QualifiedName, MD.* FROM [Repository].System_Runtime.Parameters AS P
INNER JOIN [Repository].System_Runtime.MethodDefinitions AS MD ON P.Method = MD.Id
INNER JOIN [Repository].System_Runtime.Types AS T ON P.Type = T.Id
WHERE T.QualifiedName like 'System.%'
Show all method parameters types and number of times they are being defined in the method definition
SELECT T.QualifiedName, Count(MD.Id)AS ParameterCount FROM [Repository].System_Runtime.Parameters AS P
GROUP BY T.QualifiedName
Show all method parameters that are “out” parameters and have type beginning with ‘System.’
SELECT T.QualifiedName, P.* FROM [Repository].System_Runtime.Parameters AS P
WHERE T.QualifiedName like 'System.%' AND
P.IsOutParameter = 1
Delegates and Events
Delegates and Events are how CLR implements the Observer design pattern. The Observer pattern defines one-to-many dependency between objects so that when on object (publisher) changes state, all its dependents (subscribers) are notified and updated automatically. This allows for loose coupling between the publishers and subscribers, so that the publishing code can change without breaking any of the subscriber code. In .NET, the events are properties of the type publishing the event.
Show all types that contain virtual events declared along with their add and remove methods
SELECT T.QualifiedName, E.Name AS EventName, AM.Name as AddMethod, RM.Name as RemoveMethod FROM [Repository].System_Runtime.Events AS E
INNER JOIN [Repository].System_Runtime.MethodDefinitions AS AMD ON AMD.Id = E.AddMethod
INNER JOIN [Repository].System_Runtime.Methods AS AM ON AM.Id = AMD.Id
INNER JOIN [Repository].System_Runtime.MethodDefinitions AS RMD ON RMD.Id = E.RemoveMethod
INNER JOIN [Repository].System_Runtime.Methods AS RM ON RM.Id = RMD.Id
INNER JOIN [Repository].System_Runtime.Types AS T ON T.Id = E.DeclaringType
WHERE E.IsVirtual = 1
To try these queries out, just load your .NET application's assemblies in the SQL Server Modeling Services database and happy analyzing!