読者です 読者をやめる 読者になる 読者になる

WMI のメタデータ (2)

.NET

id:siokoshou:20071127:p2 + id:NyaRuRu:20071124:p2
コメント欄の nsharp 氏のコードもちょっと混ぜてみたり.
そして何回か試行錯誤していたら自分でも何を書いているのか分からなくなってきた.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;

namespace Wmi
{
    static class Program
    {
        static void Main()
        {
            var Win32_CacheMemory = "Win32_CacheMemory";

            var mgmtScope = new ManagementScope(@"root\cimv2");
            var mgmtScopeNeutral = new ManagementScope();
            var mgmtPath = new ManagementPath(Win32_CacheMemory);
            using (var mgmtClass = new ManagementClass(mgmtScope, mgmtPath, null))
            {
                mgmtClass.Options.UseAmendedQualifiers = true;

                var query =
                    from mobj in mgmtClass.GetInstances().OfType<ManagementObject>()
                    from prop in mobj.Properties.OfType<PropertyData>()
                    let mapper = mgmtClass.GetPropertyValueMapper(prop)
                    let propValues = (prop.IsArray && prop.Value != null)
                                   ? ((Array)prop.Value).Cast<object>()
                                   : Enumerable.Repeat(prop.Value, 1)
                    let mappedValues = propValues.Select(mapper)
                    select new { prop.Name, Value = String.Join(",", mappedValues.ToArray()) };

                foreach (var item in query)
                {
                    Console.WriteLine(item.Name + " : " + item.Value);
                }
                Console.ReadKey();
            }
        }

        static Func<object, string> GetPropertyValueMapper(this ManagementClass mgmtClass, PropertyData prop)
        {
            // TODO: BitMap/BitValues mapping
            // http://msdn2.microsoft.com/en-us/library/aa393965.aspx

            mgmtClass.Options.UseAmendedQualifiers = true;

            var classProp = mgmtClass.Properties
                                .OfType<PropertyData>()
                                .FirstOrDefault(p => p.Name == prop.Name);

            if (classProp == null)
                return src => src != null ? src.ToString() : "null";

            var rawValuesQualifier = classProp.Qualifiers
                                        .OfType<QualifierData>()
                                        .FirstOrDefault(q => q.Name == "Values");

            if (rawValuesQualifier == null)
                return src => src != null ? src.ToString() : "null";

            var values = rawValuesQualifier.Value as string[] ?? new string[0];

            var rawValueMap = classProp.Qualifiers
                               .OfType<QualifierData>()
                               .FirstOrDefault(q => q.Name == "ValueMap");

            var valueMap = (rawValueMap == null)
                ? Enumerable.Range(0, values.Length)
                    .Select(i => i.ToString())
                    .ToArray()
                : rawValueMap.Value as string[] ?? new string[0];

            Dictionary<string, string> map
                = valueMap
                .Select((mapFrom, index) => new { MapFrom = mapFrom, MapTo = values[index] })
                .ToDictionary(item => item.MapFrom, item => item.MapTo);

            return key =>
                {
                    if (key == null) return "null";
                    string ret;
                    if (map.TryGetValue(key.ToString(), out ret))
                    {
                        return ret;
                    }
                    return key.ToString();
                };
        }
    }
}