Replace CntrlComparison with LeafGasComparison
This commit is contained in:
@@ -24,6 +24,8 @@ namespace LeafWeb.Core.Utility
|
||||
|
||||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
|
||||
{
|
||||
if (value is bool)
|
||||
return value;
|
||||
if (value is string)
|
||||
{
|
||||
var s = value as string;
|
||||
|
||||
@@ -8,6 +8,7 @@ namespace LeafWeb.Core.Utility
|
||||
public class ParseInfoAttribute : Attribute
|
||||
{
|
||||
public int Position { get; private set; }
|
||||
public int? BoolEncodedPosition { get; private set; }
|
||||
public string Units { get; private set; }
|
||||
public string Title { get; private set; }
|
||||
public string AlterateTitle { get; private set; }
|
||||
@@ -23,13 +24,16 @@ namespace LeafWeb.Core.Utility
|
||||
}
|
||||
}
|
||||
|
||||
public ParseInfoAttribute(int position, [CallerMemberName]string title = null, string units = null, string alternateTitle = null, string exampleValue = null)
|
||||
public ParseInfoAttribute(int position, [CallerMemberName]string title = null,
|
||||
string units = null, string alternateTitle = null, string exampleValue = null,
|
||||
int boolEncodedPosition = -1)
|
||||
{
|
||||
AlterateTitle = alternateTitle;
|
||||
ExampleValue = exampleValue;
|
||||
Title = title;
|
||||
Position = position;
|
||||
Units = units;
|
||||
BoolEncodedPosition = boolEncodedPosition == -1 ? (int?)null : boolEncodedPosition;
|
||||
}
|
||||
|
||||
public bool IsTitleMatch(string title)
|
||||
|
||||
@@ -132,6 +132,7 @@ namespace LeafWeb.Core.Utility
|
||||
return string.IsNullOrEmpty(value) || value == "NA" || value == "-9999";
|
||||
}
|
||||
|
||||
// TODO: this call maybe could be cached
|
||||
private static PropertyInfo MatchProperty(PropertyInfo[] properties, string title, int position)
|
||||
{
|
||||
return
|
||||
@@ -139,6 +140,7 @@ namespace LeafWeb.Core.Utility
|
||||
properties.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsPositionMatch(position));
|
||||
}
|
||||
|
||||
// TODO: this call maybe could be cached
|
||||
private static PropertyInfo MatchPropertyExact(PropertyInfo[] properties, string title, int position)
|
||||
{
|
||||
return
|
||||
@@ -180,6 +182,7 @@ namespace LeafWeb.Core.Utility
|
||||
{
|
||||
// http://stackoverflow.com/questions/3531318/convert-changetype-fails-on-nullable-types
|
||||
var t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
||||
value = HandleBoolEncodedProperty(property, value);
|
||||
// convertedValue = Convert.ChangeType(value, t);
|
||||
var converter = TypeDescriptor.GetConverter(t);
|
||||
convertedValue = converter.ConvertTo(value, t);
|
||||
@@ -191,5 +194,26 @@ namespace LeafWeb.Core.Utility
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static object HandleBoolEncodedProperty(PropertyInfo property, object value)
|
||||
{
|
||||
var boolEncodedPosition = property.Attribute<ParseInfoAttribute>().BoolEncodedPosition;
|
||||
if (boolEncodedPosition == null) return value;
|
||||
|
||||
var v = (value as string)?.Substring(boolEncodedPosition.Value -1, 1);
|
||||
switch (v)
|
||||
{
|
||||
// these values are False=1, True=2
|
||||
case "1":
|
||||
value = false;
|
||||
break;
|
||||
case "2":
|
||||
value = true;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException($"{property.Name} {boolEncodedPosition} {value} {v}");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user