Search code examples
c#inheritanceinitializationbond

Initializing inherited structs in Bond


I have a Bond schema that (ideally speaking) would have some inherited fields:

struct Context
{
    10: required string thing;
    20: required string otherthing;
};

struct SampleEvent : Context
{
    20: required wstring evt;
};

and when I create my derived object (SampleEvent) I can do so like this:

        SampleEvent evt = new SampleEvent { evt = str };

but where can I set up initialization of the Context fields?


Solution

  • The base's fields are inherited and can be set in the same way as the derived's fields:

    var evt = new SampleEvent {
        evt = str,
        thing = "thing1",
        otherthing = "thing2"
    };
    

    If you want to do this in a centralized place, I'd write a helper method. The generated code is partial, so you can use that functionality to add methods to the generated classes. You could also use extension methods, or a plain-old helper static method that's a factory for instances.