-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathLineBuilderBase.cs
More file actions
46 lines (37 loc) · 1.51 KB
/
LineBuilderBase.cs
File metadata and controls
46 lines (37 loc) · 1.51 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
namespace FlatFile.Core.Base
{
public abstract class LineBuilderBase<TLayoutDescriptor, TFieldSettings> : ILineBuilder
where TLayoutDescriptor : ILayoutDescriptor<TFieldSettings>
where TFieldSettings : IFieldSettingsContainer
{
private readonly TLayoutDescriptor _descriptor;
protected LineBuilderBase(TLayoutDescriptor descriptor)
{
this._descriptor = descriptor;
}
protected TLayoutDescriptor Descriptor
{
get { return _descriptor; }
}
public abstract string BuildLine<T>(T entry);
protected virtual string GetStringValueFromField(TFieldSettings field, object fieldValue)
{
string lineValue = fieldValue != null
? ConvertToString(field, fieldValue)
: field.NullValue ?? string.Empty;
lineValue = TransformFieldValue(field, lineValue);
return lineValue;
}
protected virtual string TransformFieldValue(TFieldSettings field, string lineValue)
{
return lineValue;
}
private static string ConvertToString(TFieldSettings field, object fieldValue)
{
var converter = field.TypeConverter;
if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(field.PropertyInfo.PropertyType))
return field.TypeConverter.ConvertToString(fieldValue, field.PropertyInfo);
return fieldValue.ToString();
}
}
}