0

Is it possible to defines the type of a Generic Class using a string variable ? I mean:

string myType = "System.string"
MyClass<myType> customClass = new MyClass<myType>();

I found some kind of examples but none of them works.

Many thanks in advance.

NOTE: Normally, those types are going to be numbers: float, int, double, ... I'm getting the type from a database to make generic and abstract the code.

I'm using this example: Is it possible to serialize a list of System.Object objects using protocol buffers

Then, my problem is with the type, I won't know which one is going to be, so, using this other example (@devNull): Pass An Instantiated System.Type as a Type Parameter for a Generic Class

I don't know how to tell to use the constructor with the argument instead of the empty one.

Josete Manu
  • 187
  • 2
  • 16
  • 4
    It's possible with reflection, but the real question here is why you want to do it? This is usually a sign of a code smell. – DavidG Feb 27 '20 at 12:22
  • To serialize the information of the database in a protobuffer, i need to know the type: double, int, ... In my backend, this information is store in the database with the value of the data. Why am I using this is not relevant – Josete Manu Feb 27 '20 at 12:24
  • 2
    What examples did you find that didn't work? What about [this one](https://stackoverflow.com/q/266115/5803406)? – devNull Feb 27 '20 at 12:33
  • 1
    you can run `MakeGenericType(typeArgument)` method to make generic type with argument. Argument type can be loaded from the string value. [There is example how to do it](https://stackoverflow.com/a/266282/940182) (you have that link in the question). – oleksa Feb 27 '20 at 13:22
  • Finally that answer solves my problem, thanks! – Josete Manu Feb 27 '20 at 15:48

2 Answers2

0

You can instantiate types dynamically using Reflection. That said, if you have control over both the serialisation and the deserialisation of objects, I would recommend using a NuGet package like Newtonsoft.Json, which has ready-made functionality for serialising and deserialising your objects.

Edit: I just realised that you are only reading simple types from your database. In such a case, simple reflection will do. Examples:

var mySingle = Activator.CreateInstance(Type.GetType("System.Single", false, true));
var myInt = Activator.CreateInstance(Type.GetType("System.Int32", false, true));
var myDouble = Activator.CreateInstance(Type.GetType("System.Double", false, true));
var myString = Activator.CreateInstance(Type.GetType("System.String", false, true), "some value".ToCharArray());
Philip Atz
  • 886
  • 1
  • 10
  • 26
  • Yes, I'm using that NuGet package, but for another reason – Josete Manu Feb 27 '20 at 12:50
  • .Dump() gives me this error: 'object' doesn't contain a definition for 'Dump' .... `Activator.CreateInstance(Type.GetType("System.Single", false, true)).Dump();` – Josete Manu Feb 27 '20 at 13:22
  • 1
    your samples (and answer) are for non generic classes. However OP asks about generic classes creation – oleksa Feb 27 '20 at 13:23
  • @JoseteManu forget about `.Dump()` I accidentally left it in there because I was trying out my code in LINQPad. I have amended my answer. – Philip Atz Feb 27 '20 at 15:23
  • @oleksa it's a bit confusing: he says generic class but then he says it's going to be in a `string` variable and he lists `float`, `int`, and `double` as examples. I have given an example for some simple types; for all more complex cases of classes, I have advised the use of the `Newtonsoft.Json` NuGet package. – Philip Atz Feb 27 '20 at 15:26
-1

There are always many ways to skin a cat. A simple way to handle a small set of types, which would seem to be what you want for using protocol buffers. A small set of if tests would probably be fine

object x = ...;

if (x is int i)
    ...
else if (x is decimal d)
    ...
Jeremy Lakeman
  • 9,515
  • 25
  • 29
  • This actually answers the opposite question. The OP is asking for a way to *deserialise* a `string` while your answer is about *serialising* an object into a `string`. – Philip Atz Feb 27 '20 at 15:29
  • The OP has a string that contains the type information. The point still stands that it may be useful to check for specific values and deal with them . – Jeremy Lakeman Feb 28 '20 at 00:00