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
22//******************************************************************************************************22//******************************************************************************************************
23 23
24using System;24using System;
25using GSF;
25using GSF.Data;26using GSF.Data;
26using GSF.TimeSeries.UI;27using GSF.TimeSeries.UI;
27 28
@@ -55,7 +56,7 @@ namespace PowerCalculations.PowerMultiCalculator · 27 unmodified lines
55 measurement.SignalTypeID, measurement.SignalReference, measurement.Adder, measurement.Multiplier, measurement.Description.ToNotNull(), 56 measurement.SignalTypeID, measurement.SignalReference, measurement.Adder, measurement.Multiplier, measurement.Description.ToNotNull(),
56 database.Bool(measurement.Enabled), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);57 database.Bool(measurement.Enabled), CommonFunctions.CurrentUser, database.UtcNow, CommonFunctions.CurrentUser, database.UtcNow);
57 58
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);
59 }60 }
60 else61 else
61 {62 {
Source/Libraries/GSF.Core/Data/AdoDataConnection.cs+15−1
@@ -684,7 +684,21 @@ namespace GSF.Data · 683 unmodified lines
684 684
685 // Nullable types cannot be used in type conversion, but we can use Nullable.GetUnderlyingType()685 // Nullable types cannot be used in type conversion, but we can use Nullable.GetUnderlyingType()
686 // to determine whether the type is nullable and convert to the underlying type instead686 // 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;
688 }702 }
689 703
690 /// <summary>704 /// <summary>
Source/Libraries/GSF.Core/StringExtensions.cs+2−6
@@ -206,12 +206,8 @@ namespace GSF · 205 unmodified lines
206 try206 try
207 {207 {
208 // Handle booleans as a special case to allow numeric entries as well as true/false208 // Handle booleans as a special case to allow numeric entries as well as true/false
209 if (type.IsAssignableFrom(typeof(bool)))209 if (type == typeof(bool))
210 return (T)((object)value.ParseBoolean());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);
215 211
216 // Handle objects that have type converters (e.g., Enum, Color, Point, etc.)212 // Handle objects that have type converters (e.g., Enum, Color, Point, etc.)
217 TypeConverter converter = TypeDescriptor.GetConverter(type);213 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