简体   繁体   English

如何确定运行时是否使用了枚举值?

[英]How do I find out if an enum value is used at Runtime?

We are using a quasi dynamic method to create enums from sql lookup tables using t4 templates. 我们正在使用一种准动态方法,使用t4模板从sql查找表中创建枚举。 The template generates an enum for every table that conforms to the lookup patter. 模板会为每个符合查询模式的表生成一个枚举。 Several of these enums are not being used in code, but any could be. 这些枚举中有几个并未在代码中使用,但可能会使用。

This has been fine and dandy, but now we have been asked to create a lookup management interface so that users can add new values, edit descriptions, etc. 这很好,但现在我们被要求创建一个查找管理界面,以便用户可以添加新值,编辑描述等。

They don't want to modify any items that are currently being used in code as an enum value, so is there a simple (or not so simple) way to query our assemblies to find out if an enum value is used? 他们不想修改当前在代码中用作枚举值的任何项目,因此是否有一种简单(或不太简单)的方式来查询我们的程序集以查明是否使用了枚举值?

We have a lot of code similar to this made-up example: 我们有很多类似于此示例的代码:

public Role GetAdminRole
{
  using (myContext ctx = new myContext()
  {
    return ctx.Roles.Where(i=> i.RoleId == (int)RoleEnum.Admin).SingleOrDefault();;
  }
}

Is there a way to use the Type.FindMembers() and build a filter that can query the internals of a method? 有没有一种方法可以使用Type.FindMembers()并构建一个可以查询方法内部的过滤器?

I've looked at the System.Reflection.Emit namespace, which seemed promising based on the EnumBuilder class, but couldn't figure out how to hook up a builder to an existing assembly. 我已经看过System.Reflection.Emit命名空间,它基于EnumBuilder类似乎很有前途,但无法弄清楚如何将生成器连接到现有程序集。 The System.Diagnostics.CodeAnalysis namespace sounds interesting, but it only contains two attributes (for suppressing warnings and excluding code from coverage). System.Diagnostics.CodeAnalysis命名空间听起来很有趣,但是它仅包含两个属性(用于抑制警告并从覆盖范围中排除代码)。

EDIT: While poking through ILSpy I discovered that I knew, but didn't put together, that enum values in methods get converted to their integer value when used in the above manner when compiled. 编辑:在通过ILSpy进行戳戳时,我发现我知道但没有放在一起,在编译时以上述方式使用时,方法中的枚举值会转换为它们的整数值。

If you insist on checking at runtime you could use the GetILAsByteArray method on the MethodBody class to get the IL and parse that , searching for places where your enumerations are used. 如果您坚持要在运行时进行检查,则可以使用MethodBody上的GetILAsByteArray方法获取IL并对其进行解析 ,以查找使用枚举的位置。

As you can imagine, this is going to be very painful, as you have to go through all of the methods in all of the types in all of the modules, in all of the assemblies. 可以想象,这将非常痛苦,因为您必须遍历所有模块中所有模块中所有类型的所有方法。

I strongly recommend that you use some sort of static analysis on the code ; 强烈建议您对代码使用某种静态分析; ReSharper , for example, can tell you whether or not members are used. 例如, ReSharper可以告诉您是否使用了成员。

If you want to code an in-house solution, you can take a look at Roslyn to analyse your code (warning, it's a CTP as of this writing); 如果您想编写内部解决方案的代码,可以看看Roslyn来分析您的代码(警告,在撰写本文时,它是CTP); you can parse the code in your project and do the analysis yourself. 您可以解析项目中的代码,然后自己进行分析。

That said, you're best off finding a tool that's going to an analysis of the code and not of the final, output assembly. 就是说,最好是找到一种工具来分析代码,而不是最终的输出程序集。

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

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