How can I define two types that reference each other using IL Emit

I need to define something like this using Emit reflection:

public class Foo { public Bar Bar { get; set; } } public class Bar { public Foo Foo { get; set; } } 

The difficulty is that when TypeBuilder.DefineProperty () is called, I need to pass System.Type the return value of a property that does not exist yet. If this link went in only one direction, it would be easy, but in both cases the problem is with the chicken and the egg.

I was hoping to find an overload that uses TypeBuilder instead of Type, which would allow me to define both classes at the same time, and then call TypeBuilder.CreateType () at both ends. But I do not see such a thing.

What is the correct way to solve this problem?

+4
source share
1 answer

TypeBuilder is a subclass of type: MSDN

You can pass it to DefineProperty.

+4
source

Source: https://habr.com/ru/post/1299916/


All Articles