Copy layer attributes to new Instance Definition by layer name in C#

Related to my last post, a user called archimax wrote some awesome code that lets you insert a single block from an external file into your model.

The only downside is that it seems to require that the layer structure be exactly the same as the source file. This breaks it when you import into existing files with existing and/or different layer structure. I go back to work on old stuff a lot and lose the use of this cool script whenever I change, expand or improve my library and methods.

Anybody have any idea how I could adjust the below so that layers are assigned by layer name and not just copied with object attributes? It looks like the layer index is what gets copied across, rather than a layer name. I’m not a complete C# novice, but this is just a bit above my comprehension level and I’ve spent the better part of a week’s free time chipping away at it.