Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Generic list question
Message
De
20/11/2014 02:20:18
 
 
À
20/11/2014 01:29:27
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows 7
Network:
SAMBA Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01611163
Message ID:
01611211
Vues:
40
>>>>Hi Gregory and thanks, I should have been clearer, I don't want an instance of the class I need its type , what I need to do is get a List(T) from a Generic(T) class , the generic class expects a class of type IMyInterFace to create and populate a List(T) I show a code snippet below
>>>>
>>>>
>>>>I create it like this at the moment
>>>>
>>>>if(some condition)
>>>>    XLToList(MyClass) xlist = new XLToList(MyClass)();
>>>>else
>>>>    XLToList(MyOtherClass) xlist2 = new XLToList(MyOtherClass)();
>>>>
>>>>
>>>>public class XlToList(T) where T : ITariffDataImport, new()
>>>>    {
>>>>        public delegate void Notify(object sender, CSVEventargs e);
>>>>        public delegate void NotifyEveryRecord(int RecordNumber);
>>>>        public event Notify OnFinishedProcessing;
>>>>        public event Notify OnFoundBadTariff;
>>>>        public event Notify OnFoundStartingRow;
>>>>        public event NotifyEveryRecord OnProcessingRecords;
>>>>
>>>>
>>>>        private Type thistype = typeof(T);
>>>>
>>>>        private List(T) data;
>>>>
>>>>        public List(T) Data
>>>>        {
>>>>            get
>>>>            {
>>>>                return data;
>>>>            }
>>>>            set
>>>>            {
>>>>                data = value;
>>>>            }
>>>>        }
>>>>
>>>>
>>>>and the class uses T to create types and class instances etc... any clearer ? ( sorry I've used parentheses instead of angle brackets because of the UT limitations.)
>>>>
>>>>Thanks again Greg
>>>
>>>I've got this, Pete
>>>
>>>The Data member of XlToList is a list of the interface
>>>
>>>You have to cast - see constructor of XlToList
>>>
>>>
>>>	static class Test_GenericInterface
>>>	{
>>>		internal static void Go()
>>>		{
>>>			List<ITest> xlist;
>>>		
>>>			if (true)
>>>				 xlist = new XlToList<ClassA>().Data;
>>>			else
>>>				xlist = new XlToList<ClassB>().Data;
>>>			
>>>		}
>>>		
>>>	}
>>>	public class XlToList<T> where T : ITest, new()
>>>	{
>>>		public delegate void NotifyEveryRecord(int RecordNumber);
>>>		public event NotifyEveryRecord OnProcessingRecords;
>>>
>>>
>>>		private Type thistype = typeof(T);
>>>
>>>		private List<ITest> data;
>>>
>>>		public List<ITest> Data
>>>		{
>>>			get
>>>			{
>>>				return data;
>>>			}
>>>			set
>>>			{
>>>				data = value;
>>>			}
>>>		}
>>>		public XlToList()
>>>		{
>>>			data = new List<ITest>();
>>>			data.Add( (ITest)new T());
>>>		}
>>>	}
>>>	public interface ITest
>>>	{
>>>		int Get();
>>>	}
>>>	public class ClassA : ITest
>>>	{
>>>		
>>>		public int Get()
>>>		{
>>>			return 1;
>>>		}
>>>
>>>	}
>>>	public class ClassB : ITest
>>>	{
>>>	
>>>		public int Get()
>>>		{
>>>			return 3;
>>>		}
>>>	}
>>>
But you're can't assign a List of ClassA to xlist.Data can you ?
>
>No, you can't - but you knew didn't you

I was pretty sure but hadn't tried it :-}
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform