本文介绍了在C#中将类转换为泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class BackupVirtualDataSource : AbstractVirtualListDataSource
    {
 List<BackUpInfo> Objects;
        public int ItemCount = 0;
        //BackUpInfo
        public BackupVirtualDataSource(VirtualObjectListView listView, List<BackUpInfo> ObjList)
            : base(listView)
        {
            this.Objects = ObjList;
            ItemCount = (ObjList == null) ? 0 : ObjList.Count;
        }

        public override int GetObjectIndex(object model)
        {
            return this.Objects.IndexOf((BackUpInfo)model);
        }

        public override object GetNthObject(int n)
        {
            BackUpInfo p = null;
            try
            {
                p = this.Objects[n % this.Objects.Count];
                return p;
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("| BackupVirtualDataSource.cs::GetNthObject() |", ex);
            }
            finally 
            {
                p = null;
            }
            return p;
            
        }

        public override int GetObjectCount()
        {
            return ItemCount;
        }

       
    }



i希望将以下类转换为泛型,但是我收到错误

无法找到类型或名称空间名称T



我尝试过:




i want to convert the following class to generics but iam getting error
The type or namespace name T could not be found

What I have tried:

class BackupVirtualDataSource : AbstractVirtualListDataSource
    {
 List<T> Objects;
        public int ItemCount = 0;
        //BackUpInfo
        public BackupVirtualDataSource(VirtualObjectListView listView, List<T> ObjList)
            : base(listView)
        {
            this.Objects = ObjList;
            ItemCount = (ObjList == null) ? 0 : ObjList.Count;
        }

        public override int GetObjectIndex(object model)
        {
            return this.Objects.IndexOf((T)model);
        }

        public override object GetNthObject(int n)
        {
            T p = null;
            try
            {
                p = this.Objects[n % this.Objects.Count];
                return p;
            }
            catch (Exception ex)
            {
                cGlobalSettings.oLogger.WriteLogException("| BackupVirtualDataSource.cs::GetNthObject() |", ex);
            }
            finally 
            {
                p = null;
            }
            return p;
            
        }

        public override int GetObjectCount()
        {
            return ItemCount;
        }

       
    }

推荐答案

class BackupVirtualDataSource<T> : AbstractVirtualListDataSource


这篇关于在C#中将类转换为泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:51