到处寻找一个枚举转换成字符串

到处寻找一个枚举转换成字符串

本文介绍了到处寻找一个枚举转换成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图找到处处一个解决方案,一个特定的枚举转换为字符串,不管是不是的ToString()显式调用。 (这些被替换用枚举说明,以提高混淆转换。)

I'm currently trying to find everywhere in a solution where a specific enum is converted to a string, whether or not ToString() is explicitly called. (These are being replaced with a conversion using enum descriptions to improve obfuscation.)

例如:我想找到的代码,如字符串str =值:+ SomeEnum.someValue;

Example: I'd like to find code such as string str = "Value: " + SomeEnum.someValue;

我试过用含隐式转换为枚举的包装类替换枚举本身类型和压倒一切的ToString()的包装类,但是当我尝试搜索的的ToString使用()重写它给我的解决方案中的地点列表,其中的ToString()调用上的任何东西(并且只有在它被显式调用)。搜索是在Visual Studio ReSharper的完成。

I've tried replacing the enum itself with a wrapper class containing implicit conversions to the enum type and overriding ToString() in the wrapper class, but when I try searching for uses of the ToString() override it gives me a list of places in the solution where ToString() is called on anything (and only where it is called explicitly). The search was done with ReSharper in Visual Studio.

有另一种方式来找到这些枚举到字符串转换?在整个解决方案去手动听起来并不像很多乐趣。

Is there another way to find these enum-to-string conversions? Going through the entire solution manually doesn't sound like much fun.

推荐答案

在罗斯林的诀窍是使用 SemanticModel.GetTypeInfo()然后检查 ConvertedType 来找到这些类隐式转换的。

The trick in Roslyn is to use SemanticModel.GetTypeInfo() and then check the ConvertedType to find these sort of implicit conversions.

一个完整的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;
using Roslyn.Services.CSharp;
using Roslyn.Compilers.Common;

class Program
{
    static void Main(string[] args)
    {
        var code = @"enum E { V } class { static void Main() { string s = ""Value: "" + E.V; } }";
        var doc = Solution.Create(SolutionId.CreateNewId())
            .AddCSharpProject("foo", "foo")
            .AddMetadataReference(MetadataFileReference.CreateAssemblyReference("mscorlib"))
            .AddDocument("doc.cs", code);
        var stringType = doc.Project.GetCompilation().GetSpecialType(SpecialType.System_String);
        var e = doc.Project.GetCompilation().GlobalNamespace.GetTypeMembers("E").Single();
        var v = e.GetMembers("V").Single();
        var refs = v.FindReferences(doc.Project.Solution);
        var toStrings = from referencedLocation in refs
                        from r in referencedLocation.Locations
                        let node = GetNode(doc, r.Location)
                        let convertedType = doc.GetSemanticModel().GetTypeInfo(GetNode(doc, r.Location)).ConvertedType
                        where convertedType.Equals(stringType)
                        select r.Location;
        foreach (var loc in toStrings)
        {
            Console.WriteLine(loc);
        }
    }

    static CommonSyntaxNode GetNode(IDocument doc, CommonLocation loc)
    {
        return loc.SourceTree.GetRoot().FindToken(loc.SourceSpan.Start).Parent.Parent.Parent;
    }
}

这篇关于到处寻找一个枚举转换成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:30