为什么我会遇到“无法转换类型”的异常?

4
我有以下代码。
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: 无法将类型为'System.Func2[Db.T_projects,System.Int64]'的对象转换为类型'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。


5
您尝试的是协变转换。这对值类型(如 Int64)不受支持。string 是引用类型,所以可以使用。 - René Vogt
3
为什么协变性和逆变性不支持值类型? - René Vogt
2个回答

0

正如René Vogt在评论中告诉你的那样,这是因为协变转换不支持值类型(如Int64),而对于引用类型(如string)则可以。

通过将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 

}

您可能想要放弃 where TKey : System.IComparable 的限制,因为这对可空类型不起作用,例如:

SortTerm<Db.T_projects>.Create(x => x.created);

for

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>

0

截至文档,逆变仅适用于引用类型 - 例如string

协变仅适用于引用类型;如果您为变体类型参数指定值类型,则该类型参数对于生成的构造类型是不变的。

因此,您可以执行以下操作:

Func<string> myFunc = ...
Func<object> func = myFunc;

但不包括这个:

Func<int> myFunc = ...
Func<object> func = myFunc;

为了避免这种情况,可以将你的id包装成object
Func<object> = () => (object) id;

这将是您代码中的以下内容:

SortTerm<Db.T_projects>.Create(x => (object) x.id);

或在现有委托上创建一个新的委托:

Func<object> = () => myFunc();

这在你的代码中对应着:

SortTerm<Db.T_projects>.Create(new Func<MyType, object>(x => x.id));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接