Help me, is it possible in C# to make a dynamically created class
whose number of properties and their types
are not known in advance?
For example, there is a POCO class and it is necessary to load a file template (for example - XML) when loading, in which the names of objects (properties), their types and values will be specified.
It seems to me that this can be done through a dictionary
and a wrapper class
(maybe I'm wrong and this is a bad decision). But I can't figure out how to dynamically unpack from object to the correct realy class without creating dozens of as
conditions?
public static class Program
{
public static void Main()
{
Dictionary<string, ElemType> keys = new Dictionary<string, ElemType>();
keys.Add(
"UserID", new ElemType() { NameType = "System.Int32", ValueType = "123" }
);
keys.Add(
"UserName", new ElemType() { NameType = "System.String", ValueType = "abc" }
);
var type = Type.GetType(keys["UserID"].NameType);
///var elem = (???type)Activator.CreateInstance(type);
}
}
public class ElemType
{
public string NameType { get; set; }
public object ValueType { get; set; }
}