Skip to content

Commit c73550c

Browse files
authored
Support nullable enums
1 parent 75d159f commit c73550c

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

CoreHelpers.WindowsAzure.Storage.Table/Attributes/StoreEnumAsStringAttribute.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,19 @@ public void ReadProperty<T>(Azure.Data.Tables.TableEntity dataObject, PropertyIn
4242
// get the string value
4343
var stringValue = Convert.ToString(dataObject[propertyInfo.Name]);
4444

45-
// prepare the value
46-
var resultValue = Enum.Parse(propertyInfo.PropertyType, stringValue);
45+
// Handle null/empty stored values quickly
46+
// Nullable type would be defaulted to null already
47+
// non-nullable cannot be set to null
48+
if (string.IsNullOrEmpty(stringValue))
49+
return;
4750

51+
// Support nullable enum properties by parsing against the underlying enum type
52+
var propType = propertyInfo.PropertyType;
53+
var enumType = Nullable.GetUnderlyingType(propType) ?? propType;
4854

49-
// set the value
50-
propertyInfo.SetValue(obj, resultValue);
55+
// Parse the enum stringValue name (stored as string) using the resolved enum type
56+
var parsed = Enum.Parse(enumType, stringValue);
57+
propertyInfo.SetValue(obj, parsed);
5158
}
5259
}
5360
}

0 commit comments

Comments
 (0)