示例#1
0
        private void btnFeed_Click(object sender, EventArgs e)
        {
            IEggProducer animal = pgdTier.SelectedObject as IEggProducer;

            if (animal != null)
            {
                animal.Eat();
            }
        }
示例#2
0
        private void btnNew_Click(object sender, EventArgs e)
        {
            Type newTierType = cbxAnimalTypes.SelectedItem as Type;

            if (newTierType != null)
            {
                Assembly eggFarmAssembly = Assembly.Load("EierfarmBl");

                IEggProducer tier = eggFarmAssembly.CreateInstance(newTierType.FullName) as IEggProducer;
                if (tier != null)
                {
                    cbxTiere.Items.Add(tier);
                    cbxTiere.SelectedItem = tier;
                }
            }
        }
示例#3
0
        private void btnSpeichern_Click(object sender, EventArgs e)
        {
            // Benutzer nach Speicherort fragen
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                RestoreDirectory = true,
                Filter           = "EggProducer|*.epp|All|*.*",
                FilterIndex      = 0
            };

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                IEggProducer tier = pgdTier.SelectedObject as IEggProducer;

                if (tier != null)
                {
                    IDisposableImplementer o = null;
                    try
                    {
                        o = new IDisposableImplementer();
                        // Tier dort speichern
                        using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName))
                        {
                            //Type tpChicken = Type.GetType("EierfarmBl.Chicken");

                            // Jede konkrete Instanz kennt ihren konkreten Typ (aus GetType())
                            XmlSerializer serializer = new XmlSerializer(typeof(IEggProducer));
                            serializer.Serialize(writer, tier);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                    finally
                    {
                        // NICHT VERGESSEN, wenn kein using verwendet!
                        o?.Dispose();
                    }
                }
            }
        }