使用LINQ的.Cast()运算符时,显式/隐式强制转换运算符失败 [英] Explicit/implicit cast operator fails when using LINQ's .Cast() operator
问题描述
我正在尝试使用一个具有显式(但也会因隐式而失败)强制转换运算符的类,该运算符在使用LINQ的Cast<T>()
函数时会失败.这是两个类的定义
I am trying to use a class that has a explicit (but also fails with implicit) cast operator that fails when using LINQ's Cast<T>()
function. Here is the definitions of the two classes
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; }
}
这是我用来测试的代码.
Here is the code I am using to test it.
//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>());
这是我得到的例外情况
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:
为了让它按我想要的方式工作,我在演员表中做错什么了?
What am I doing wrong in my casting to get this to work the way I want?
推荐答案
定义显式/隐式强制转换运算符时,它们将在编译时绑定在调用位置.这就是第一行起作用的原因:编译器可以计算出所需的所有类型信息,因此可以将您的自定义显式强制转换运算符替换为默认的.
When you define explicit/implicit cast operators, they are bound at call-sites at compile-time. That's why the first line works: the compiler can work out all the type information needed, and so it can substitute your custom explicit cast operator for the default one.
但是,由于Cast<T>
仅执行通用类型转换,因此编译器不了解您的运算符,因此将其忽略.结果:无效的强制转换异常.
However, since the Cast<T>
just performs a generic cast, the compiler doesn't know about your operator, and thus it is ignored. Result: invalid cast exception.
您可以通过执行.Select(x => (DatabaseInfo)x)
来解决此问题.另外,您可以添加一个名为ToDatabaseInfo()
的方法,这样就不会隐藏实际发生的情况.
You can get around this by instead performing a .Select(x => (DatabaseInfo)x)
. Alternatively, you could add on a method called ToDatabaseInfo()
, so that you're not hiding what's actually going on.
这篇关于使用LINQ的.Cast()运算符时,显式/隐式强制转换运算符失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!