patterncsharpMinor
Genericizing PropertyValues
Viewed 0 times
propertyvaluesgenericizingstackoverflow
Problem
This should be the last iteration for this code.
-
Does this approach make sense or am I heading down the wrong path?
-
The only thing I can see to make this better is to genericize the
What I've done between the last question and now:
I've used a private method
Step 1
I decided to edit the
Where the first string in the dictionary is the type the property will return and second in the dictionary is the
Step 2
I updated the calls to the
To...
```
public class Win32BaseBoard
{
-
Does this approach make sense or am I heading down the wrong path?
-
The only thing I can see to make this better is to genericize the
GetType methods at the end so that I could eliminate the goofy if statement in the GetProperties method. Any suggestions?What I've done between the last question and now:
I've used a private method
GetProperty as suggested in the last question in order to minimize the repeated code.private object GetProperty(string propertyName)
{
using (ManagementObjectSearcher moSearcher = new ManagementObjectSearcher
("SELECT " + propertyName + " FROM " + WmiClassName))
using (var collection = moSearcher.Get())
using (var enu = collection.GetEnumerator())
return (!enu.MoveNext() || enu.Current[propertyName] == null) ? null : enu.Current[propertyName];
}Step 1
I decided to edit the
PropertyValue class along these lines to handle the dictionarypublic PropertyValue(Dictionary propertyList, string wmiClassName).Where the first string in the dictionary is the type the property will return and second in the dictionary is the
propertyName as a string so that it is easy to work with. I'd like to genericize the type part so I could use a list or array instead of a dictionary. Really just to eliminate one of the string variables.Step 2
I updated the calls to the
WMIClasses:public class Win32Processor
{
public Win32Processor() { }
readonly PropertyValue propertyValue = new PropertyValue("Win32_Processor");
///
/// Processor architecture used by the platform.
///
public ushort Architecture()
{ return propertyValue.GetUnignedInt16("Architecture"); }
///
/// Description of the object. This property is inherited from CIM_ManagedSystemElement.
///
public string Description()
{ return propertyValue.GetString("Description"); }
}To...
```
public class Win32BaseBoard
{
Solution
First:
Screaming to be a
Second, these guys in every
a) are immutable once created, so it makes better sense to make them class-level variables. So they're initialized only once.
b) should then be
c) should be declared as
Third:
Fourth:
Those are initial thoughts.
if (pair.Key.ToLower() == "datetime")
{ return GetDateTimeFromDmtf(pair.Value); }
if (pair.Key.ToLower() == "string")
{ return GetString(pair.Value); }
if (pair.Key.ToLower() == "ulong")
{ return GetUnsignedInt64(pair.Value); }
if (pair.Key.ToLower() == "bool")
{ return GetBool(pair.Value); }
if (pair.Key.ToLower() == "ushort")
{ return GetUnignedInt16(pair.Value); }
if (pair.Key.ToLower() == "uint")
{ return GetUnsignedInt32(pair.Value); }Screaming to be a
switch statement.Second, these guys in every
GetValues method:Dictionary propertyList = new Dictionary(){
///
///
///
{"ushort", "AddressWidth"},
///
///
///
{"ushort", "Architecture"},};a) are immutable once created, so it makes better sense to make them class-level variables. So they're initialized only once.
b) should then be
readonly.c) should be declared as
IDictionary so that you're developing against an interface rather than an implementation. This will need to be changed throughout method signatures, etc.Third:
GetUnignedInt16 really should be spelled correctly. GetUnsignedInt16.Fourth:
GetProperties() in the PropertyValue class does not "get properties". It gets a single property. It takes the Dictionary, but only returns the first property value (this is due to return being in the foreach loop. My guess is maybe you mean to add to a list or array?). Seems like a misnaming and a bug at the same time. Fix the code before the review.Those are initial thoughts.
Code Snippets
if (pair.Key.ToLower() == "datetime")
{ return GetDateTimeFromDmtf(pair.Value); }
if (pair.Key.ToLower() == "string")
{ return GetString(pair.Value); }
if (pair.Key.ToLower() == "ulong")
{ return GetUnsignedInt64(pair.Value); }
if (pair.Key.ToLower() == "bool")
{ return GetBool(pair.Value); }
if (pair.Key.ToLower() == "ushort")
{ return GetUnignedInt16(pair.Value); }
if (pair.Key.ToLower() == "uint")
{ return GetUnsignedInt32(pair.Value); }Dictionary<string, string> propertyList = new Dictionary<string, string>(){
/// <summary>
///
/// </summary>
{"ushort", "AddressWidth"},
/// <summary>
///
/// </summary>
{"ushort", "Architecture"},};Context
StackExchange Code Review Q#43777, answer score: 6
Revisions (0)
No revisions yet.