Change to C# code generator

Hello,

I am upgrading a .Net 4.8 project from ServiceStack 5.9.2 to 10.0.6 (still on .Net 4.8). The upgrade was mostly painless however changes to the CSharpGenerator causes a problem for me. I had 2 issues related to the collections and I was able to fix one but not the other.

The new code to initialize a collection uses C#12 syntax (e.g.: = new(); and = [];) which is not compatible with my .Net 4.8 project stuck on C#7.3

Do you have suggestion on how to fix this problem? I can manually fix the generated code using a regex search&replace but I would like to avoid manual steps that if possible.

Thanks,
Alex

Do you have InitializeCollections: True uncommented? because it should default to False in which case it wouldn’t generate the collection initializers.

Alternatively you could custom DTOs on the fly and doing the source code search/replace there by registering an implementation of INativeTypesFormatter in your IOC:

public interface INativeTypesFormatter
{
    void AddHeader(StringBuilderWrapper sb, ILangGenerator gen, IRequest req);
    string Transform(string code, ILangGenerator gen, IRequest req);
}

You can detect the language with gen.Lang and do any source code transformations in your custom Transform() implementation.

Yes, I have InitializeCollections uncommented since I want my collections to be initialized to have the same behavior as I had with ServiceStack 5.9.

I’ll look into using INativeTypesFormatter.

Thanks