I have an input file, being the DLL of a C# piece of code, containing managed class definitions.
I also have a *.json file, who determines how the conversion should be done.

As an example, this is the C# code for a particular managed class:

namespace <Company>.<Product>.<Chapter>.Messages
  public class GetInfoMessage : TargetedClientRequestMessage
  { ...

This is how the corresponding *.JSON configuration file excerpt looks like:

  "Class": "<Company>.<Product>.<Chapter>.Messages.GetInfoMessage",
  "Fields": [ "Type" ],
  "ConstructorFields": [ "Type" ]

Currently a convertor is written for performing the "Managed-to-Native" conversion automatically, the result of this conversion looks as follows (just the header file):

class GetInfoMessage: public <Company>::<Product>::<Chapter>::messages::TargetedClientRequestMessage, public ::serialization::SerializableObjectRegistration<GetInfoMessage> {

    #pragma region private members
    InfoType type_;
    #pragma endregion private members

    explicit GetInfoMessage(InfoType type);
    explicit GetInfoMessage();

    #pragma region getters & setters
    InfoType getType() const;
    void setType(InfoType type);
    #pragma endregion getters & setters
    bool operator==(const GetInfoMessage&lhs) const;
    bool operator<(const GetInfoMessage&lhs) const;
    virtual void serialize(::serialization::Archive & ar) override;

}; // end of class GetInfoMessage

The source code of this conversion (written here in-house) is really ugly, as you can see from following excerpt:

W.WriteLine(" {");

This all makes me wonder why my company has written its own "Managed-to-Native" class definition convertor, I see two drawbacks:

  • maintainability : as soon as any new feature is used by the creators of the managed class definitions, the convertor needs to be updated.
  • uniqueness of the output : the convertor is written solely for generating C++ headerfiles (.h extension) and source code files (.cpp extension), so whenever a new language is to be used, a new convertor needs to be written (I'm particularly thinking of the R language).

Therefore I wonder: is there no other way to convert managed class types to native ones, if possible in different readable output formats?

I have access to the C# source code, in case this might be needed.

Thanks in advance

P.s. In case you wonder "But why don't you simply use the JSON as a header file?" or "Why are you using JSON like this, this is the wrong way to do it, you need to do it like this: ...", please take into account that this is the first time that I'm in contact with JSON.

  • FWIW the Android Open Source Project takes a very similar approach. – RubberDuck Feb 8 '18 at 14:07
  • Many (if not most) in-house tools like this are clunky, ugly little critters. They are meant to do the job with as little fuss and investment as possible. Not worth worrying about. – Mark Benningfield Feb 8 '18 at 16:26
  • 3
    Code-generated code is not meant to be maintained, but only regenerated as needed. – Robert Harvey Feb 8 '18 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.