0

I was wandering, if I have code like this in some DLL:

public class DemoClass
{
    public void TestAction(XMParams command)
    {
        var firstName = command.Parse<string>(Params.First, "First Name");
        var lastName = command.Parse<string>(Params.Second, "Last Name");

        var userData = new UserDataDto{
            FirstName = firstName,
            LastName = lastName
        }

        command.StoreValue(userData, "User Data");
    }
}

Is it posible to detect these lines where command.Parse is used, and to extract these data: command.Parse<Type>(Index, Description)

and

command.StoreValue(Type, DescriptiveName);

Into this the list of objects which looks like this one:

public class InputParamObj
{
    public int Index {get;set;}
    public string Type {get;set;}
    public string Description {get;set;}
}
public class OutputObj
{
    public string Type {get;set;}
    public string Description {get;set;}
}
public class CommandData
{
    public List<InputParamObj> InputParams {get;set;}
    public OutputObj Output {get;set;}
}

Note, this piece of code will always be in known method for example inside the "TestAction" method.

ShP
  • 1,143
  • 1
  • 12
  • 41
  • Sounds like not a reflection task at all. Reflection is a metadata API, allowing to get info about types, methods etc, not about how and from where they are called. I think what you want is called interception and is another world. Please look here for one example of these technique (http://msdn.microsoft.com/en-us/library/dn178466(v=pandp.30).aspx) – Konstantin Chernov Sep 06 '14 at 15:12

1 Answers1

1

You don't want to do that kind of stuff via reflection. With MethodInfo.GetMethodBody() you could get the binary IL Stream for the method implementation, but good luck working with that.

You need to parse your code. Check this to see a nice list of solutions. Most of them are good, depending on your real needs. I would personally go with Roslyn, the new 'compiler as a service' from Microsoft, because from what I saw, it is really a great api: lots of power and pretty well written. But it may not suit your needs right now.

Community
  • 1
  • 1
Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37
  • Thank you, I'm looking at Mono.Cecil, it's really awesome! I will have to do some manual and dirty job to get what I want but it can be done. I will let you know if I found some better way. – ShP Sep 06 '14 at 20:12