Question

I am removing replication from SQL Server, so I need to search for code that by mistake (or auto generated code) read the column rowguid.

To prevent any problem, I need to search in assemblies on production server to make sure all assemblies were updated. And find some legacy assembly that I don´t know.

For applications that uses Entity Framework, I need to find properties with rowguid name. But old code uses code like table["rowguid"]. So I need to find all references to that string. How can I do that?

I am using this code. It works but is inefficient:

foreach (var assemblyFile in allAssemblies)
{
    AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(assemblyFile.FullName, parameters);

    AstBuilder astBuilder = new AstBuilder(new DecompilerContext(assemblyDefinition.MainModule));
    astBuilder.AddAssembly(assemblyDefinition);

    using (StringWriter output = new StringWriter())
    {
        astBuilder.GenerateCode(new PlainTextOutput(output));
        string result = output.ToString();
        if (result.IndexOf("rowguid", StringComparison.OrdinalIgnoreCase) > -1)
        {
            Console.WriteLine(assemblyFile.FullName + ": ");
            Console.WriteLine();
            Console.WriteLine(result);
        }
    }
}

No correct solution

OTHER TIPS

I think the below sample code will be helpful

 public static class StringHelper
{
    public static void ReplaceString(string old, string replacement, AssemblyDefinition asm)
    {
        foreach (ModuleDefinition mod in asm.Modules)
        {
            foreach (TypeDefinition td in mod.Types)
            {
                IterateType(td, old, replacement);
            }
        }
    }
    public static void IterateType(TypeDefinition td, string old, string replacement)
    {
        foreach (TypeDefinition ntd in td.NestedTypes)
        {
            IterateType(ntd, old, replacement);
        }

        foreach (MethodDefinition md in td.Methods)
        {
            if (md.HasBody)
            {
                for (int i = 0; i < md.Body.Instructions.Count - 1; i++)
                {
                    Instruction inst = md.Body.Instructions[i];
                    if (inst.OpCode == OpCodes.Ldstr)
                    {
                        if (inst.Operand.ToString().Equals(old))
                        {
                            inst.Operand = replacement;
                        }
                    }
                }
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top