在构造函数参数

时间:2017-03-30 20:08:04

标签: generics reflection activator

我对以下代码有一个非常奇怪的问题:

using System;
using System.Linq;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var testTuples1 = GimeStringTuples("foo", "bar");
            var testTuples2 = GimeTuples("foo", "bar");
            Console.ReadKey();
        }

        public static object GimeStringTuples(params string[] values)
        {
            Type genericType = Type.GetType("System.Tuple`" + values.Length);
            Type[] typeArgs = values.Select(_ => typeof(string)).ToArray();
            Type specificType = genericType.MakeGenericType(typeArgs);
            return Activator.CreateInstance(specificType, values);
        }

        public static object GimeTuples<T>(params T[] values)
        {
            Type genericType = Type.GetType("System.Tuple`" + values.Length);
            Type[] typeArgs = values.Select(_ => typeof(T)).ToArray();
            Type specificType = genericType.MakeGenericType(typeArgs);

            dynamic result;
            string[] testArgs = { "foo", "bar" };
            result = Activator.CreateInstance(specificType, testArgs);
            result = Activator.CreateInstance(specificType, values);
            return result;
        }
    }
}

它在第二行到最后一行失败了:

result = Activator.CreateInstance(specificType, values);

这很奇怪,因为它基本上与之前执行的行相同:

result = Activator.CreateInstance(specificType, testArgs);

在这两种情况下,相同的参数作为specificType参数传递,而string[2]作为第二个参数传递。

GimeStringTuples方法工作得很好......虽然没有涉及泛型 - 这可能是提示。

任何人都能解释这种不寻常的行为吗?

2 个答案:

答案 0 :(得分:1)

我可以告诉你会发生什么。查看生成的程序集,您可以看到编译器引入了object[]包装values参数:

Activator.CreateInstance(specificType, new string[] { "foo", "bar" });
Activator.CreateInstance(specificType, new object[] { values });

现在无法找到正确的超载。 如果你添加一个演员表,你将得到预期的结果,代码再次起作用:

Activator.CreateInstance(specificType, values as string[])

但我无法告诉你为什么会发生这种情况,也许它可以从规格中挖掘出来。

答案 1 :(得分:0)

好的解决方案(感谢@thehennyy):

using System;
using System.Linq;

namespace ConsoleApp1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var testTuple = GimeTuples("foo", "bar");
            Console.WriteLine(testTuple);
            Console.ReadKey();
        }

        public static object GimeTuples<T>(params T[] values)
        {
            Type genericType = Type.GetType("System.Tuple`" + values.Length);
            Type[] typeArgs = values.Select(_ => typeof(T)).ToArray();
            Type specificType = genericType.MakeGenericType(typeArgs);
            object[] constructorArguments = values.Cast<object>().ToArray();
            return Activator.CreateInstance(specificType, constructorArguments);
        }
    }
}

这会强制编译器将通用数组元素作为参数传递,而不是将整个数组作为单个参数传递。

相关问题