Function returning a generic type whose value is k

2019-05-01 10:52发布

I need to use a generic interface like the following:

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

An object implementing this interface is returned by a generic method like the following:

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

Type T is unknown until run-time.

Using reflection I am able to invoke the GetContainer<T> method and get the result.

My problem is that I don't know how to enumerate the result which has type Object (therefore I cannot cast it to IEnumerable).

I have also tried casting as follows but it does not work (it says "Type is expected"):

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

where type is the runtime type, myService is the service exposing the GetContainer<T> method, and property is of type IProperty as needed.

UPDATE: see my complete solution in my blog: http://stefanoricciardi.com/2010/02/18/generics-with-type-uknown-at-compile-time/

6条回答
三岁会撩人
2楼-- · 2019-05-01 11:27

Sorry if I misunderstood, I was having trouble understanding exactly what your objective was. Were you looking for something like this?

var myContainer = typeof(ClassWithGetContainer)
                 .GetMethod("GetContainer")
                 .MakeGenericMethod(runtimeType)
                 .Invoke(InstanceOfClassWithGetContainer, new object[] { property });
查看更多
SAY GOODBYE
3楼-- · 2019-05-01 11:31

typeof(IContainer<>).MakeGenericType(type) will only evaluate at runtime, while "as" needs to know the type at compiler time.

What I really don't understand is this comment: My problem is that I don't know how to enumerate the result which has type Object (therefore I cannot cast it to IEnumerable).

myContainer may be an Object but it can surely be cast to IEnumerable? If it can't then it can't be enumerated.

查看更多
beautiful°
4楼-- · 2019-05-01 11:35

If you're thinking of moving to .Net 4 that is what the dynamic type provides.

查看更多
地球回转人心会变
5楼-- · 2019-05-01 11:44

I'm assuming that your object will only be returned as being one of a restricted number of types, so why not test against those before casting e.g. if object is thisclass?

查看更多
走好不送
6楼-- · 2019-05-01 11:50

First, when casting you need a type (double, int); typeof takes a type argument and returns a class of type Type.

     object x = 0.0;
     Type t = typeof(double);
     double y = x as t; //does not compile - t is not a type - it's 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

Second, here is some example code:

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);
      }
   }
}
查看更多
ら.Afraid
7楼-- · 2019-05-01 11:51

Your type T must be known by the compiler, so this will not work. You can try making a non generic version of your interface, like this:

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

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

This way you have something to cast to and are able to use it.

查看更多
登录 后发表回答