0

I want to create a class dynamically and add properties to that class dynamically and after that I want to create a object of that class and Generic list of that class and access it something like this : enter image description here

Abhay
  • 47
  • 1
  • 10
  • What did you tried? When creating a class at runtime, you can't see the properties in the designer. Unless you've implemented an interface. – Jeroen van Langen Jun 06 '17 at 13:10
  • Have you considered looking into Anonymous types? https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/anonymous-types – Droxx Jun 06 '17 at 13:13
  • Are you trying to get mixin functionality in C#? Unfortunately C3 doesnt support mixins. https://en.wikipedia.org/wiki/Mixin – Max Young Jun 06 '17 at 13:16
  • Is https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/ what you want? – mjwills Jun 06 '17 at 13:30
  • Define what you mean with "dynamically". At run time? At compile time, but flexibly? Based on user input? As it stands dozens of answers are possible. What problem are you trying to solve that a regular class or a `Dictionary` could not (effectively) solve? – Jeroen Mostert Jun 06 '17 at 13:45
  • @JeroenMostert actually I want to create a class at runtime like we normally create a class and add properties to it so I want that kind of functionality at runtime so with that class I could access the properties of that class and create List<> and all like shown in above image. – Abhay Jun 07 '17 at 04:53
  • @MaxYoung No I'm not trying to use mixin kind of functionality just create dynamic class and do all that thing we do with normal class – Abhay Jun 07 '17 at 05:10
  • @JeroenvanLangen I used ExpandoObject to create a class and use this example https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class-in-c but not worth it with my prospective – Abhay Jun 07 '17 at 05:12
  • @Abhay _"I want to create a class at runtime like we normally create a class and add properties to it so I want that kind of functionality at runtime so with that class I could access the properties of that class and create List<> and all like shown in above image."_ The more it read it, the more contradictory is becomes... It's very unclear. Runtime means **not** in visualstudio.. – Jeroen van Langen Jun 07 '17 at 06:55
  • @JeroenvanLangen whats so unclear for you, just suppose you have to create a class with some fields after you execute a program that would be _dynamic_. – Abhay Jun 07 '17 at 07:28
  • @Abhay this: => _"after you execute a program that would be dynamic"_ what would be dynamic, where would you store it? in an assembly? in mem? in a database? textfile? Are you trying to generate a .cs file? like the dataset generator? – Jeroen van Langen Jun 07 '17 at 08:56
  • @JeroenvanLangen Yes the class would be dynamic and It will be a .cs extension file & I don't want to store that class anywhere. – Abhay Jun 07 '17 at 09:59
  • @JeroenvanLangen basically my purpose of _dynamic_ _class_ is that I receive some _parameter_ from somewhere and create a _class_ with these _parameter_ as _class_ property and then create an _object_ of that _class_ and store some values in it and add that _object_ to a _generic List collection_ of that _class_ type and _return_ that collection. – Abhay Jun 07 '17 at 10:03
  • You might take a look at the [Code Generation and T4 Text Templates](https://msdn.microsoft.com/en-us/library/bb126445.aspx) – Jeroen van Langen Jun 07 '17 at 10:04

3 Answers3

1

Microsoft released a library for creating dynamic linq queries. There is a ClassFactory which can be used to create classes at runtime.

Here's an example:

class Program
{
    static void SetPropertyValue(object instance, string name, object value)
    {
        // this is just for example, it would be wise to cache the PropertyInfo's
        instance.GetType().GetProperty(name)?.SetValue(instance, value);
    }

    static void Main(string[] args)
    {
        // create an enumerable which defines the properties
        var properties = new[]
        {
            new DynamicProperty("Name", typeof(string)),
            new DynamicProperty("Age", typeof(int)),
        };

        // create the class type
        var myClassType = ClassFactory.Instance.GetDynamicClass(properties);

        // define a List<YourClass> type.
        var myListType = typeof(List<>).MakeGenericType(myClassType);

        // create an instance of the list
        var myList = (IList)Activator.CreateInstance(myListType);

        // create an instance of an item
        var first = Activator.CreateInstance(myClassType);

        // use the method above to fill the properties
        SetPropertyValue(first, "Name", "John");
        SetPropertyValue(first, "Age", 24);

        // add it to the list
        myList.Add(first);


        var second = Activator.CreateInstance(myClassType);

        SetPropertyValue(second, "Name", "Peter");
        SetPropertyValue(second, "Age", 38);

        myList.Add(second);
    }
}

You can download it here: DynamicLibrary.cs

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
-1

You can create a List of your class with:

List<ClassA> list = new List<ClassA>();

And add your created object of that class to The List with:

list.Add(dynamic);
Dennis Larisch
  • 563
  • 4
  • 19
  • I don't want to add dynamic object to _ClassA_ I want to create _ClassA_ dynamically and make a list of that class and add object of that class to List and access properties of that class like I shown in above image. – Abhay Jun 07 '17 at 05:00
-1

If you simply want the data in an undefined format, you could use Dictionary
Ex:
Dictionary < string, object > param = new Dictionary< string, object>();
param.Add("msg", "hello");
param.Add("number", 1234);

Later could be accessible as:
param["msg"] as string
param["number"] as int

LeD
  • 85
  • 1
  • 4