gpt4 book ai didi

c# - 为什么会出现此 "Unable to cast object of type"异常?

转载 作者:行者123 更新时间:2023-11-30 12:55:29 25 4
gpt4 key购买 nike

我有以下代码

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)不支持协变转换,而它适用于引用类型(如字符串)。

通过将排序器放入具有匹配签名的委托(delegate)中来修复它。
类型转换委托(delegate),可以这么说:

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

}

您可能希望放弃 where 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
}

据我所知,没有办法

where TKey : System.IComparable or System.Nullable<System.IComparable>

关于c# - 为什么会出现此 "Unable to cast object of type"异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48881671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com