c# – 为什么我得到“无法转换类型的对象”异常?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 为什么我得到“无法转换类型的对象”异常?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3023字,纯文字阅读大概需要5分钟。
内容图文
![c# – 为什么我得到“无法转换类型的对象”异常?](/upload/InfoBanner/zyjiaocheng/768/ba0a4c6604db42fe941def61f7ac39f5.jpg)
我有以下代码
public class SortTerm<T>
{
public System.Func<T, System.IComparable> Sort;
public SortDirection Direction;
public SortTerm(System.Func<T, System.IComparable> sorter, SortDirection direction)
{
this.Sort = sorter;
this.Direction = direction;
}
public SortTerm(System.Func<T, System.IComparable> sorter)
: this(sorter, SortDirection.Ascending)
{ }
public static SortTerm<T> Create<TKey>(System.Func<T, TKey> sorter, SortDirection direction)
where TKey : System.IComparable
{
return new SortTerm<T>((System.Func<T, System.IComparable>)(object)sorter, direction);
} // End Constructor
public static SortTerm<T> Create<TKey>(System.Func<T, TKey> sorter)
where TKey : System.IComparable
{
return Create<TKey>(sorter, SortDirection.Ascending);
} // End Constructor
}
哪个需要强制转换System.Func< T,TKey>到System.Func< T,IComparable>
为什么
SortTerm<Db.T_projects>.Create(x => x.name);
工作,而
SortTerm<Db.T_projects>.Create(x => x.id);
给出无效的演员表
InvalidCastException: Unable to cast object of type ‘System.Func2[Db.T_projects,System.Int64]’ to type
‘System.Func2[Db.T_projects,System.IComparable]’.
当long / Int64定义为
public struct Int64 : IComparable, IComparable<Int64>, IConvertible, IEquatable<Int64>, IFormattable
虽然字符串的定义与IComparable无差别……
public sealed class String : IEnumerable<char>, IEnumerable, IComparable, IComparable<String>, IConvertible, IEquatable<String>, ICloneable
为了完整
public partial class T_projects
{
public long id; // int not null
public string name; // nvarchar(4000) not null
}
这不应该工作吗?
更重要的是,如何使这项工作?
注意:
将会有一个List< SortTerm< T>>,所以我不能在排序定义中使用TKey.
解决方法:
正如RenéVogt在评论中告诉你的那样,这是因为值类型(如Int64)不支持共变转换,而它适用于引用类型(如字符串).
通过将sorter放入具有匹配签名的委托中来修复它.
一个类型转发代表,所以说:
public class SortTerm<T>
{
public System.Func<T, System.IComparable> Sort;
public SortDirection Direction;
public SortTerm(System.Func<T, System.IComparable> sorter, SortDirection direction)
{
this.Sort = sorter;
this.Direction = direction;
}
public SortTerm(System.Func<T, System.IComparable> sorter)
: this(sorter, SortDirection.Ascending)
{ }
public static SortTerm<T> Create<TKey>(System.Func<T, TKey> sorter, SortDirection direction)
where TKey : System.IComparable
{
// return new SortTerm<T>((System.Func<T, System.IComparable>)(object)sorter, direction);
return new SortTerm<T>(delegate (T x) {
TKey ret = sorter(x);
return (System.IComparable)ret;
}, direction);
} // End Constructor
public static SortTerm<T> Create<TKey>(System.Func<T, TKey> sorter)
where TKey : System.IComparable
{
return Create<TKey>(sorter, SortDirection.Ascending);
} // End Constructor
}
您可能希望放弃TKey:System.IComparable限制,因为这对于可空类型不起作用,例如:
SortTerm<Db.T_projects>.Create(x => x.created);
对于
public partial class T_projects
{
public long id; // int not null
public string name; // nvarchar(4000) not null
public System.DateTime? created; // datetime null
}
作为AFAIK,没有办法
where TKey : System.IComparable or System.Nullable<System.IComparable>
内容总结
以上是互联网集市为您收集整理的c# – 为什么我得到“无法转换类型的对象”异常?全部内容,希望文章能够帮你解决c# – 为什么我得到“无法转换类型的对象”异常?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。