1

Faced interesting problem. C# runtime throws "Not Supported" exception when I attempt to call a generic method constructed with TypeDelegator argument. Here is the example:

using System;
using System.Reflection;
using System.Linq.Expressions;

namespace ConsoleApplication2
{
    public class Arg { }
    public class MyTypeDelegator : TypeDelegator
    {
        protected override bool HasElementTypeImpl() { return false; }
    }
    public static class Extension
    {
        public static int Method<T>(this Arg arg) { return 0; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var methodInfo = typeof(Extension).GetMethod("Method").MakeGenericMethod(new MyTypeDelegator());
            // System.NotSupportedException here:
            methodInfo.Invoke(new Arg(), new Object[] { });
            // and here:
            var call = Expression.Call(methodInfo, new Expression[] {Expression.Constant(new Arg(), typeof(Arg)) });
        }
    }
}

What would be a workaround or solution for this?

PS: I cannot drop MyTypeDelegator, because it serves as a type alias for dynamic soft classes, coming from 3rd party lib. Signature and implementation of real generic Method depend on the specific data stored in the MyTypeDelegator.

Andrew
  • 2,055
  • 2
  • 20
  • 27

0 Answers0