简体   繁体   English

通过反射检查方法内部是否使用了特定的扩展方法

[英]Check via reflection if specific extension method is used inside the method

I was wandering, if I have code like this in some DLL: 我在流浪,如果我在某些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 ) 是否有可能在使用command.Parse的位置检测这些行并提取这些数据:command.Parse < 类型 >( 索引描述

and

command.StoreValue( Type , DescriptiveName ); command.StoreValue( TypeDescriptiveName );

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. 注意,这段代码将始终是已知方法中的示例,例如在“ TestAction”方法中。

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. 使用MethodInfo.GetMethodBody()您可以获取方法实现的二进制IL流,但是祝您好运。

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. 我个人将使用Microsoft的新“编译器即服务” Roslyn ,因为从我的角度来看,它确实是一个很棒的api:功能强大且编写得很好。 But it may not suit your needs right now. 但这可能不适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM