ジェネリック ValueCollection を ICollection として返そうとしています。彼の MSDN ドキュメントから、Dictionary.ValueCollection は ICollection インターフェイスを実装していると書かれています。ただし、何らかの理由で、ValueCollection を ICollection としてキャストする必要があるときにエラーが発生します。これはコード サンプルで、その下に表示されるエラーが表示されます。
public ICollection<T> GetAllComponents<T>() where T : Component
{
Dictionary<Entity, Component>.ValueCollection retval = null;
if(!this.componentEntityDatabase.ContainsKey(typeof(T)))
{
Logger.w (Logger.GetSimpleTagForCurrentMethod (this), "Could not find Component " + typeof(T).Name + " in database");
return new List<T>();
}
Dictionary<Entity, Component> entityRegistry = this.componentEntityDatabase [typeof(T)];
retval = entityRegistry.Values;
return (ICollection<T>)retval;
}
エラー:
Cannot convert type 'Systems.Collections.Generic.Dictionary<Entity,Component>.ValueCollection' to System.Collections.Generic.ICollection<T>
私はこれを間違っていますか?または、ディクショナリから値をコピーせずにこれを達成する別の方法はありますか?