jeudi 28 mars 2019

Is there a way to create an object of a specific type, based on a value of a string variable (not the string type)?

I'm trying to instance multiple objects of diverse types based on what you set on the config file.

Also, I'm trying to avoid to use the 'switch' statement with every Type of object that you can instance.

My original idea was use sort type of reflection, to create a object with a Type obtained from a config value.

For example: These is a YAML Config example

workers:
  - type: "Type1"
    parameters:
      param_0: "test"
      param_1: 1000
  - type: "Type2"
    parameters:
      param_0: "test"
      param_1: 1000

When these settings are analyzed; at run-time, the program must instance a "Type1" object with the "Parameters"; and then another instance of a "Type2" object with its "Parameters".

Please let me know if you need more information about it.

PD: Sorry for my bad English.





Aucun commentaire:

Enregistrer un commentaire