-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataPropertyExtensions.cs
More file actions
105 lines (99 loc) · 3.43 KB
/
DataPropertyExtensions.cs
File metadata and controls
105 lines (99 loc) · 3.43 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
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace BootstrapBlazor.DataConverters;
static class DataPropertyExtensions
{
public static IDataPropertyConverter? GetConverter(this DataPropertyConverterAttribute attribute)
{
return attribute.GetConverterByType() ?? attribute.GetDefaultConverter();
}
private static IDataPropertyConverter? GetConverterByType(this DataPropertyConverterAttribute attribute)
{
IDataPropertyConverter? converter = null;
var converterType = attribute.ConverterType;
if (converterType != null)
{
var converterParameters = attribute.ConverterParameters;
var c = Activator.CreateInstance(converterType, converterParameters);
if (c is IDataPropertyConverter v)
{
converter = v;
}
}
return converter;
}
private static IDataPropertyConverter? GetDefaultConverter(this DataPropertyConverterAttribute attribute)
{
IDataPropertyConverter? converter = null;
var type = attribute.Type;
if (type != null)
{
if (type == typeof(byte[]))
{
converter = new DataByteArrayConverter();
}
else if (type == typeof(string))
{
converter = new DataStringConverter(attribute.EncodingName);
}
else if (type.IsEnum)
{
converter = new DataEnumConverter(attribute.Type);
}
else if (type == typeof(bool))
{
converter = new DataBoolConverter();
}
else if (type == typeof(short))
{
converter = new DataInt16BigEndianConverter();
}
else if (type == typeof(int))
{
converter = new DataInt32BigEndianConverter();
}
else if (type == typeof(long))
{
converter = new DataInt64BigEndianConverter();
}
else if (type == typeof(float))
{
converter = new DataSingleBigEndianConverter();
}
else if (type == typeof(double))
{
converter = new DataDoubleBigEndianConverter();
}
else if (type == typeof(ushort))
{
converter = new DataUInt16BigEndianConverter();
}
else if (type == typeof(uint))
{
converter = new DataUInt32BigEndianConverter();
}
else if (type == typeof(ulong))
{
converter = new DataUInt64BigEndianConverter();
}
}
return converter;
}
public static object? ConvertTo(this DataPropertyConverterAttribute attribute, ReadOnlyMemory<byte> data)
{
object? ret = null;
var start = attribute.Offset;
var length = attribute.Length;
if (data.Length >= start + length)
{
var buffer = data.Slice(start, length);
var converter = attribute.GetConverter();
if (converter != null)
{
ret = converter.Convert(buffer);
}
}
return ret;
}
}