This project is read-only.

Copy an interface and a few types to a new assembly

Topics: PE reader, PE Writer
May 8, 2010 at 3:35 AM



Here's a problem I'm having - every month I receive a DLL from another department. I open it in Reflector, decompile and copy the stuff I need to a project in VS. It would be great if I could automate the process with CCI Metadata. There are a few interfaces I'm interested in and I have their names, I need those interfaces, all classes that implement them and all classes/interfaces that are used as parameters/return types by the former. How can I copy those to a new DLL?


With thanks,


May 8, 2010 at 1:33 PM

Load the assembly, make a mutable copy leaving out what you don't need or removing what you don't need, possibly change the assembly name and location and save the resulting assembly. You will probably have to do it in two steps: collecting enough info to decide what you want to copy and the copy. You can do it using the visitor pattern or not, depending on your mood and complexity of the task.

May 8, 2010 at 1:40 PM

Hi perplexed,

I'm trying to do it by overriding Visit, but can't find the correct overload, which will let me remove types. void Visit(TypeDefinition typeDefinition) almost works, only the types come emtpy, without any fields, properties. After ITypeReference Visit(ITypeReference reference) PeWriter throws exception, doesn't like the nulls I've returned for types I don't want.

		public override ITypeReference Visit(ITypeReference reference)
			if(reference == null) return null;


			if ( <name OK> )
				return reference;

			return null;

May 8, 2010 at 3:20 PM
Edited May 8, 2010 at 3:28 PM

The visitor pattern is used in many ways in CCI. Try checking the source code of the class you are subclassing, particularly wrt the methods you are overriding.

A type definition and a type reference are not the same thing.

I'd suggest you to try and do it without any visiting device. That way you can focus on the meaning of the various kinds of metadata objects and how they are related to each other. Otherwise you are just wasting your time.