找不到类型的构造函数 [英] Constructor on type not found

查看:123
本文介绍了找不到类型的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

异常消息:Constructor on type StateLog not found.

我有以下代码仅对一个类无效:

I have the following code that does not work for only one class:

        List<T> list = new List<T>();
        string line;
        string[] lines;

        HttpWebResponse resp = (HttpWebResponse)HttpWebRequest.Create(requestURL).GetResponse();

        using (var reader = new StreamReader(resp.GetResponseStream()))
        {
            while ((line = reader.ReadLine()) != null)
            {
                lines = line.Split(splitParams);
                list.Add((T)Activator.CreateInstance(typeof(T), lines));
            }
        }

它不适用的类的构造函数与它所适用的其他类完全相同.唯一的区别是该类将传递16个参数,而不是2-5个参数.构造函数如下所示:

The constructor for the class that it does not work for is exactly like the other classes for which it works. The only difference is that this class will be passed 16 arguments instead of 2-5. The constructor looks as such:

    public StateLog(string[] line)
    {
        try
        {
            SessionID = long.Parse(line[0]);
            AgentNumber = int.Parse(line[1]);
            StateIndex = int.Parse(line[5]);
            ....
        }
        catch (ArgumentNullException anex)
        {
            ....
        }
    }

就像我说的那样,它适用于使用此方法的其他5个类,唯一的区别是输入的数量.

Like I said, it works for the other 5 classes that use this, the only difference is the number of inputs.

推荐答案

那是因为您使用的是数组协方差而进行编译.

That's because you are using the Activator.CreateInstance overload which accepts an object array, which is supposed to contain a list of constructor parameters. In other words, it's trying to find a StateLog constructor overload which has 16 parameters, instead of one. This compiles due to array covariance.

因此,当编译器看到此表达式时:

So when the compiler sees this expression:

Activator.CreateInstance(typeof(T), lines)

由于linesstring[],因此它假定您要依靠协方差将其自动转换为object[],这意味着编译器实际上是这样的:

since lines is a string[], it presumes you want to rely on covariance to have it cast it to object[] automatically, meaning that the compiler actually sees it like this:

Activator.CreateInstance(typeof(T), (object[])lines)

然后,该方法将尝试找到一个构造函数,该构造函数的参数数量等于lines.Length,所有类型均为string.

The method will then try to find a constructor which has a number of parameters equal to lines.Length, all of type string.

例如,如果您具有以下构造函数:

For example, if you have these constructors:

class StateLog
{
      public StateLog(string[] line) { ... }
      public StateLog(string a, string b, string c) { ... }
}

调用Activator.CreateInstance(typeof(StateLog), new string[] { "a", "b", "c" })将调用第二个构造函数(具有三个参数的那个),而不是第一个构造函数.

Calling Activator.CreateInstance(typeof(StateLog), new string[] { "a", "b", "c" }) will call the second constructor (the one with three parameters), instead of the first one.

您真正想要的是有效地将整个lines数组作为第一个数组项传递:

What you actually want is to pass the entire lines array as the first array item, effectively:

var parameters = new object[1];
parameters[0] = lines;
Activator.CreateInstance(typeof(T), parameters)

当然,您可以简单地使用内联数组初始化器:

Of course, you can simply use an inline array initializer:

list.Add((T)Activator.CreateInstance(typeof(T), new object[] { lines }));

这篇关于找不到类型的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆