forked from CoreHelpers/AzureStorageTable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyInfoSetValueFromEntityProperty.cs
More file actions
109 lines (94 loc) · 4.11 KB
/
PropertyInfoSetValueFromEntityProperty.cs
File metadata and controls
109 lines (94 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.WindowsAzure.Storage.Table;
namespace CoreHelpers.WindowsAzure.Storage.Table.Extensions
{
public static class PropertyInfoSetValueFromEntityProperty
{
public static void SetValueFromEntityProperty(this PropertyInfo property, Object entity, EntityProperty entityProperty)
{
if (entityProperty.IsNull())
{
property.SetOrAddValue(entity, null, false);
}
else
{
// define the propertytype
var isCollection = (property.PropertyType.GetTypeInfo().GetInterface("IList") != null);
var propertyType = property.PropertyType;
// handle colleciton s
if (isCollection)
propertyType = property.PropertyType.GenericTypeArguments[0];
var nullableUnderlyingType = Nullable.GetUnderlyingType(propertyType);
if (nullableUnderlyingType != null)
propertyType = nullableUnderlyingType;
switch (entityProperty.PropertyType)
{
case EdmType.String:
int intEnum;
if (propertyType.IsEnum && int.TryParse(entityProperty.StringValue, out intEnum) && propertyType.IsEnumDefined(intEnum))
property.SetOrAddValue(entity, Enum.ToObject(property.PropertyType, intEnum), isCollection);
else if (propertyType.IsEnum && propertyType.IsEnumDefined(entityProperty.StringValue))
property.SetOrAddValue(entity, Enum.Parse(propertyType, entityProperty.StringValue), isCollection);
else if (propertyType == typeof(string))
property.SetOrAddValue(entity, entityProperty.StringValue, isCollection);
break;
case EdmType.Binary:
if (propertyType != typeof(byte[]))
break;
property.SetOrAddValue(entity, entityProperty.BinaryValue, isCollection);
break;
case EdmType.Boolean:
if (propertyType != typeof(bool) && propertyType != typeof(bool?))
break;
property.SetOrAddValue(entity, entityProperty.BooleanValue, isCollection);
break;
case EdmType.DateTime:
if (propertyType == typeof(DateTime))
{
property.SetOrAddValue(entity, entityProperty.DateTimeOffsetValue.Value.UtcDateTime, isCollection);
}
else if (propertyType == typeof(DateTime?))
{
property.SetOrAddValue(entity, entityProperty.DateTimeOffsetValue.HasValue ? entityProperty.DateTimeOffsetValue.Value.UtcDateTime : (DateTime?)null, isCollection);
}
else if (propertyType == typeof(DateTimeOffset))
{
property.SetOrAddValue(entity, entityProperty.DateTimeOffsetValue.Value, isCollection);
}
else if (propertyType == typeof(DateTimeOffset?))
{
property.SetOrAddValue(entity, entityProperty.DateTimeOffsetValue, isCollection);
}
break;
case EdmType.Double:
if (propertyType != typeof(double) && propertyType != typeof(double?))
break;
property.SetOrAddValue(entity, entityProperty.DoubleValue, isCollection);
break;
case EdmType.Guid:
if (propertyType != typeof(Guid) && propertyType != typeof(Guid?))
break;
property.SetOrAddValue(entity, entityProperty.GuidValue, isCollection);
break;
case EdmType.Int32:
if (propertyType != typeof(int) && propertyType != typeof(int?) &&
propertyType != typeof(double) && propertyType != typeof(double?))
break;
if (propertyType == typeof(double) || propertyType == typeof(double?))
property.SetOrAddValue(entity, Convert.ToDouble(entityProperty.Int32Value), isCollection);
else
property.SetOrAddValue(entity, entityProperty.Int32Value, isCollection);
break;
case EdmType.Int64:
if (propertyType != typeof(long) && propertyType != typeof(long?))
break;
property.SetOrAddValue(entity, entityProperty.Int64Value, isCollection);
break;
}
}
}
}
}