How to find all the methods in an assembly that create an object of class X?

An interesting question here: In my current project, we use a custom performance monitoring kit that is very highly configured (it uses Perfmon, so we have to manually register each performance counter. For each method there is one performance counter that we monitor, and there are many of them).

I was wondering if there are any tools that would allow me to analyze the assembly of the project, find all the methods that create an instance of the XClass class, and then write them to a file? This would allow me to reduce the amount of manual configuration that I need to do by a wide margin.

Thanks Ed

EDIT

Sorry, “writing them to a file” was a little far-fetched: indeed, I need to reformat them using some additional data and write them in a configuration-specific XML format. It would be better if I could code it, so it can be configured as a build task (therefore, I do not need to run it manually), and any future changes can be made easily and documented, etc.

+3
source share
4 answers

Open the assembly in Reflector (the free version is fine); find type ( F3), then raise anaylyzer ( Ctrl+ R) and expand the "Initiated with" node.

"" node ; :

System.Data.SqlClient.SqlConnectionStringBuilder
    Depends On
    Used By
    Exposed By
    Instantiated By
        SqlDependencyProcessDispatcher.GetHashHelper(String, SqlConnectionStringBuilder&, DbConnectionPoolIdentity&, String&, String) : SqlConnectionContainerHashHelper
        System.Data.SqlClient.SqlClientFactory.CreateConnectionStringBuilder() : DbConnectionStringBuilder
        System.Web.Management.SqlWebEventProvider.Initialize(String, NameValueCollection) : Void
        System.Web.SessionState.SqlSessionStateStore.CreatePartitionInfo(String) : IPartitionInfo
        System.Web.SessionState.SqlSessionStateStore+SqlPartitionInfo.get_TracingPartitionString() : String
        System.Web.SessionState.SqlSessionStateStore+SqlStateConnection..ctor(SqlPartitionInfo, TimeSpan)
+5

, Mono.Cecil. Foo.Bar.Baz:

using Mono.Cecil;
using Mono.Cecil.Cil;

// ...

static void SearchMethod (MethodDefinition method)
{    
    foreach (var instruction in method.Body.Instructions) {
        if (instruction.OpCode != OpCodes.Newobj)
            continue;

        var constructor = (MethodReference) instruction.Operand;
        if (constructor.DeclaringType.FullName != "Foo.Bar.Baz")
            continue;

        Console.WriteLine ("new Foo.Bar.Baz in {0}", method.FullName);
    }
}

static void Main ()
{
    var module = ModuleDefinition.ReadModule ("Foo.Bar.dll");
    var methods = module.Types.SelectMany (t => t.Methods).Where (m => m.HasBody);
    foreach (var method in methods)
         SearchMethod (method);
}
+2

NDepend. : CQL ( ).

NDepend , (, ) .

, :

WHERE CreateA "MyNamespace.MyClass"

+1

You will probably want to change your design to use a factory instead, so that the factory is responsible for additional bookkeeping.

However, you can look in the System.Reflection namespace (from memory) where the Assembly class is used.

0
source

Source: https://habr.com/ru/post/1781984/


All Articles