The function returns a generic type whose value is known only at runtime.

I need to use a common interface, for example:

public interface IContainer<T>
{
    IEnumerable<IContent<T>> Contents { get; }
}

An object that implements this interface is returned using a common method, such as:

IContainer<T> GetContainer<T>(IProperty property);

Type Tunknown before execution.

Using reflection, I can call the method GetContainer<T>and get the result.

My problem is that I don’t know how to list the result with the type Object(therefore, I cannot attribute it to IEnumerable).

I also tried casting as follows, but it doesn’t work (it says “Type expected”):

var myContainer = genericMethodInfo.Invoke(
                           myService, 
                           new object[] { property })
    as typeof(IContainer<>).MakeGenericType(type);

where typeis the runtime type, myServiceis the service that provides the method GetContainer<T>, and propertyhas the type IPropertyas needed.

UPDATE: . : http://stefanoricciardi.com/2010/02/18/generics-with-type-uknown-at-compile-time/

+5
6

typeof (IContainer < > ). MakeGenericType () , "" .

, , : , , , Object ( IEnumerable).

myContainer , , , IEnumerable? , .

+1

T , . , :

public interface IContainer
{
    IEnumerable<IContent> Contents { get; }
}

public interface IContainer<T> : IContainer { ... }

, .

+1

-, (double, int); typeof Type.

     object x = 0.0;
     Type t = typeof(double);
     double y = x as t; //does not compile - t is not a type - it an instance of type Type
     double y = x as typeof(double); //same as above
     double y = x as double; //compiles - double is a type
     Type z = x as Type; //compiles - Type is a type

-, :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Reflection;
using System.Diagnostics;

namespace TryThis
{
   public interface IContainer<T>
   {
      IEnumerable<IContent<T>> Contents { get; }
   }
   public interface IContent<T>
   { 
      T GetMyContent(); 
   }
   public interface IProperty 
   { }

   public class Content<T> : IContent<T>
   {
      T m_content = default(T);
      public T GetMyContent() { return m_content; }
      public Content(T val) { m_content = val; }
   }

   public class Contents<T> : IEnumerable<IContent<T>>
   {
      List<IContent<T>> m_contents = new List<IContent<T>>();
      IEnumerator<IContent<T>> IEnumerable<IContent<T>>.GetEnumerator() { return m_contents.GetEnumerator(); }
      IEnumerator IEnumerable.GetEnumerator() { return m_contents.GetEnumerator(); }
      public Contents(params T[] contents) { foreach (T item in contents) m_contents.Add(new Content<T>(item)); }
   }

   public class TestGenericContent : IContainer<int>
   {
      public IContainer<int> GetContainer(IProperty property) { return this; }
      public IEnumerable<IContent<int>> Contents { get { return new Contents<int>(1, 2, 3); } }
   }

   public static class TryThisOut
   {
      static void Test2(object o)
      {
         Type t = o.GetType();
         Type tInterface = t.GetInterface("IContainer`1"); //could be null if o does not implement IContainer<T>
         Type tGenericArg = tInterface.GetGenericArguments()[0]; //extracts T from IContainer<T>
         MethodInfo info = t.GetMethod("GetContainer");
         IProperty propArg = null; //null in this example
         object oContainer = info.Invoke(o, new object[] { propArg });

         PropertyInfo prop = tInterface.GetProperty("Contents");
         object oContents = prop.GetGetMethod().Invoke(oContainer, null);
         //oContents is of type IEnumerable<IContent<T>>, which derives from IEnumerable, so we can cast
         IEnumerable enumeratedContents = oContents as IEnumerable;

         MethodInfo getContentItem = typeof(IContent<>).MakeGenericType(tGenericArg).GetMethod("GetMyContent");
         foreach (object item in enumeratedContents)
         {            
            object oContentItem = getContentItem.Invoke(item, null);
            Debug.Print("Item {0} of type {1}", oContentItem, oContentItem.GetType());
            //...
         }
      }

      public static void Test()
      {
         object o = new TestGenericContent();
         Test2(o);
      }
   }
}
+1

.Net 4, dynamic..

0

I assume that your object will only be returned as one of a limited number of types, so why not test it before casting, for example. if the object is thisclass?

0
source

Sorry, if I misunderstood, I had problems understanding what your goal was. Have you been looking for something like this?

var myContainer = typeof(ClassWithGetContainer)
                 .GetMethod("GetContainer")
                 .MakeGenericMethod(runtimeType)
                 .Invoke(InstanceOfClassWithGetContainer, new object[] { property });
0
source

All Articles