Könnte mir bitte jemand erklären, warum die folgende Besetzung nicht funktioniert und die Lösung für das Problem.InvalidCastException: Unable cast generische Liste zu Objekt
Ich habe eine GroupedResult
:
public class GroupedResult<TKey, TElement>
{
public TKey Key { get; set; }
private readonly IEnumerable<TElement> source;
public GroupedResult(TKey key, IEnumerable<TElement> source)
{
this.source = source;
this.Key = key;
}
}
public class Bacon
{
}
ich die List<string, Bacon>
zu List<string, object>
werfen möchte. Ich habe die folgenden und andere Möglichkeiten ausprobiert.
var list = new List<GroupedResult<string, Bacon>>
{
new GroupedResult<string, Bacon>("1", new List<Bacon>()),
new GroupedResult<string, Bacon>("2", new List<Bacon>())
};
var result = list.Cast<GroupedResult<string, object>>().ToList();
Aber ich bekomme immer folgende Fehlermeldung:
InvalidCastException: Unable to cast object of type 'GroupedResult
2[System.String,UserQuery+Bacon]' to type 'GroupedResult
2[System.String,System.Object]'.
Klassen sind unveränderlich so eine 'GropuedResult' ist kein 'GroupedResult '. –
Lee