Although this question is similar to this question, How do I attach a method to a dynamically-created C# type at runtime?, it does not contain the answer I am looknig for.
I am creating a type dynamically, and then saving this in a dynamic assembly DLL, which can be used in another project.
However, I want to "attach" a C# static method to this dynamic type and call it in the constructor. (since this method is a little complicated).
One way this can be done is to write the method in C# in another project, then use reflection in the dynamic type to invoke it. However, this would mean that the dynamic type will have to ship with a second assembly.
Another way would be to create the method dynamically and then write the IL for this. I would like to avoid this, since I think it would take too much effort. Is there a way to take an existing method written in C# and just "copy" it to the dynamic type?
Update
I am currently creating the new type using AssemblyBuilder, and ModuleBuilder.DefineType.
I have already tried grabbing the method body using MethodInfo.GetMethodBody().GetILAsByteArray() and then setting the newly defined method body using MethodBuilder.CreateMethodBody(), but does not work for some reason. I noticed that the IL code is only ~450 bytes, which seems too small for me, because there are several string literals in there that would easily use up this space. I am guessing there is some additional things I need to do to make this work.
As a side question, is it possible to copy an entire type into my dynamic assembly?