开发者

How can I define two types referencing each other using IL Emit

开发者 https://www.devze.com 2022-12-19 05:44 出处:网络
I need to define something like this using reflection Emit: public class Foo { public Bar Bar { get; set; }

I need to define something like this using reflection Emit:

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

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

The difficulty is that when calling TypeBuilder.DefineProperty(), I need to pass the System.Type of the property's return value, which doesn't exist yet. If the reference only went one way, it would be easy, but going both ways causes a chicken and egg problem.

I was hoping to find an overload that takes a TypeBuilder instead of a Type, which would let me define both classes at the same time and then call TypeBuilder.CreateType() on both at then end. But I'm not seeing s开发者_如何学编程uch thing.

What is the right way to solve this?


TypeBuilder is a subclass of Type: MSDN

You can pass it in to DefineProperty.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号