can you hold a generic type variable c# code example

Example 1: c# reflection create generic type

Type generic = typeof(Dictionary<,>);

// Create an array of types to substitute for the type parameters of Dictionary. The key is of type string, and the type to be contained in the Dictionary is Test.
Type[] typeArgs = { typeof(string), typeof(Test) };

// Create a Type object representing the constructed generic type.
Type constructed = generic.MakeGenericType(typeArgs);

var instance = Activator.CreateInstance(constructedType);

Example 2: c# store generic type without arguments

public interface IModel<out T> where T : class
{
    T Value { get; }
}

public class Model<T> : IModel<T> where T : class
{
    public T Value { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        var foo = new Model<string>()
        {
            Value = "hello world",
        };

        IModel<object> boo = foo;

        Console.WriteLine(boo.Value);
    }
}