0

I'm trying to make something like this work.

using System;
using System.Collections.Generic;

namespace RaindancerWeb.Api {
    public class Class1 {

        public static void Main() {
            IDictionary<string, Type> dataKeys = new Dictionary<string, Type>() {
                { "GiveMeAString", typeof(string)},
                { "GiveMeAnInt", typeof(int)},
                { "GiveMeADouble", typeof(double)}
            };

            MySerialise(dataKeys);
        }

        public static void MySerialise(IDictionary<string, Type> extraDataKeys) {
            foreach (KeyValuePair<string, Type> pair in extraDataKeys) {
                string s = DoSomething<pair.Value>(pair.Key);
                Console.WriteLine(s);
            }

        }

        private static string DoSomething<T>(string key) {
            // Use the Key, to retrieve some info from Database, and parse it to type T
            // Return some serialisation of the data as a string.
            throw new NotImplementedException();
        }
    }
}

The DoSomething<pair.Value> throws an error, as I cannot use a variable as a type.

Is there a way to make something like this work?

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69
  • This may help https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method – Serg Feb 18 '22 at 10:26
  • 2
    The title asks one thing, the question another. When you use generic types you need to pass actual *types* to the type parameters, not instances of the `Type` class. You can't pass runtime values. In your case, the dictionary contains `Type` instances which is somewhat .... unusual. – Panagiotis Kanavos Feb 18 '22 at 10:27
  • @PanagiotisKanavos sorry for the confusion. It may be unusual, but it is what I'm trying to do. Do you think it can work? – Ramzi Khahil Feb 18 '22 at 10:31
  • @Serg thanks for the link. I think it is not exactly what I asked. My question is about being able to call a Method with Generics. The question you pointed out is about calling a method from that Type - which is not what I want. – Ramzi Khahil Feb 18 '22 at 10:35
  • @RamziKhahil you're trying to create a serializer. What you wrote is what you think the solution would be like. What is `DoSomething` trying to do? Why not pass the `Type` instance to it instead of trying to call it like a generic method? Generics are used when the type can be determined at *compile time*. – Panagiotis Kanavos Feb 18 '22 at 10:37
  • @PanagiotisKanavos Cause I was tasked with something wired - office politics. I can just say "not possible", but I want to actually try. Also, I find it somewhat interessting as a challenge. :) Anyway, your comment about compile time is very much answering my question. – Ramzi Khahil Feb 18 '22 at 10:42
  • You haven't explained what you want to do though, you only posted a snippet that could never work - without type constraints, a generic method can only treat values as `object` instances, because the compiler has no idea what members are available. Whatever it is you want to do is probably possible - after all, there are all kinds of serializers out there already – Panagiotis Kanavos Feb 18 '22 at 10:58
  • In fact, the built-in serialization support can probably do whatever you want to do already. Both JSON.NET and System.Text.Json can use custom type serializers for example. You can specify custom serializers for specific properties as well – Panagiotis Kanavos Feb 18 '22 at 11:00
  • @PanagiotisKanavos I wish it would be that easy. But the current system has a DB-Table, which has columns like 'type' and 'defaultValue'. Both are strings, and I should get those out. So, essentially what is in `dataKeys`, just as a DB-Table. Part of the job is reading this out, and putting it into a JSON. – Ramzi Khahil Feb 18 '22 at 11:04

0 Answers0