0

I wish to implement a clone method in a dynamic type, but the problem is I can't new up said type before I've declared typeBuilder.CreateType() - I get the exception: System.NotSupportedException : The invoked member is not supported before the type is created.

Is there a way around this?

edit: Here's what I'm replicating with Emit. The cloning work itself is done in a protected constructor, and isn't done externally because I need to copy private members.

public class SomeOperatorInstance : OperatorInstance, ISomeOperatorInstance
{
    public SomeOperatorInstance() { }

    internal SomeOperatorInstance(SomeOperatorInstance source) : base(source) { }

    public override IOperatorInstance Clone()
    {
        return new SomeOperatorInstance(this);
    }
}
George R
  • 3,784
  • 3
  • 34
  • 38
  • You're probably going to need to show some code before anyone can really help you. – Iridium Jan 02 '14 at 10:09
  • 2
    No, you cant create an instance of a type which has not yet been created. but there might be a better pattern corresponding to what you wish to achieve... please clarify :) – Olivier Jan 02 '14 at 15:32
  • Are you actually trying to create an an instance of that type or just emitting code that does that? In any case, more details are needed, especially a simple example code that demonstrates this problem. – svick Jan 03 '14 at 20:51

1 Answers1

1

The problem was I was trying to use reflection to get the constructor. What I should have done (and what works) is that I give it the ConstructorBuilder that I've setup for the type. This allows me to have the type instantiate itself before actually Creating the type.

In short: Don't use reflection to get the constructor; use the ConstructorBuilder instead.

George R
  • 3,784
  • 3
  • 34
  • 38