Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast a generic type at runtime in c#

I need to create an IEnumerable<IEnumerable<T>> when I only know T at runtime.

I have built up my collection like so:

new List<List<object>>() 

where all the objects in the inner list are a T

However because of co/contravariance (can never remember which it is!) my List of Lists isnt an IEnumerable of IEnumerables.

What can I do about this?

I've tried using Convert.ChangeType but it moans that List isn't IConvertible

Clue: Read the question. Again. I said I only know T at runtime.

like image 926
Andrew Bullock Avatar asked Oct 06 '11 15:10

Andrew Bullock


People also ask

What is generic in runtime?

When a generic type is first constructed with a value type as a parameter, the runtime creates a specialized generic type with the supplied parameter or parameters substituted in the appropriate locations in the MSIL. Specialized generic types are created one time for each unique value type that is used as a parameter.

Can dynamic type be used for generic?

Not really. You need to use reflection, basically. Generics are really aimed at static typing rather than types only known at execution time. To use reflection, you'd use Type.

How do you find a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.


4 Answers

OK, based on Master Morality's answer, I've come up with this. Shockingly simple.

public static IEnumerable Cast(this IEnumerable self, Type innerType)
{
    var methodInfo = typeof (Enumerable).GetMethod("Cast");
    var genericMethod = methodInfo.MakeGenericMethod(innerType);
    return genericMethod.Invoke(null, new [] {self}) as IEnumerable;
}

Simple. Blogged about it here: Casting an enumerable when the inner type is only known at runtime

like image 151
Andrew Bullock Avatar answered Oct 03 '22 08:10

Andrew Bullock


I've had similar issues with TinyIoC, and rather than "converting", the "cleanest" solution I've found is to make your method generic (so public IEnumerable'T DoStuff'T()), then call that using MakeGenericMethod using your runtime type. It stays "clean" because your actual method that constructs the list just operates as if it is a normal generic method, so it doesn't get cluttered with casting etc.

Without seeing your code it's hard to know if that fits the bill - here's the relevant bits to make the generic method from TinyIoc:

public static class TypeExtensions
{
    private static SafeDictionary<GenericMethodCacheKey, MethodInfo> _genericMethodCache;

    static TypeExtensions()
    {
        _genericMethodCache = new SafeDictionary<GenericMethodCacheKey, MethodInfo>();
    }

    /// <summary>
    /// Gets a generic method from a type given the method name, binding flags, generic types and parameter types
    /// </summary>
    /// <param name="sourceType">Source type</param>
    /// <param name="bindingFlags">Binding flags</param>
    /// <param name="methodName">Name of the method</param>
    /// <param name="genericTypes">Generic types to use to make the method generic</param>
    /// <param name="parameterTypes">Method parameters</param>
    /// <returns>MethodInfo or null if no matches found</returns>
    /// <exception cref="System.Reflection.AmbiguousMatchException"/>
    /// <exception cref="System.ArgumentException"/>
    public static MethodInfo GetGenericMethod(this Type sourceType, System.Reflection.BindingFlags bindingFlags, string methodName, Type[] genericTypes, Type[] parameterTypes)
    {
        MethodInfo method;
        var cacheKey = new GenericMethodCacheKey(sourceType, methodName, genericTypes, parameterTypes);

        // Shouldn't need any additional locking
        // we don't care if we do the method info generation
        // more than once before it gets cached.
        if (!_genericMethodCache.TryGetValue(cacheKey, out method))
        {
            method = GetMethod(sourceType, bindingFlags, methodName, genericTypes, parameterTypes);
            _genericMethodCache[cacheKey] = method;
        }

        return method;
    }

    private static MethodInfo GetMethod(Type sourceType, BindingFlags bindingFlags, string methodName, Type[] genericTypes, Type[] parameterTypes)
    {
        var methods =
            sourceType.GetMethods(bindingFlags).Where(
                mi => string.Equals(methodName, mi.Name, StringComparison.InvariantCulture)).Where(
                    mi => mi.ContainsGenericParameters).Where(mi => mi.GetGenericArguments().Length == genericTypes.Length).
                Where(mi => mi.GetParameters().Length == parameterTypes.Length).Select(
                    mi => mi.MakeGenericMethod(genericTypes)).Where(
                        mi => mi.GetParameters().Select(pi => pi.ParameterType).SequenceEqual(parameterTypes)).ToList();

        if (methods.Count > 1)
        {
            throw new AmbiguousMatchException();
        }

        return methods.FirstOrDefault();
    }

    private sealed class GenericMethodCacheKey
    {
        private readonly Type _sourceType;

        private readonly string _methodName;

        private readonly Type[] _genericTypes;

        private readonly Type[] _parameterTypes;

        private readonly int _hashCode;

        public GenericMethodCacheKey(Type sourceType, string methodName, Type[] genericTypes, Type[] parameterTypes)
        {
            _sourceType = sourceType;
            _methodName = methodName;
            _genericTypes = genericTypes;
            _parameterTypes = parameterTypes;
            _hashCode = GenerateHashCode();
        }

        public override bool Equals(object obj)
        {
            var cacheKey = obj as GenericMethodCacheKey;
            if (cacheKey == null)
                return false;

            if (_sourceType != cacheKey._sourceType)
                return false;

            if (!String.Equals(_methodName, cacheKey._methodName, StringComparison.InvariantCulture))
                return false;

            if (_genericTypes.Length != cacheKey._genericTypes.Length)
                return false;

            if (_parameterTypes.Length != cacheKey._parameterTypes.Length)
                return false;

            for (int i = 0; i < _genericTypes.Length; ++i)
            {
                if (_genericTypes[i] != cacheKey._genericTypes[i])
                    return false;
            }

            for (int i = 0; i < _parameterTypes.Length; ++i)
            {
                if (_parameterTypes[i] != cacheKey._parameterTypes[i])
                    return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            return _hashCode;
        }

        private int GenerateHashCode()
        {
            unchecked
            {
                var result = _sourceType.GetHashCode();

                result = (result * 397) ^ _methodName.GetHashCode();

                for (int i = 0; i < _genericTypes.Length; ++i)
                {
                    result = (result * 397) ^ _genericTypes[i].GetHashCode();
                }

                for (int i = 0; i < _parameterTypes.Length; ++i)
                {
                    result = (result * 397) ^ _parameterTypes[i].GetHashCode();
                }

                return result;
            }
        }
    }
}

Which is called as follows:

private object GetIEnumerableRequest(Type type)
{
    var genericResolveAllMethod = this.GetType().GetGenericMethod(BindingFlags.Public | BindingFlags.Instance, "ResolveAll", type.GetGenericArguments(), new[] { typeof(bool) });

    return genericResolveAllMethod.Invoke(this, new object[] { false });
}

And ResolveAll is defined as:

public IEnumerable<ResolveType> ResolveAll<ResolveType>()
    where ResolveType : class
{
    return ResolveAll<ResolveType>(true);
}

Hope that makes sense :)

like image 24
Steven Robbins Avatar answered Sep 29 '22 08:09

Steven Robbins


  1. Use it untyped as IEnumerable<IEnumerable>
  2. Use reflection to call a function that takes a IEnumerable<IEnumerable<T>> with the appropriate T
  3. use a switch statement to cast to appropriate type
  4. use dynamic


Examples
static IEnumerable<IEnumerable<T>> castList<T>(List<List<object>> list) {
    return list.Select(x => x.Cast<T>());
}
void DoSomething(Type myT, List<List<object>> list) {
    object untyped = typeof(MyClass).GetMethod("castList")
        .MakeGenericMethod(myT)
        .Invoke(null, new[] { list });
    // untyped is an IEnumerable<IEnumerable<myT>> at runtime, 
    // but obviously you don't know that at compile time.

    // what can you do with untyped? 
    // 1: use it like an untyped container
    var option1 = (IEnumerable<IEnumerable>)untyped;
    foreach(var inner in option1)
        foreach(object item in inner)
            Console.WriteLine(object);
    // 2: pass it to a function that you reflect on using
    //    the above makeGenericMethod strategy
    typeof(MyClass).GetMethod("Process")
        .MakeGenericMethod(myT)
        .Invoke(null, new[] { untyped });
    // 3: Cast it conditionally
    switch(Type.GetTypeCode(myT)) {
        case TypeCode.Int32:
             Process((IEnumerable<IEnumerable<int>>)untyped);
             break;
        case TypeCode.Single:
             Process((IEnumerable<IEnumerable<float>>)untyped);
             break;
    }
    // 4: make it a dynamic
    dynamic dyn = untyped;
    Process(dyn);
}
static void Process<T>(IEnumerable<IEnumerable<T>> ienumerable) {
    Console.WriteLine("Processing type: {0}", typeof(T).Name);
    foreach(var inner in ienumerable)
        foreach(T item in inner)
            DoSomething(item); // item is now type T
}
like image 36
Jimmy Avatar answered Oct 01 '22 08:10

Jimmy


Edit: If you only know T at run time, you could do it by building an expression. and compiling it. like so:

var listOfLists = new List<List<object>>();

//... do list building...

//types
var runTimeType = typeof(MyRuntimeType);
var innerListType = typeof(List<>)
    .MakeGenericType(typeof(object));
var innerEnumerableType = typeof(IEnumerable<>)
    .MakeGenericType(runTimeType);
var outerListType = typeof(List<>)
    .MakeGenericType(innerListType);

//methods
var castm = typeof(Enumerable).GetMethod("Cast")
    .MakeGenericMethod(runTimeType);
var selectm = typeof(Enumerable).GetMethods()
    .Where(x => x.Name == "Select").First()
    .MakeGenericMethod(innerListType, innerEnumerableType);

//expressions (parameters)
var innerParamx = Expression.Parameter(innerListType);
var outerParamx = Expression.Parameter(outerListType);

// listOfLists.Select(x => x.Cast<T>()); 
// as an expression
var castx = Expression.Call(castm, innerParamx);
var lambdax = Expression.Lambda(castx, innerParamx);
var selectx = Expression.Call(selectm, outerParamx, lambdax);
var lambdax2 = Expression.Lambda(selectx, outerParamx);

var result = lambdax2.Compile().DynamicInvoke(listOfLists);

you could optionally cached lambdax2.Compile() somewhere for each runtime type, performance.

like image 26
Master Morality Avatar answered Sep 29 '22 08:09

Master Morality