示例#1
0
        public static void TestMain()
        {
            List <MyBase> myList = new List <MyBase>();

            HashSet <Type> existingTypes = new HashSet <Type>();

            Action <MyBase> addNoDupType = (b) =>
            {
                if (!existingTypes.Contains(b.GetType()))
                {
                    myList.Add(b);
                    existingTypes.Add(b.GetType());
                }
            };

            MyBase derive1Inst1 = new MyDerive1();
            MyBase derive1Inst2 = new MyDerive1();
            MyBase derive2Inst  = new MyDerive2();

            addNoDupType(derive1Inst1);
            addNoDupType(derive1Inst2);
            addNoDupType(derive2Inst);

            foreach (var myBase in myList)
            {
                myBase.PrintId();
            }
        }
        public void TestMain()
        {
            MyDerive1 myDerive1 = new MyDerive1()
            {
                Id   = 1,
                Name = "MyDerive1"
            };

            Console.WriteLine("See if it works: " + myDerive1.Name + myDerive1.Id);

            Console.ReadLine();
        }