Skip to content

Commit

Permalink
included tests for each function changed + considered enum name or va…
Browse files Browse the repository at this point in the history
…lue during conversion

Allow bind Enum values in IAutoInitialize ViewModels

Currently if anyone try pass through a enum value as raw type(value or name) within INavigationParameters get an error of conversion, to solve this problem was included the conditions above.

Allow bind Enum values in IAutoInitialize ViewModels

Currently if anyone try pass through a enum value as raw type(value or name) within INavigationParameters get an error of conversion, to solve this problem was included the conditions above.

Update ParametersExtensions.cs

force to use only enum name

I'm considering to use only the enum name because if the value is being considered during the conversion can cause side effect because the value as number can be refered to a lot of things in navigation parameters not the enum type searched.
  • Loading branch information
candidodmv authored and dansiegel committed Nov 20, 2019
1 parent cbacc3f commit 26e090c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Source/Prism/Common/ParametersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static object GetValue(this IEnumerable<KeyValuePair<string, object>> par
return kvp.Value;
else if (type.IsAssignableFrom(kvp.Value.GetType()))
return kvp.Value;
else if (type.IsEnum && (Enum.IsDefined(type, kvp.Value.ToString()) || int.TryParse(kvp.Value.ToString(), out var enumValue) && Enum.IsDefined(type, enumValue)))
else if (type.IsEnum && Enum.IsDefined(type, kvp.Value.ToString()))
return Enum.Parse(type, kvp.Value.ToString());
else
return Convert.ChangeType(kvp.Value, type);
Expand All @@ -50,7 +50,7 @@ public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object>>
value = (T)kvp.Value;
else if (type.IsAssignableFrom(kvp.Value.GetType()))
value = (T)kvp.Value;
else if (type.IsEnum && (Enum.IsDefined(type, kvp.Value.ToString()) || int.TryParse(kvp.Value.ToString(), out var enumValue) && Enum.IsDefined(type, enumValue)))
else if (type.IsEnum && Enum.IsDefined(type, kvp.Value.ToString()))
value = (T)Enum.Parse(type, kvp.Value.ToString());
else
value = (T)Convert.ChangeType(kvp.Value, type);
Expand Down Expand Up @@ -79,7 +79,7 @@ public static IEnumerable<T> GetValues<T>(this IEnumerable<KeyValuePair<string,
values.Add((T)kvp.Value);
else if (type.IsAssignableFrom(kvp.Value.GetType()))
values.Add((T)kvp.Value);
else if (type.IsEnum && (Enum.IsDefined(type, kvp.Value.ToString()) || int.TryParse(kvp.Value.ToString(), out var enumValue) && Enum.IsDefined(type, enumValue)))
else if (type.IsEnum && Enum.IsDefined(type, kvp.Value.ToString()))
values.Add((T)Enum.Parse(type, kvp.Value.ToString()));
else
values.Add((T)Convert.ChangeType(kvp.Value, type));
Expand Down

0 comments on commit 26e090c

Please sign in to comment.