vendredi 9 octobre 2020

C# set a generic type based in input [duplicate]

I know this is possible, however I don't know exactly how and after 90 minutes of Google searches I'm stuck.

You have a generic c# class...

  public class MyField<T>
    {
        public string Name { get; set; }
        public T Value { get; set; }

        public MyField(string name, T value)
        {
            Name = name;
            Value = value;
        }
    }    
}

I want to set the type of value based in input (XML file, but let's put that aside).

Here is the code that I'm trying to get working...

 class Program
    {
        static void Main(string[] args)
        {
             
            string strDataType = "int";
            var t = new Type [] { typeof(strDataType) };
            Field<t> field = new Field<t>(true, 1, "Acme", 'R', 'N', 1, 1, 2, 12);

    
            Console.WriteLine("Press Enter to continue");
            Console.ReadLine();
        }
    }    

Here is the out put I am getting...

enter image description here

So how based on input can I set the type of value to int or DateTime?

Thank You





Aucun commentaire:

Enregistrer un commentaire