History

GSF.Core: Fixed type conversions for Guids and Enums in AdoDataConnection.ExecuteScalar<T>().

StephenCWills committed 10 years ago

Commit c021a4c1 parent 1ac4ccb3 files · +19 −8Browse files

Unified · Split

3 files
  1. Source/Libraries/Adapters/PowerCalculations/PowerMultiCalculator/MeasurementRepository.cs +2 −1
  2. Source/Libraries/GSF.Core/Data/AdoDataConnection.cs +15 −1
  3. Source/Libraries/GSF.Core/StringExtensions.cs +2 −6
Source/Libraries/Adapters/PowerCalculations/PowerMultiCalculator/MeasurementRepository.cs+2−1
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 //******************************************************************************************************
2323
2424 using System;
25+using GSF;
2526 using GSF.Data;
2627 using GSF.TimeSeries.UI;
2728
@@ -55,7 +56,7 @@ namespace PowerCalculations.PowerMultiCalculator · 27 unmodified lines
5556 measurement.SignalTypeID, measurement.SignalReference, measurement.Adder, measurement.Multiplier, measurement.Description.ToNotNull(),
5657 database.Bool(measurement.Enabled), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);
5758
58− measurement.SignalID = Guid.Parse(database.ExecuteScalar<object>(Guid.Empty, "SELECT SignalID FROM Measurement WHERE PointTag={0}", measurement.PointTag).ToString());
59+ measurement.SignalID = database.ExecuteScalar<Guid>("SELECT SignalID FROM Measurement WHERE PointTag={0}", measurement.PointTag);
5960 }
6061 else
6162 {
Source/Libraries/GSF.Core/Data/AdoDataConnection.cs+15−1
@@ -684,7 +684,21 @@ namespace GSF.Data · 683 unmodified lines
684684
685685 // Nullable types cannot be used in type conversion, but we can use Nullable.GetUnderlyingType()
686686 // to determine whether the type is nullable and convert to the underlying type instead
687− return (T)Convert.ChangeType(value, Nullable.GetUnderlyingType(type) ?? type);
687+ type = Nullable.GetUnderlyingType(type) ?? type;
688+
689+ // Handle Guids
690+ if (type == typeof(Guid))
691+ return (T)(object)System.Guid.Parse(value.ToString());
692+
693+ // Handle string types that may have a converter function (e.g., Enums)
694+ if (value.GetType() == typeof(string))
695+ return value.ToString().ConvertToType<T>(type);
696+
697+ // Handle native types
698+ if (typeof(IConvertible).IsAssignableFrom(value.GetType()))
699+ return (T)Convert.ChangeType(value, type);
700+
701+ return (T)value;
688702 }
689703
690704 /// <summary>
Source/Libraries/GSF.Core/StringExtensions.cs+2−6
@@ -206,12 +206,8 @@ namespace GSF · 205 unmodified lines
206206 try
207207 {
208208 // Handle booleans as a special case to allow numeric entries as well as true/false
209− if (type.IsAssignableFrom(typeof(bool)))
210− return (T)((object)value.ParseBoolean());
211−
212− // This is faster for native types than using type converter...
213− if (type.IsAssignableFrom(typeof(IConvertible)))
214− return (T)Convert.ChangeType(value, type, culture);
209+ if (type == typeof(bool))
210+ return (T)(object)value.ParseBoolean();
215211
216212 // Handle objects that have type converters (e.g., Enum, Color, Point, etc.)
217213 TypeConverter converter = TypeDescriptor.GetConverter(type);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t