Accessing reflection generics and creating new instances of them?

9dj82nds

Member
Joined
Nov 1, 2022
Messages
10
Programming Experience
5-10
C#:
var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => String.Equals(t.Namespace, "SomeNamespace", StringComparison.Ordinal)).ToArray();
foreach (Type T in types)
{
    //Neither of these will work
    var foo = Activator.CreateInstance(typeof(`What would I put here?`).MakeGenericType(T));
    var bar = Activator.CreateInstance(T.GetType().MakeGenericType(T));

I'm trying to work my way around the above code in order to generate the objects within a namespace, so I can use it for a generic system down the line:


C#:
    public class GenericObject<T>
    {
        public T[] GetData() => DeserializeJson.GetFiles<T>(typeof(T).Name);
        public GenericTab<T> tab;
    }


1. Is there any way I can do something like `new GenericObject<typeof(T)>()`
2. If not, is there any way I can get the classes from the `SomeNamespace` and assign them to `GenericObjects<T>` in a different way?
 
That has nothing to do with generics, Foo is not a generic type. Activator.CreateInstance(types[0])
Why are you telling me this has nothing to do with generics, I know it's not. You're not reading the code so I need to make it as simple as I can, this is causing the divergence of my original question. If you took two seconds to actually visit the fiddle https://dotnetfiddle.net/jyTyYJ you would know you cannot do what you're suggesting:

1667432110160.png

1667432139078.png


You cannot get a type from a runtime generic, I'm looking for any other way to accomplish what I am trying to do.
 
You say types[0] represent the Type object for Foo class.
C#:
var instance = Activator.CreateInstance(types[0]) // a Foo instance
done.
 
You say types[0] represent the Type object for Foo class.
C#:
var instance = Activator.CreateInstance(types[0]) // a Foo instance
done.
Okay thank you, and what I'm trying to do with my last question is there any way to use that instance (That has been created from the activator) to create a generic class without using the concrete type: `new Foo<theTypeOfInstance>();` Even by using reflection?
 
I thought that was already answered in this thread?
 

C#:
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Something
{
    public class Foo
    {
        string bar;
    }

    public class Bar
    {
        string bar;
    }
}

public class Test<T>
{
}

public class Program
{
    public static void Main()
    {
        var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == "Something");
        List<Type> myTypes = new List<Type>();
        foreach (Type T in types)
        {
            var generic = typeof(Test<>);
            var specific = generic.MakeGenericType(T);
            var instance = Activator.CreateInstance(specific);
            Console.WriteLine(instance);
        }
    }
}
 

C#:
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;

namespace Something
{
    public class Foo
    {
        string bar;
    }

    public class Bar
    {
        string bar;
    }

    public class Test<T>
    {
    }

    public class GenericObject<T>
    {
    }
}


public class Program
{
    public static void Main()
    {
        var types = Assembly.GetExecutingAssembly()
                            .GetTypes()
                            .Where(t => t.Namespace == "Something")
                            .ToList();
        var concreteTypes = types.Where(t => !t.IsGenericType);
        var genericTypes = types.Where(t => t.IsGenericType);

        foreach (var concrete in concreteTypes)
        {
            var instance = Activator.CreateInstance(concrete);
            Console.WriteLine(instance);
        }

        
        foreach (var generic in genericTypes)
        {
            foreach(var concrete in concreteTypes)
            {
                var specific = generic.MakeGenericType(concrete);
                var instance = Activator.CreateInstance(specific);
                Console.WriteLine(instance);
            }            
        }
    }
}
 
It is not possible to do any of the options you are trying there. You must do as explained before:
  1. get the Type for the generic class and the Type for each type argument,
  2. use MakeGenericType to make a constructed generic Type,
  3. use Activator.CreateInstance on that type.
 
My posts #21 and #22 demonstrate how to do the steps outlined above.
 
Back
Top Bottom