Is it possible to export a dll definition from my

2019-08-23 01:00发布

I have code in my codebase I'm unfamiliar with that I'm looking to clean up and lock down with test cases.

The problems I'm facing are that

A) the developer who wrote it is no longer with the company, and it looks like no standards were used and no code review took place, so there aren't even comments (variables are named a, b, and c and functions are 200 lines long...ugh)

B) the developer who wrote it had a square-peg-round-hole mentality (i.e. "hey, this new feature I just learned about is really cool, I think I'll abuse the heck out of it")

C) it's incredibly messy, undocumented, and a non-trivial task to rewrite its functionality

That said, the code is spaghetti I've narrowed down a section that makes a large number of Reflection.Emit calls to dynamically make a dll that performs the work. This is hokey beyond belief to accomplish the task at hand, but see point B).

Half of me wants to say "forget this" and just rewrite the code so I can lock it down with tests. The other half wants to salvage what's there, because I know it works, my customers use the code on a daily basis, and it's not going to be easy.

Is there any way to do something like the following?

foreach(Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
    if(IS_Assembly_IM_LOOKING_FOR(a))
    {
        SaveToFile(a, @"C:\Temp\MyAssembly.dll")
    }
}

so that I can have a "locked down" version of the assembly and I can write unit tests for it in isolation and have a 0 percent chance of ever breaking it by accidentally modifying the code that dynamically creates it?

标签: c# reflection
1条回答
劳资没心,怎么记你
2楼-- · 2019-08-23 01:43
  1. You can modify the code that creates the assembly such that it saves it to the disk (see this SO question)
  2. You might also succeed doing that using a debugger (such as WinDbg), which can dump an assembly from memory into file.
查看更多
登录 后发表回答