0

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; }
}
8
  • Have a look at Dynamic objects Commented May 6, 2022 at 14:36
  • This also may help: stackoverflow.com/questions/3862226/… Commented May 6, 2022 at 14:37
  • 2
    why not just use Linq2Xml in order to read the xml? What exactly do you want to achieve here? Could you give an example of your data and the desired output? Commented May 6, 2022 at 14:39
  • Did you take a look at this? dynamically-add-c-sharp-properties-at-runtime Commented May 6, 2022 at 14:39
  • @McNets I have no experience with Dynamic objects with this technology yet, if this is the only option, then I will have to study it. Thanks! Commented May 6, 2022 at 14:54

1 Answer 1

1

You can use an ExpandObject which can define properties during the runtime. Below is a very simple working prototype

    static void Main(string[] args)
    {
        // Define a type with the following properties
        //  string  Name
        //  int     Age
        //  bool    Alive


        dynamic test2 = CreateInstance(
            ("Name", "John"),
            ("Age", 50),
            ("Alive", true));

        Console.WriteLine($"Name:{test2.Name} Age:{test2.Age}");
    }

    static dynamic CreateInstance(params (string name, object value)[] properties)
    {
        dynamic eo = new ExpandoObject();
        var dict = (IDictionary<string, object>)eo;

        foreach (var item in properties)
        {
            dict[item.name] = item.value;
        }

        return eo;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

It seems this is what I need! Thanks! dynamic elem = new ExpandoObject(); elem.UserName = "KUL"; elem.UserID = 0; var tName = elem.UserName.GetType(); var tID = elem.UserID.GetType();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.