Dynamically Creating Generic List<T> and Generic Dictionary<k,T> at Runtime Using C#

Generic List<T>

Used Namespaces

using System;
using System.Collections.Generic;

Used Concept

Generic Collection C#

Steps to create a generic List<T> at runtime

Step 1: Creating the generic List<T>

//Creating the Generic List.
Type d1 = typeof(List<>);

Step 2: Determining the type of the generic List<T>

//Creating the Type for Generic List.
Type[] typeArgs = { typeof(string) };

Step 3: Combining its Type to form the List<T>

//Now combing the List and its type.
Type makeme = d1.MakeGenericType(typeArgs);

Output

List<String> -- This created in the above step.

Step 4: Creating the Instance for List<T>

//Creating the Instance for List.
object o = Activator.CreateInstance(makeme);

Output

List<String> obj=new List<string>; -- This is created from the above step.

Step 5: Converting the Object to the List<T> object and add the item to it

//Now typecasting the object to List.
List<string> itsMe = o as List<string>;
itsMe.Add("Arun");

Step 6: Displaying the result

//Displaying the Result
Response.Write((itsMe == null) ? "Failed" : "Succeeded");

Step 7: Code snippet with Output

genericlist.gif

Generic Dictionary<k,T>

Used Concept

Generic Collection C#

Used Namespaces

using System;
using System.Collections.Generic;

Steps to create a Generic Dictionary<k,T> at runtime:

Step 1: Creating the Dictionary Type

//Creating the Dictionary Type.
Type d1 = typeof(Dictionary<,>);

Step 2: Creating Types for the KeyValue

//Creating KeyValue Type for Dictionary.
Type[] typeArgs = { typeof(string), typeof(List<string>) };

Step 3: Passing the KeyValue Type and make it a generic type

//Passing the Type and create Dictionary Type.
Type makeme = d1.MakeGenericType(typeArgs);

Step 4: Creating an instance for Dictionary<K,T>

//Creating Instance for Dictionary<K,T>.
object o = Activator.CreateInstance(makeme);

Step 5: Converting the Object to a Dictionary Object

//Typecasting the Object to Dictionary Obj.
Dictionary<string, List<string>> itsMe = o as Dictionary<string, List<string>>;

Step 6: Getting the Type casted Object

//Getting the Type of Casted object.
Type dicType = itsMe.GetType();

Step 7: Helps to get the Generic Arguments alone as collection

// This is used to check any generic arguments passed.
Type[] dicTypes = dicType.GetGenericArguments();

Step 8: Helps to determine if the typecasted object type is generic

//To check whether it is generic type.
Response.Write("Is this a generic type? {0}" + dicType.IsGenericType);

Step 9: Helps to check whether it is Generic Type definition

Response.Write("   Is this a generic type definition? {0}" + dicType.IsGenericTypeDefinition);

Step 10: Iterating the passed Generic Type arguments

//Iterate the passed generic type arguments.
foreach (Type type in dicTypes)
{
     Response.Write("" + type.IsGenericType);
}

Step 11: Creating the List<T> item and added to the value and set the key for it

//Creating the list item and adding to dictionary.
List<string> add = new List<string>();
itsMe.Add("Arun", add);

Step 12: Displaying the results

//Results
Response.Write((itsMe == null) ? "Failed" : "Succeeded");

Output for Runtime Dictionary

genericlistd.gif

Code Snippet

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace DictionaryRuntime
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateDicGenericInstance();
        }
 
        /// <summary>
        /// This is used to create instance generic list obj dynamically.
        /// </summary>
        public void CreateDicGenericInstance()
        {
            //Creating the Dictionary.
            Type d1 = typeof(Dictionary<,>);
 
            //Creating KeyValue Type for Dictionary.
            Type[] typeArgs = { typeof(string), typeof(List<string>) };
 
            //Passing the Type and create Dictionary Type.
            Type makeme = d1.MakeGenericType(typeArgs);
 
            //Creating Instance for Dictionary<K,T>.
            object o = Activator.CreateInstance(makeme);
 
            //Typecasting the Object to Dictionary Obj.
            Dictionary<string, List<string>> itsMe =
                o as Dictionary<string, List<string>>;
 
            //Getting the Type of Casted object.
            Type dicType = itsMe.GetType();
 
            // This is used to check any generic arguments passed.
            Type[] dicTypes = dicType.GetGenericArguments();
 
            //To check whether it is generic type.
            Response.Write("   Is this a generic type? {0}" + dicType.IsGenericType);
            Response.Write("   Is this a generic type definition? {0}" + dicType.IsGenericTypeDefinition);
 
            //Iterate the passed generic type arguments.
            foreach (Type type in dicTypes)
            {
                Response.Write("" + type.IsGenericType);
            }
 
            //Creating the list item and adding to dictionary.
            List<string> add = new List<string>();
            itsMe.Add("Arun", add); 

            //Results
            Response.Write((itsMe == null) ? "Failed" : "Succeeded");
        }
    }
}

Thanks for reading this article. Have a nice day.


Similar Articles