Skip to content

Commit 16dd918

Browse files
committed
Merge branch 'master' into refactoring
2 parents 4c4393f + d6b7fa8 commit 16dd918

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

Example/Data/Entities/User.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public User()
2525
[NotMapped]
2626
public string Ignored { get; set; }
2727

28+
public UserType Type { get; set; }
29+
2830
public Guid Test2 { get; set; }
2931

3032
[NotMapped]
@@ -47,5 +49,10 @@ public string[] VContents
4749
public virtual List<Content> Contents { get; set; }
4850

4951
public virtual List<Setting> Settings { get; set; }
52+
53+
public enum UserType
54+
{
55+
Admin, User, Manager
56+
}
5057
}
5158
}

Example/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ static void Main(string[] args)
4949
User us = new User()
5050
{
5151
Name = "Morris Janatzek",
52-
Username = ""
52+
Username = "",
53+
Type = User.UserType.Manager,
54+
Test = 2
5355
};
5456

5557
db.Users.Add(us);

FileContextCore/Serializer/SerializerHelper.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ static class SerializerHelper
1313
{
1414
public static object Deserialize(this string input, Type type)
1515
{
16-
type = Nullable.GetUnderlyingType(type) ?? type;
17-
1816
if (string.IsNullOrEmpty(input))
1917
{
2018
return type.GetDefaultValue();
2119
}
2220

21+
type = Nullable.GetUnderlyingType(type) ?? type;
22+
2323
if (type == typeof(DateTimeOffset))
2424
{
2525
return DateTimeOffset.Parse(input, CultureInfo.InvariantCulture);
@@ -29,12 +29,12 @@ public static object Deserialize(this string input, Type type)
2929
{
3030
return TimeSpan.Parse(input, CultureInfo.InvariantCulture);
3131
}
32-
32+
3333
if (type == typeof(Guid))
3434
{
3535
return Guid.Parse(input);
3636
}
37-
37+
3838
if (type.IsArray)
3939
{
4040
Type arrType = type.GetElementType();
@@ -47,7 +47,11 @@ public static object Deserialize(this string input, Type type)
4747

4848
return arr.ToArray();
4949
}
50-
50+
51+
if (type.IsEnum)
52+
{
53+
return Enum.Parse(type, input);
54+
}
5155

5256
return Convert.ChangeType(input, type, CultureInfo.InvariantCulture);
5357
}
@@ -60,7 +64,7 @@ public static string Serialize(this object input)
6064
{
6165
string result = "";
6266

63-
object[] arr = (object[])input;
67+
object[] arr = (object[]) input;
6468

6569
for (int i = 0; i < arr.Length; i++)
6670
{
@@ -75,19 +79,22 @@ public static string Serialize(this object input)
7579
return result;
7680
}
7781

78-
return input is IFormattable formattable ? formattable.ToString(null, CultureInfo.InvariantCulture) : input.ToString();
79-
}
82+
return input is IFormattable formattable
83+
? formattable.ToString(null, CultureInfo.InvariantCulture)
84+
: input.ToString();
85+
}
8086

8187
return "";
8288
}
8389

84-
public static TKey GetKey<TKey, T>(IPrincipalKeyValueFactory<T> keyValueFactory, IEntityType entityType, Func<string, string> valueSelector)
90+
public static TKey GetKey<TKey, T>(IPrincipalKeyValueFactory<T> keyValueFactory, IEntityType entityType,
91+
Func<string, string> valueSelector)
8592
{
86-
return (TKey)keyValueFactory.CreateFromKeyValues(
93+
return (TKey) keyValueFactory.CreateFromKeyValues(
8794
entityType.FindPrimaryKey().Properties
8895
.Select(p =>
8996
valueSelector(p.GetColumnName())
9097
.Deserialize(p.GetValueConverter()?.ProviderClrType ?? p.ClrType)).ToArray());
9198
}
9299
}
93-
}
100+
}

0 commit comments

Comments
 (0)