使用LINQ的.Cast()运算符时,显式/隐式转换运算符失败

Sco*_*ain 32 c# linq casting exception

我试图使用一个具有显式(但也失败的隐式)转换操作符的类,该操作符在使用LINQ Cast<T>()函数时失败.以下是这两个类的定义

public class DatabaseInfoElement : ConfigurationElement
{
    [ConfigurationProperty("AllowedServer", IsRequired = true)]
    public string AllowedServer { get { return (string)base["AllowedServer"]; } }

    [ConfigurationProperty("DatabaseName", IsRequired = true)]
    public string DatabaseName { get { return (string)base["DatabaseName"]; } }

    [ConfigurationProperty("SqlInstance", IsRequired = true)]
    public string SqlInstance { get { return (string)base["SqlInstance"]; } }

    public static explicit operator DatabaseInfo(DatabaseInfoElement element)
    {
        return new DatabaseInfo(element.AllowedServer, element.DatabaseName, element.SqlInstance);
    }

}

public class DatabaseInfo
{
    public DatabaseInfo(string allowedServer, string sqlInstance, string databaseName)
    {
        AllowedServer = allowedServer;
        SqlInstance = sqlInstance;
        DatabaseName = databaseName;
    }

    public string AllowedServer { get; set; }
    public string SqlInstance { get; set; }
    public string DatabaseName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我用来测试它的代码.

//Gets the ConfigurationSection that contains the collection "Databases"
var section = DatabaseInfoConfig.GetSection();

//This line works perfectly.
DatabaseInfo test = (DatabaseInfo)section.Databases[0];

//This line throws a execption
var test2 = new List<DatabaseInfo>(section.Databases.Cast<DatabaseInfo>());
Run Code Online (Sandbox Code Playgroud)

这是我得到的例外

System.InvalidCastException was unhandled by user code
  HResult=-2147467262
  Message=Unable to cast object of type 'Server.Config.DatabaseInfoElement' to type 'Server.DatabaseInfo'.
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.d__b1`1.MoveNext()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at Sandbox.Main() in E:\Code\Sandbox\Program.cs:line 82
  InnerException: 

在我的演员表中我做错了什么才能按照我想要的方式工作?

dle*_*lev 50

定义显式/隐式转换运算符时,它们在编译时绑定在调用点.这就是第一行工作的原因:编译器可以计算出所需的所有类型信息,因此它可以将您的自定义显式转换运算符替换为默认值.

但是,由于Cast<T>只执行通用转换,编译器不知道您的运算符,因此它被忽略.结果:无效的强制转换异常

你可以通过执行一个来解决这个问题.Select(x => (DatabaseInfo)x).或者,您可以添加一个名为的方法ToDatabaseInfo(),这样您就不会隐藏实际发生的事情.

  • 这个答案提供了更多关于为什么`Cast&lt;T&gt;` 使用隐式运算符失败的信息:/sf/answers/56614421/ (2认同)