-1

I have a variable defined as:

Generate<IResource> generation = FindComponent<Generate<IResource>>();  

Which finds the first component of this type.

But i need to now get the type of IResource from the variable generation so i know what resource is in use. But i am not sure how to obtain it from the variable. Is this possible?

WDUK
  • 1,412
  • 1
  • 12
  • 29

1 Answers1

1

In theory, the following would do, using the Type.GetGenericArguments method:

generation.GetType().GetGenericArguments()[0]);

Check this fiddle

using System;
            

public class Generate<IResource> {
}

public interface IResource {}
public class Resource: IResource {}

public class Program
{
    public static void Main()
    {
        Generate<Resource> generation = new Generate<Resource>();
        Console.WriteLine(generation.GetType().GetGenericArguments()[0]);
    }
}

Output: Resource

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61