C# 如何返回通用列表<;T>;从通用方法<;T>;()

C# 如何返回通用列表<;T>;从通用方法<;T>;(),c#,generics,C#,Generics,早上好 我有一个类产品{},它有一个字典: Dictionary<Type, List<IPropertyItem>> extensions; 字典扩展; 我使用以下方法保存数据: public void SaveItem<T>(T item) { Type currentType = typeof(T); if (!extensions.ContainsKey(currentType)) {

早上好

我有一个类产品{},它有一个字典:

Dictionary<Type, List<IPropertyItem>> extensions;
字典扩展;
我使用以下方法保存数据:

public void SaveItem<T>(T item)
    {
        Type currentType = typeof(T);

        if (!extensions.ContainsKey(currentType))
        {
            extensions.Add(currentType, new List<IPropertyItem>());
        }

        if (currentType == typeof(Discount))
        {
            Discount newDiscount = (Discount)Convert.ChangeType(item, typeof(Discount));                
            extensions[currentType].Add(newDiscount);
        }
        else if(currentType == typeof(Tax))
        {
            Tax newTax = (Tax)Convert.ChangeType(item, typeof(Tax));
            extensions[currentType].Add(newTax);
        }
        else if(currentType == typeof(Size))
        {
            Size newSize = (Size)Convert.ChangeType(item, typeof(Size));
            extensions[currentType].Add(newSize);
        }           
    }
公共作废保存项(T项)
{
类型currentType=类型(T);
如果(!extensions.ContainsKey(currentType))
{
Add(currentType,new List());
}
if(currentType==typeof(折扣))
{
折扣新折扣=(折扣)转换.ChangeType(项目,类型(折扣));
扩展[currentType]。添加(新折扣);
}
else if(currentType==类型(税))
{
Tax newTax=(Tax)Convert.ChangeType(项目,类型(Tax));
扩展[currentType]。添加(newTax);
}
else if(currentType==typeof(大小))
{
Size newSize=(Size)Convert.ChangeType(item,typeof(Size));
扩展[currentType]。添加(新闻大小);
}           
}
现在,我想得到一个存储在字典中的特定值类型的列表,我的意思是,我希望该方法返回一个类似以下函数的列表:

public List<T> GetExtensionsDictionary<T>()
    {
        Type currentType = typeof(T);
        List<T> returnedList = new List<T>();

        if (!extensions.ContainsKey(currentType))
        {
            return null;
        }

        return extensions[T];            
    }
公共列表GetExtensionsDictionary() { 类型currentType=类型(T); List returnedList=新列表(); 如果(!extensions.ContainsKey(currentType)) { 返回null; } 返回扩展[T]; } 上面调用的方法是:

List<Discount> myDiscounts = myProduct.GetExtensionsDictionary<Discount>();
List mydiscounters=myProduct.GetExtensionsDictionary();
thnks


任何帮助都将不胜感激……

我相信这就是您想要的:

public List<T> GetExtensionsDictionary<T>()
{
    Type currentType = typeof(T);
    List<T> returnedList = new List<T>();

    if (!extensions.ContainsKey(currentType))
    {
        return null;
    }

    return extensions[currentType].Cast<T>().ToList();            
}
公共列表GetExtensionsDictionary() { 类型currentType=类型(T); List returnedList=新列表(); 如果(!extensions.ContainsKey(currentType)) { 返回null; } 返回扩展[currentType].Cast().ToList(); } 您必须使用
currentType
而不是
T
索引到
extensions
,因为
T
是一个通用参数,您需要使用
typeof
获得的运行时类型

您可以使用linq方法
Cast
ToList
将字典中
currentType
处的集合转换为
列表
。这是列表的副本,因此请注意性能方面的考虑


确保您正在
使用System.Linq

我相信这就是你想要的:

public List<T> GetExtensionsDictionary<T>()
{
    Type currentType = typeof(T);
    List<T> returnedList = new List<T>();

    if (!extensions.ContainsKey(currentType))
    {
        return null;
    }

    return extensions[currentType].Cast<T>().ToList();            
}
公共列表GetExtensionsDictionary() { 类型currentType=类型(T); List returnedList=新列表(); 如果(!extensions.ContainsKey(currentType)) { 返回null; } 返回扩展[currentType].Cast().ToList(); }
您必须使用
currentType
而不是
T
索引到
extensions
,因为
T
是一个通用参数,您需要使用
typeof
获得的运行时类型

您可以使用linq方法
Cast
ToList
将字典中
currentType
处的集合转换为
列表
。这是列表的副本,因此请注意性能方面的考虑


确保您正在
使用System.Linq

那么问题出在哪里?你想要GetExtensionsDictionary的扩展方法吗?我认为问题在于他的
GetExtensionsDictionary
方法无法编译,他不知道如何将字典中的集合转换为
列表
,这就是我在回答中提到的,无论如何,你应该引入类型参数约束,以避免不必要的
Convert.ChangeType
转换,例如
public void SaveItem(T项)其中T:IPropertyItem
。那么问题出在哪里?您想要GetExtensionsDictionary的扩展方法吗?我认为问题在于他的
GetExtensionsDictionary
方法无法编译,并且他不知道如何将字典中的集合转换为
列表
,这就是我在回答中提到的。无论如何,你应该引入类型参数约束,以避免不必要的
Convert.ChangeType
转换,例如
public void SaveItem(T item),其中T:IPropertyItem
。这就是答案,事实上我以前尝试过这个返回,但我没有添加库Linq!!!这才是真正的问题。。。又多了thanks@writer没问题,请把它标记为正确的,伙计,我试着得到那些要点,这就是答案,事实上我以前试过返回,但我没有添加库Linq!!!这才是真正的问题。。。又多了thanks@writer没问题,请把它标记为正确的,伙计,我在试着拿那些分数