public void AddCache(string rootName, string key, string value) { WmiObjectCacheEntry entry; if (Cache.Any(e => e.RootName == rootName)) { entry = Cache.First(e => e.RootName == rootName); } else { entry = new WmiObjectCacheEntry(rootName); Cache.Add(entry); } entry.Values.Add(key, value); }
public T Create <T>() { T instance = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] props = instance.GetType().GetProperties().Where(i => i.GetCustomAttribute <WmiObject>() != null).ToArray(); foreach (PropertyInfo prop in props) { WmiObject accessory = prop.GetCustomAttribute <WmiObject>(); // cache features if (accessory.Cache) { if (Cache.Any(e => e.RootName == accessory.RootName)) { WmiObjectCacheEntry entry = Cache.First(e => e.RootName == accessory.RootName); if (entry.Values.Any(v => v.Key == accessory.KeyName)) { prop.SetValue(instance, (string)entry.Values[accessory.KeyName]); continue; } } } // end cache features if (!Equals(accessory, null)) { using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From " + accessory.RootName)) { foreach (var o in searcher.Get()) { object wmi_value = o[accessory.KeyName]; if (!Equals(wmi_value, null)) { prop.SetValue(instance, (string)wmi_value); AddCache(accessory.RootName, accessory.KeyName, (string)wmi_value); SaveCache(); } } } } } return(instance); }