DictionaryBase и методы подклассных объектов - PullRequest
0 голосов
/ 01 января 2011

Я использую DictionaryBase для сбора объектов.Но я не могу получить методы подкласса (HardDrive.cs) только суперкласс, абстрактные методы DeviceInfo, такие как .Named ();

Класс HardDrive: DeviceInfo

deviceCollection = DictionaryBase

Я пытаюськ использованию методов HardDrive, таких как Freespace, Size и т. д.

Например, он работал:

deviceCollection["Western Digital"].Named(); // Superclass method: Named

Не работает:

deviceCollection["Western Digital"].Size(); // Subclass method: Size

Я не знаю, чтоВозможно ли это преобразовать в подкласс или около того?

Заранее благодарим.

Имя файла: DeviceInfo.cs

using System;
using System.Management;
using System.Windows.Forms;

namespace TestHarnessHardDrives
{
  public abstract class DeviceInfo
  {
    protected string name;

    public string Name
    {
      get
      {
        return name;
      }
      set
      {
        name = value;
      }
    }

    public DeviceInfo()
    {
      name = "The device with no name";
    }

    public DeviceInfo(string newName)
    {
      name = newName;
    }

    public void Named()
    {
      Console.WriteLine("{0} has been named.", name);
    }
  }
}

Имя файла: HardDrive.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestHarnessHardDrives
{
  class HardDrive: DeviceInfo
  {
    // properties
    private ulong _size;
    private ulong _freespace;
    private string _volumeSerialNumber;
    private string _filesystem;

    //methods

    // VolumeName + Caption
    public HardDrive(string newName, ulong newSize): base(newName)
    {
      _size = newSize;
    }

    // Freespace
    public ulong Freespace
    {
      get
      {
        return _freespace;
      }
      set
      {
        _freespace = value;
      }
    }

    // Size
    public ulong Size
    {
      get
      {
        return _size;
      }
      set
      {
        _size = value;
      }
    }

    // VolumeSerialNumber
    public string VolumeSerialNumber
    {
      get
      {
        return _volumeSerialNumber;
      }
      set
      {
        _volumeSerialNumber = value;
      }
    }

    // Filesystem
    public string Filesystem
    {
      get
      {
        return _filesystem;
      }
      set
      {
        _filesystem = value;
      }
    }
  }
}

имя файла: Devices.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestHarnessHardDrives
{
    class Devices: DictionaryBase
    {
        public void Add(string newID, DeviceInfo newDevice)
        {
            Dictionary.Add(newID, newDevice);
        }

        public void Remove(string newID, DeviceInfo oldDevice)
        {
            Dictionary.Remove(oldDevice);
        }

        public Devices()
        {
        }

        public DeviceInfo this [string deviceID]
        {
            get
            {
                return (DeviceInfo)Dictionary[deviceID];
            }
            set
            {
                Dictionary[deviceID] = value;
            }

        }

        public new IEnumerator GetEnumerator()
        {
            foreach (object device in Dictionary.Values)
                yield return (DeviceInfo)device;
        }
    }
}

.... основная программа

Filename: Program.cs
using System;
using System.Management;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestHarnessHardDrives
{
    class Program
    {
        public static Devices deviceCollection = new Devices();

        static void getHardDrives()
        {
            string keyHDDName;

            try
            {
                ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_LogicalDisk");

                deviceCollection.Add("Western Digital", new HardDrive("Western Digital",100000));

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Description"].Equals("Local Fixed Disk"))
                    {
                        Console.WriteLine("Description: {0}", queryObj["Description"]);

                        keyHDDName = queryObj["VolumeName"].ToString() + " "
                                    + queryObj["Caption"].ToString();

                        deviceCollection.Add(keyHDDName, new HardDrive(keyHDDName, 100000));

                    };

                    //     Console.WriteLine("FileSystem: {0}", queryObj["FileSystem"]);
                    //     Console.WriteLine("FreeSpace: {0}", queryObj["FreeSpace"]);
                    //     Console.WriteLine("Size: {0}", queryObj["Size"]);
                    //     Console.WriteLine("VolumeSerialNumber: {0}", queryObj["VolumeSerialNumber"]);
                }

            }

        //      deviceCollection.Add(new SystemInterface("Western Digital"));

            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }


        static void Main(string[] args)
        {

            Console.WriteLine("Create an Array type Collection of DeviceInfo " +
                                    "objects and use it");

            getHardDrives();

            foreach (DeviceInfo myDevices in deviceCollection)
            {
                myDevices.Named();
                myDevices.GetType();
            }

            deviceCollection["Western Digital"].Named();

            Console.ReadKey();
        }
    }
}

Ответы [ 2 ]

1 голос
/ 01 января 2011

Если у вас нет кода, который входит в класс Devices, он вам действительно не нужен - вы можете просто использовать Dictionary<DeviceInfo>.

Кажется, что в методе getHardDrives() требуется извлекать только жесткие диски из словаря устройств.

Учитывая IDictionary<DeviceInfo>, вы можете использовать эту строку для получения жестких дисков:

var hardDrives = devices.Values.OfType<HardDrive>();

Это даст вам IEnumerable<HardDrive>.

Если вы хотите получить ключ и значение из словаря:

var hardDrivesAndIds = devices.Where(x => x.Value is HardDrive);

Это даст вам IEnumerable<KeyValuePair<string, DeviceInfo>>.

Другая альтернатива:

var hardDriveDictionary = devices.Where(x => x.Value is HardDrive).ToDictionary(x => x.Key, y => y.Value);

Это даст вам Dictionary<string, DeviceInfo>.

0 голосов
/ 01 января 2011

Это то, что дженерики для.Наследовать от Dictionary вместо DictionaryBase.Затем все, что вы добавляете и удаляете из коллекции, будет напечатано как HardDrive.ЛУЧШЕ, НЕТ, вам даже не нужно наследовать ... просто создать словарь.Вот так ...

var devices = new Dictionary<string, HardDrive>();

devices.Add("Western Digital", new HardDrive("WesternDigital"));
devices.Add("Seagate", new HardDrive("Seagate"));

Итак, ваша основная программа будет выглядеть так:

    namespace TestHarnessHardDrives
{
    class Program
    {
        public static Dictionary<string, HardDrive> deviceCollection = new Dictionary<string, HardDrive>

        static void getHardDrives()
        {
            string keyHDDName;

            try
            {
                ManagementObjectSearcher searcher =
                new ManagementObjectSearcher("root\\CIMV2",
                "SELECT * FROM Win32_LogicalDisk");

                deviceCollection.Add("Western Digital", new HardDrive("Western Digital",100000));

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Description"].Equals("Local Fixed Disk"))
                    {
                        Console.WriteLine("Description: {0}", queryObj["Description"]);

                        keyHDDName = queryObj["VolumeName"].ToString() + " "
                                    + queryObj["Caption"].ToString();

                        deviceCollection.Add(keyHDDName, new HardDrive(keyHDDName, 100000));

                    };

                    //     Console.WriteLine("FileSystem: {0}", queryObj["FileSystem"]);
                    //     Console.WriteLine("FreeSpace: {0}", queryObj["FreeSpace"]);
                    //     Console.WriteLine("Size: {0}", queryObj["Size"]);
                    //     Console.WriteLine("VolumeSerialNumber: {0}", queryObj["VolumeSerialNumber"]);
                }

            }

        //      deviceCollection.Add(new SystemInterface("Western Digital"));

            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }


        static void Main(string[] args)
        {

            Console.WriteLine("Create an Array type Collection of DeviceInfo " +
                                    "objects and use it");

            getHardDrives();

            foreach (DeviceInfo myDevices in deviceCollection)
            {
                myDevices.Named();
                myDevices.GetType();
            }

            deviceCollection["Western Digital"].Named();

            Console.ReadKey();
        }
    }
}

ПРИМЕЧАНИЕ. Все, что я написал до сих пор, применимо, только если вы хотитеКоллекция с ничего, кроме жестких дисков в нем.Если у вас есть какое-то другое устройство, которое также наследует от вашего базового класса Device, но имеет свои собственные методы, то одних обобщений недостаточно для решения вашей проблемы.В этот момент вы можете хранить все как набор типов устройств и использовать проверку типов для приведения его к полному типу, иначе вам придется размещать разные типы устройств в разных коллекциях.

...