├── .gitattributes
├── .gitignore
├── ChangeLog.md
├── LICENSE
├── README.md
├── appveyor.yml
├── assets
├── build.bat
├── icon_256.png
├── icon_v1_160.jpg
├── psake-common.ps1
└── psake-project.ps1
├── nuspecs
├── FlatFile.Core.Attributes.nuspec
├── FlatFile.Core.nuspec
├── FlatFile.Delimited.Attributes.nuspec
├── FlatFile.Delimited.nuspec
├── FlatFile.FixedLength.Attributes.nuspec
├── FlatFile.FixedLength.nuspec
└── FlatFile.nuspec
└── src
├── .editorconfig
├── .nuget
├── NuGet.Config
├── nuget.exe
└── packages.config
├── FlatFile.Benchmark
├── Converters
│ ├── CsvHelperTypeConverterForCustomType.cs
│ └── FlatFileTypeConverterForCustomType.cs
├── Entities
│ ├── CustomObject.cs
│ ├── CustomType.cs
│ └── FixedSampleRecord.cs
├── FlatFile.Benchmark.csproj
├── FlatFileVsCsvHelperBenchmark.cs
├── FlatFileVsFileHelpersBenchmark.cs
├── Generators
│ └── FakeGenarator.cs
├── Mapping
│ ├── CsvHelperMappingForCustomObject.cs
│ ├── DelimitedSampleRecordLayout.cs
│ ├── FixedSampleRecordLayout.cs
│ └── FlatFileMappingForCustomObject.cs
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── FlatFile.Core.Attributes
├── Base
│ ├── FieldSettingsBaseAttribute.cs
│ └── LayoutBaseAttribute.cs
├── Entities
│ └── PropertyDescription.cs
├── Extensions
│ ├── AttributeUtil.cs
│ └── TypeExtensions.cs
├── FlatFile.Core.Attributes.csproj
├── Infrastructure
│ └── ILayoutDescriptorProvider.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.Core
├── Base
│ ├── FieldSettingsBase.cs
│ ├── FieldsContainer.cs
│ ├── FlatFileEngine.cs
│ ├── LayoutBase.cs
│ ├── LayoutDescriptorBase.cs
│ ├── LineBulderBase.cs
│ └── LineParserBase.cs
├── Exceptions
│ └── ParseLineException.cs
├── Extensions
│ ├── ExpressionExtensions.cs
│ ├── FieldsSettingsExtensions.cs
│ ├── ReflectionHelper.cs
│ ├── TypeChangeExtensions.cs
│ └── TypeExtensions.cs
├── FlatFile.Core.csproj
├── IFieldSettingsConstructor.cs
├── IFieldSettingsFactory.cs
├── IFieldsContainer.cs
├── IFlatFileEngine.cs
├── IFlatFileEngineFactory.cs
├── IFlatFileMultiEngine.cs
├── ILayout.cs
├── ILayoutDescriptor.cs
├── ILineBuilderFactory.cs
├── ILineBulder.cs
├── ILineParser.cs
├── ILineParserFactory.cs
├── ITypeConverter.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.Delimited.Attributes
├── DelimitedFieldAttribute.cs
├── DelimitedFileAttribute.cs
├── FlatFile.Delimited.Attributes.csproj
├── FlatFileEngineFactoryExtensions.cs
├── Infrastructure
│ ├── DelimitedLayoutDescriptorProvider.cs
│ └── DelimitedMultiLayoutDescriptor.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.Delimited
├── DelimitedFieldSettings.cs
├── FlatFile.Delimited.csproj
├── IDelimitedFieldSettingsConstructor.cs
├── IDelimitedLayout.cs
├── IDelimitedLayoutDescriptor.cs
├── IDelimitedLineBuilder.cs
├── IDelimitedLineBuilderFactory.cs
├── IDelimitedLineParser.cs
├── IDelimitedLineParserFactory.cs
├── IDetailRecord.cs
├── IMasterRecord.cs
├── Implementation
│ ├── DelimetedFileMultiEngine.cs
│ ├── DelimitedFieldSettingsConstructor.cs
│ ├── DelimitedFieldSettingsFactory.cs
│ ├── DelimitedFileEngine.cs
│ ├── DelimitedFileEngineFactory.cs
│ ├── DelimitedLayout.cs
│ ├── DelimitedLineBuilder.cs
│ ├── DelimitedLineBuilderFactory.cs
│ ├── DelimitedLineParser.cs
│ └── DelimitedLineParserFactory.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.FixedLength.Attributes
├── FixedLengthFieldAttribute.cs
├── FixedLengthFileAttribute.cs
├── FlatFile.FixedLength.Attributes.csproj
├── FlatFileEngineFactoryExtensions.cs
├── Infrastructure
│ └── FixedLayoutDescriptorProvider.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.FixedLength
├── FixedFieldSettings.cs
├── FlatFile.FixedLength.csproj
├── IDetailRecord.cs
├── IFixedFieldSettingsConstructor.cs
├── IFixedLayout.cs
├── IFixedLengthLineBuilder.cs
├── IFixedLengthLineBuilderFactory.cs
├── IFixedLengthLineParser.cs
├── IFixedLengthLineParserFactory.cs
├── IMasterRecord.cs
├── Implementation
│ ├── FixedFieldSettingsConstructor.cs
│ ├── FixedFieldSettingsFactory.cs
│ ├── FixedLayout.cs
│ ├── FixedLengthFileEngine.cs
│ ├── FixedLengthFileEngineFactory.cs
│ ├── FixedLengthFileMultiEngine.cs
│ ├── FixedLengthLineBuilder.cs
│ ├── FixedLengthLineBuilderFactory.cs
│ ├── FixedLengthLineParser.cs
│ └── FixedLengthLineParserFactory.cs
├── Padding.cs
└── Properties
│ └── AssemblyInfo.cs
├── FlatFile.Tests
├── App.config
├── Base
│ ├── Entities
│ │ └── TestObject.cs
│ └── IntegrationTests.cs
├── Core
│ └── FieldsContainerTests.cs
├── Delimited
│ ├── DelimitedAttributeMappingIntegrationTests.cs
│ ├── DelimitedIntegrationTests.cs
│ ├── DelimitedLayoutTests.cs
│ ├── DelimitedMultiEngineTests.cs
│ └── DelimitedWithHeaderIntegrationTests.cs
├── FixedLength
│ ├── FixedLayoutTests.cs
│ ├── FixedLengthAttributeMappingIntegrationTests.cs
│ ├── FixedLengthIntegrationTests.cs
│ ├── FixedLengthLineParserTests.cs
│ ├── FixedLengthMasterDetailTests.cs
│ └── FixedLengthMultiEngineTests.cs
├── FlatFile.Tests.csproj
├── Properties
│ └── AssemblyInfo.cs
├── Specifications
│ ├── Defenitions
│ │ └── FixedLengthFileDefinitions.cs
│ ├── Entities
│ │ └── FixedLengthTypeMapping.cs
│ ├── Extensions
│ │ ├── FileEngineExtensions.cs
│ │ ├── ScenarioContextExtensions.cs
│ │ └── StringExtensions.cs
│ ├── FixedLengthFileMappingFeature.feature
│ ├── FixedLengthFileMappingFeature.feature.cs
│ └── Transforms
│ │ └── TransformsBinding.cs
└── packages.config
├── FlatFile.sln
└── SharedAssemblyInfo.cs
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 |
9 | # Build results
10 |
11 | [Dd]ebug/
12 | [Rr]elease/
13 | x64/
14 | build/
15 | [Bb]in/
16 | [Oo]bj/
17 |
18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
19 | !packages/*/build/
20 |
21 | # MSTest test Results
22 | [Tt]est[Rr]esult*/
23 | [Bb]uild[Ll]og.*
24 |
25 | *_i.c
26 | *_p.c
27 | *.ilk
28 | *.meta
29 | *.obj
30 | *.pch
31 | *.pdb
32 | *.pgc
33 | *.pgd
34 | *.rsp
35 | *.sbr
36 | *.tlb
37 | *.tli
38 | *.tlh
39 | *.tmp
40 | *.tmp_proj
41 | *.log
42 | *.vspscc
43 | *.vssscc
44 | .builds
45 | *.pidb
46 | *.log
47 | *.scc
48 |
49 | # Visual C++ cache files
50 | ipch/
51 | *.aps
52 | *.ncb
53 | *.opensdf
54 | *.sdf
55 | *.cachefile
56 |
57 | # Visual Studio profiler
58 | *.psess
59 | *.vsp
60 | *.vspx
61 |
62 | # Guidance Automation Toolkit
63 | *.gpState
64 |
65 | # ReSharper is a .NET coding add-in
66 | _ReSharper*/
67 | *.[Rr]e[Ss]harper
68 |
69 | # TeamCity is a build add-in
70 | _TeamCity*
71 |
72 | # DotCover is a Code Coverage Tool
73 | *.dotCover
74 |
75 | # NCrunch
76 | *.ncrunch*
77 | .*crunch*.local.xml
78 |
79 | # Installshield output folder
80 | [Ee]xpress/
81 |
82 | # DocProject is a documentation generator add-in
83 | DocProject/buildhelp/
84 | DocProject/Help/*.HxT
85 | DocProject/Help/*.HxC
86 | DocProject/Help/*.hhc
87 | DocProject/Help/*.hhk
88 | DocProject/Help/*.hhp
89 | DocProject/Help/Html2
90 | DocProject/Help/html
91 |
92 | # Click-Once directory
93 | publish/
94 |
95 | # Publish Web Output
96 | *.Publish.xml
97 |
98 | # NuGet Packages Directory
99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
100 | packages/
101 |
102 | # Windows Azure Build Output
103 | csx
104 | *.build.csdef
105 |
106 | # Windows Store app package directory
107 | AppPackages/
108 |
109 | # Others
110 | sql/
111 | *.Cache
112 | ClientBin/
113 | [Ss]tyle[Cc]op.*
114 | ~$*
115 | *~
116 | *.dbmdl
117 | *.[Pp]ublish.xml
118 | *.pfx
119 | *.publishsettings
120 |
121 | # RIA/Silverlight projects
122 | Generated_Code/
123 |
124 | # Backup & report files from converting an old project file to a newer
125 | # Visual Studio version. Backup files are not needed, because we have git ;-)
126 | _UpgradeReport_Files/
127 | Backup*/
128 | UpgradeLog*.XML
129 | UpgradeLog*.htm
130 |
131 | # SQL Server files
132 | App_Data/*.mdf
133 | App_Data/*.ldf
134 |
135 |
136 | #LightSwitch generated files
137 | GeneratedArtifacts/
138 | _Pvt_Extensions/
139 | ModelManifest.xml
140 |
141 | # =========================
142 | # Windows detritus
143 | # =========================
144 |
145 | # Windows image file caches
146 | Thumbs.db
147 | ehthumbs.db
148 |
149 | # Folder config file
150 | Desktop.ini
151 |
152 | # Recycle Bin used on file shares
153 | $RECYCLE.BIN/
154 |
155 | # Mac desktop service store files
156 | .DS_Store
157 | assets/FlatFile.Core.Compiled.nuspec
158 | assets/FlatFile.Delimited.Compiled.nuspec
159 | assets/FlatFile.FixedLength.Compiled.nuspec
160 |
--------------------------------------------------------------------------------
/ChangeLog.md:
--------------------------------------------------------------------------------
1 | # ChangeLog / ReleaseNotes
2 |
3 | ## Version 0.2.23
4 |
5 | * Fixed csv tag in the nuget packages.
6 | * Refactored psake with adding release notes to nuget.
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Pavel Nosovich
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | -
2 | version: 0.2.{build}
3 |
4 | environment:
5 | BuildEnvironment: appveyor
6 |
7 | branches:
8 | only:
9 | - master
10 |
11 | assembly_info:
12 | patch: true
13 | file: AssemblyInfo.*
14 | assembly_version: "{version}"
15 | assembly_file_version: "{version}"
16 | assembly_informational_version: "{version}"
17 |
18 | test: off
19 |
20 | artifacts:
21 | - path: 'build\**\*.nupkg'
22 |
23 | build_script: assets\build.bat pack
24 |
25 | cache:
26 | - src\packages # preserve "packages" directory in the root of build folder
27 |
28 | deploy:
29 | provider: NuGet
30 | api_key:
31 | secure: yNaPU9GGsdDdMjOB5tbQTuIIq4DFHNGUkZP1ZTDDCKQG3g7pZjxcBFZblTMeIawA
32 | artifact: /.*build.*\.nupkg/
33 | on:
34 | branch: master
35 |
36 | -
37 | version: 0.2.{build}
38 |
39 | environment:
40 | BuildEnvironment: appveyor
41 |
42 | branches:
43 | only:
44 | - /dev.*/
45 | - /feature.*/
46 |
47 | assembly_info:
48 | patch: true
49 | file: AssemblyInfo.*
50 | assembly_version: "{version}"
51 | assembly_file_version: "{version}"
52 | assembly_informational_version: "{version}"
53 |
54 | build_script: assets\build.bat pack
55 |
56 | cache:
57 | - src\packages # preserve "packages" directory in the root of build folder
58 |
--------------------------------------------------------------------------------
/assets/build.bat:
--------------------------------------------------------------------------------
1 | @echo off
2 |
3 | src\.nuget\NuGet.exe install src\.nuget\packages.config -OutputDirectory src\packages
4 |
5 | powershell.exe -NoProfile -ExecutionPolicy unrestricted -Command "& {Import-Module '.\src\packages\psake.4.4.1\tools\psake.psm1'; invoke-psake '.\assets\psake-project.ps1' %*; if ($LastExitCode -and $LastExitCode -ne 0) {write-host "ERROR CODE: $LastExitCode" -fore RED; exit $lastexitcode} }"
6 |
--------------------------------------------------------------------------------
/assets/icon_256.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/assets/icon_256.png
--------------------------------------------------------------------------------
/assets/icon_v1_160.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/assets/icon_v1_160.jpg
--------------------------------------------------------------------------------
/assets/psake-common.ps1:
--------------------------------------------------------------------------------
1 | Properties {
2 | ### Directories
3 | $base_dir = $root
4 | $build_dir = "$base_dir\build"
5 | $src_dir = "$base_dir\src"
6 | $package_dir = "$src_dir\packages"
7 | $nuspec_dir = "$base_dir\nuspecs"
8 | $temp_dir = "$build_dir\Temp"
9 | $framework_dir = $env:windir + "\Microsoft.Net\Framework\v4.0.30319"
10 |
11 | ### Tools
12 | $nuget = "$src_dir\.nuget\nuget.exe"
13 | $xunit = "$package_dir\xunit.runners*\tools\xunit.console.clr4.exe"
14 | $7zip = "$package_dir\7-Zip.CommandLine.*\tools\7za.exe"
15 |
16 | ### AppVeyor-related
17 | $appVeyorConfig = "$base_dir\appveyor.yml"
18 | $appVeyor = $env:APPVEYOR
19 |
20 | ### Project information
21 | $solution_path = "$src_dir\$solution"
22 | $sharedAssemblyInfo = "$src_dir\SharedAssemblyInfo.cs"
23 | $config = "Release"
24 | $frameworks = @("NET35", "NET40", "NET45")
25 |
26 | ### Files
27 | $releaseNotes = "$base_dir\ChangeLog.md"
28 | }
29 |
30 | ## Tasks
31 |
32 | Task Restore -Description "Restore NuGet packages for solution." {
33 | "Restoring NuGet packages for '$solution_path'..."
34 | Exec { .$nuget restore $solution_path }
35 | }
36 |
37 | Task Clean -Description "Clean up build and project folders." {
38 | Clean-Directory $build_dir
39 |
40 | if ($solution) {
41 | "Cleaning up '$solution'..."
42 |
43 | foreach ($framework in $frameworks) {
44 | Exec { msbuild $solution_path /target:Clean /nologo /verbosity:minimal /p:Framework=$framework}
45 | }
46 | }
47 | }
48 |
49 | Task Compile -Depends Clean, Restore -Description "Compile all the projects in a solution." {
50 | "Compiling '$solution'..."
51 |
52 | $extra = $null
53 | if ($appVeyor) {
54 | $extra = "/logger:C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
55 | }
56 |
57 | foreach ($framework in $frameworks) {
58 | Exec { msbuild $solution_path /p:"Configuration=$config;Framework=$framework" /nologo /verbosity:minimal $extra }
59 | }
60 | }
61 |
62 | ### Pack functions
63 |
64 | function Create-Package($project, $version, $notes) {
65 | Create-Directory $temp_dir
66 | Copy-Files "$nuspec_dir\$project.nuspec" $temp_dir
67 |
68 | Try {
69 | Replace-Content "$nuspec_dir\$project.nuspec" '#releaseNotes#' $notes
70 |
71 | Replace-Content "$nuspec_dir\$project.nuspec" '$version$' $version
72 |
73 | Exec { .$nuget pack "$nuspec_dir\$project.nuspec" -OutputDirectory "$build_dir" -BasePath "$build_dir" -Version $version}
74 | }
75 | Finally {
76 | Move-Files "$temp_dir\$project.nuspec" $nuspec_dir
77 | }
78 | }
79 |
80 | function Get-ReleaseNotes {
81 | $content = (Get-Content "$releaseNotes") -Join "`n"
82 | return $content
83 | }
84 |
85 | ### Version functions
86 |
87 | function Get-BuildVersion {
88 | $version = Get-SharedVersion
89 | $buildVersion = $env:APPVEYOR_BUILD_VERSION
90 |
91 | if ($buildVersion -ne $null) {
92 | $version = $buildVersion
93 | }
94 |
95 | return $version
96 | }
97 |
98 | function Get-SharedVersion {
99 | $line = Get-Content "$sharedAssemblyInfo" | where {$_.Contains("AssemblyVersion")}
100 | $line.Split('"')[1]
101 | }
102 |
103 | function Update-AppveyorVersion($version) {
104 | Check-Version($version)
105 |
106 | $versionPattern = "version: [0-9]+(\.([0-9]+|\*)){1,3}"
107 | $versionReplace = "version: $version"
108 |
109 | if (Test-Path $appVeyorConfig) {
110 | "Patching $appVeyorConfig..."
111 | Replace-Content "$appVeyorConfig" $versionPattern $versionReplace
112 | }
113 | }
114 |
115 |
116 | ### Common functions
117 |
118 | function Create-Directory($dir) {
119 | New-Item -Path $dir -Type Directory -Force > $null
120 | }
121 |
122 | function Clean-Directory($dir) {
123 | If (Test-Path $dir) {
124 | "Cleaning up '$dir'..."
125 | Remove-Item "$dir\*" -Recurse -Force
126 | }
127 | }
128 |
129 | function Copy-Files($source, $destination) {
130 | Copy-Item "$source" $destination -Force > $null
131 | }
132 |
133 | function Move-Files($source, $destination) {
134 | Move-Item "$source" $destination -Force > $null
135 | }
136 |
137 | function Replace-Content($file, $pattern, $substring) {
138 | (gc $file) -Replace $pattern, $substring | sc $file
139 | }
140 |
--------------------------------------------------------------------------------
/assets/psake-project.ps1:
--------------------------------------------------------------------------------
1 | Properties {
2 | $solution = "FlatFile.sln"
3 | }
4 |
5 | $root = (split-path -parent $MyInvocation.MyCommand.Definition) + '\..'
6 |
7 | Include "$root\assets\psake-common.ps1"
8 |
9 | Task Default -Depends Pack
10 |
11 | Task Pack -Depends Compile -Description "Create NuGet packages and archive files." {
12 | $version = Get-BuildVersion
13 | $releaseNotes = Get-ReleaseNotes
14 |
15 | $projects = @(
16 | "FlatFile.Core",
17 | "FlatFile.Core.Attributes",
18 | "FlatFile.Delimited",
19 | "FlatFile.FixedLength",
20 | "FlatFile.Delimited.Attributes",
21 | "FlatFile.FixedLength.Attributes",
22 | "FlatFile"
23 | )
24 |
25 | $projects | ForEach {
26 | Create-Package $_ $version $releaseNotes
27 | }
28 | }
--------------------------------------------------------------------------------
/nuspecs/FlatFile.Core.Attributes.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.Core.Attributes.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.Core.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.Core.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.Delimited.Attributes.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.Delimited.Attributes.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.Delimited.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.Delimited.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.FixedLength.Attributes.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.FixedLength.Attributes.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.FixedLength.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.FixedLength.nuspec
--------------------------------------------------------------------------------
/nuspecs/FlatFile.nuspec:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/nuspecs/FlatFile.nuspec
--------------------------------------------------------------------------------
/src/.editorconfig:
--------------------------------------------------------------------------------
1 | root=true
2 |
3 | [*]
4 | end_of_line = LF
5 |
6 | [*.cs]
7 | indent_style = space
8 | indent_size = 4
9 |
10 | [*.markdown]
11 | indent_style = space
12 | indent_size = 2
13 |
14 | [*.json]
15 | indent_style = space
16 | indent_size = 4
--------------------------------------------------------------------------------
/src/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/src/.nuget/nuget.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/forcewake/FlatFile/20004b34148fbf5c949ee1c77502a57ad6818e14/src/.nuget/nuget.exe
--------------------------------------------------------------------------------
/src/.nuget/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Converters/CsvHelperTypeConverterForCustomType.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Converters
2 | {
3 | using System;
4 | using CsvHelperTypeConversion = CsvHelper.TypeConversion;
5 |
6 | public class CsvHelperTypeConverterForCustomType : CsvHelperTypeConversion.ITypeConverter
7 | {
8 | private readonly FlatFileTypeConverterForCustomType converter;
9 |
10 | public CsvHelperTypeConverterForCustomType()
11 | {
12 | converter = new FlatFileTypeConverterForCustomType();
13 | }
14 |
15 | public string ConvertToString(CsvHelperTypeConversion.TypeConverterOptions options, object value)
16 | {
17 | return converter.ConvertToString(value);
18 | }
19 |
20 | public object ConvertFromString(CsvHelperTypeConversion.TypeConverterOptions options, string text)
21 | {
22 | return converter.ConvertFromString(text);
23 | }
24 |
25 | public bool CanConvertFrom(Type type)
26 | {
27 | return converter.CanConvertFrom(type);
28 | }
29 |
30 | public bool CanConvertTo(Type type)
31 | {
32 | return converter.CanConvertTo(type);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Converters/FlatFileTypeConverterForCustomType.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Converters
2 | {
3 | using System;
4 | using FlatFile.Benchmark.Entities;
5 | using FlatFile.Core;
6 |
7 | public class FlatFileTypeConverterForCustomType : ITypeConverter
8 | {
9 | public bool CanConvertFrom(Type type)
10 | {
11 | return type == typeof(string);
12 | }
13 |
14 | public bool CanConvertTo(Type type)
15 | {
16 | return type == typeof (CustomType);
17 | }
18 |
19 | public string ConvertToString(object source)
20 | {
21 | var obj = (CustomType)source;
22 | return string.Format("{0}|{1}|{2}", obj.First, obj.Second, obj.Third);
23 | }
24 |
25 | public object ConvertFromString(string source)
26 | {
27 | var values = source.Split('|');
28 |
29 | var obj = new CustomType
30 | {
31 | First = int.Parse(values[0]),
32 | Second = int.Parse(values[1]),
33 | Third = int.Parse(values[2]),
34 | };
35 | return obj;
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Entities/CustomObject.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Entities
2 | {
3 | using System;
4 | using Newtonsoft.Json;
5 |
6 | public class CustomObject
7 | {
8 | public Guid GuidColumn { get; set; }
9 |
10 | public int IntColumn { get; set; }
11 |
12 | public string StringColumn { get; set; }
13 |
14 | public string IgnoredColumn { get; set; }
15 |
16 | public CustomType CustomTypeColumn { get; set; }
17 |
18 | public override string ToString()
19 | {
20 | return JsonConvert.SerializeObject(this);
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Entities/CustomType.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Entities
2 | {
3 | using Newtonsoft.Json;
4 |
5 | public class CustomType
6 | {
7 | public int First { get; set; }
8 | public int Second { get; set; }
9 | public int Third { get; set; }
10 |
11 | public override string ToString()
12 | {
13 | return JsonConvert.SerializeObject(this);
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Entities/FixedSampleRecord.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Entities
2 | {
3 | using System;
4 | using FileHelpers;
5 |
6 | ///
7 | /// Sample fixed length record for testing
8 | ///
9 | [FixedLengthRecord()]
10 | public class FixedSampleRecord : IEquatable
11 | {
12 | [FieldFixedLength(11)]
13 | private long _cuit;
14 |
15 | [FieldFixedLength(160)]
16 | [FieldTrim(TrimMode.Both)]
17 | private string _nombre;
18 |
19 | [FieldFixedLength(6)]
20 | private int _actividad;
21 |
22 | public long Cuit
23 | {
24 | get { return _cuit; }
25 | set { _cuit = value; }
26 | }
27 |
28 | public string Nombre
29 | {
30 | get { return _nombre; }
31 | set { _nombre = value; }
32 | }
33 |
34 | public int Actividad
35 | {
36 | get { return _actividad; }
37 | set { _actividad = value; }
38 | }
39 |
40 | public bool Equals(FixedSampleRecord other)
41 | {
42 | if (ReferenceEquals(null, other)) return false;
43 | if (ReferenceEquals(this, other)) return true;
44 | return _cuit == other._cuit && string.Equals(_nombre, other._nombre) && _actividad == other._actividad;
45 | }
46 |
47 | public override bool Equals(object obj)
48 | {
49 | if (ReferenceEquals(null, obj)) return false;
50 | if (ReferenceEquals(this, obj)) return true;
51 | if (obj.GetType() != this.GetType()) return false;
52 | return Equals((FixedSampleRecord) obj);
53 | }
54 |
55 | public override int GetHashCode()
56 | {
57 | unchecked
58 | {
59 | int hashCode = _cuit.GetHashCode();
60 | hashCode = (hashCode*397) ^ (_nombre != null ? _nombre.GetHashCode() : 0);
61 | hashCode = (hashCode*397) ^ _actividad;
62 | return hashCode;
63 | }
64 | }
65 |
66 | public static bool operator ==(FixedSampleRecord left, FixedSampleRecord right)
67 | {
68 | return Equals(left, right);
69 | }
70 |
71 | public static bool operator !=(FixedSampleRecord left, FixedSampleRecord right)
72 | {
73 | return !Equals(left, right);
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/FlatFileVsCsvHelperBenchmark.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark
2 | {
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Linq;
7 | using System.Text;
8 | using BenchmarkIt;
9 | using CsvHelper;
10 | using FlatFile.Benchmark.Entities;
11 | using FlatFile.Benchmark.Mapping;
12 | using FlatFile.Delimited.Implementation;
13 | using FluentAssertions;
14 | using Xunit;
15 |
16 | public class FlatFileVsCsvHelperBenchmark
17 | {
18 | [Fact(Skip = "Too long for CI")]
19 | public void WriteAllRecordsWithMapping()
20 | {
21 | var records = new List
22 | {
23 | new CustomObject
24 | {
25 | GuidColumn = new Guid("f96a1c66-4777-4642-86fa-703098065f5f"),
26 | IntColumn = 1,
27 | StringColumn = "one",
28 | CustomTypeColumn = new CustomType
29 | {
30 | First = 1,
31 | Second = 2,
32 | Third = 3,
33 | },
34 | },
35 | new CustomObject
36 | {
37 | GuidColumn = new Guid("06776ed9-d33f-470f-bd3f-8db842356330"),
38 | IntColumn = 2,
39 | StringColumn = "two",
40 | CustomTypeColumn = new CustomType
41 | {
42 | First = 4,
43 | Second = 5,
44 | Third = 6,
45 | },
46 | },
47 | };
48 |
49 | Benchmark.This("CsvWriter.WriteRecords", () =>
50 | {
51 | using (var memoryStream = new MemoryStream())
52 | using (var streamWriter = new StreamWriter(memoryStream))
53 | using (var writer = new CsvWriter(streamWriter))
54 | {
55 | writer.Configuration.RegisterClassMap();
56 |
57 | writer.WriteRecords(records);
58 |
59 | streamWriter.Flush();
60 | }
61 | })
62 | .Against.This("FlatFileEngine.Write", () =>
63 | {
64 | var layout = new FlatFileMappingForCustomObject();
65 | using (var stream = new MemoryStream())
66 | {
67 | var factory = new DelimitedFileEngineFactory();
68 |
69 | var flatFile = factory.GetEngine(layout);
70 |
71 | flatFile.Write(stream, records);
72 | }
73 | })
74 | .WithWarmup(1000)
75 | .For(10000)
76 | .Iterations()
77 | .PrintComparison();
78 | }
79 |
80 | [Fact(Skip = "Too long for CI")]
81 | public void ReadAllRecordsWithMapping()
82 | {
83 | const string fileContent =
84 | @"String Column,Int Column,Guid Column,Custom Type Column
85 | one,1,f96a1c66-4777-4642-86fa-703098065f5f,1|2|3
86 | two,2,06776ed9-d33f-470f-bd3f-8db842356330,4|5|6
87 | ";
88 | Benchmark.This("CsvWriter.WriteRecords", () =>
89 | {
90 | using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent)))
91 | using (var streamReader = new StreamReader(memoryStream))
92 | using (var reader = new CsvReader(streamReader))
93 | {
94 | reader.Configuration.RegisterClassMap();
95 |
96 | var objects = reader.GetRecords().ToArray();
97 | }
98 | })
99 | .Against.This("FlatFileEngine.Write", () =>
100 | {
101 | var layout = new FlatFileMappingForCustomObject();
102 | using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(fileContent)))
103 | {
104 | var factory = new DelimitedFileEngineFactory();
105 |
106 | var flatFile = factory.GetEngine(layout);
107 |
108 | var objects = flatFile.Read(stream).ToArray();
109 |
110 | }
111 | })
112 | .WithWarmup(1000)
113 | .For(10000)
114 | .Iterations()
115 | .PrintComparison();
116 | }
117 | }
118 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Generators/FakeGenarator.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Generators
2 | {
3 | using FakeO;
4 | using FlatFile.Benchmark.Entities;
5 |
6 | public class FakeGenarator
7 | {
8 | public FixedSampleRecord Generate(int next)
9 | {
10 | var record = Create.Fake(
11 | c => c.Cuit = next,
12 | c => c.Nombre = String.Random(160),
13 | c => c.Actividad = Number.Next(100, 200));
14 |
15 | return record;
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Mapping/CsvHelperMappingForCustomObject.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Mapping
2 | {
3 | using CsvHelper.Configuration;
4 | using FlatFile.Benchmark.Converters;
5 | using FlatFile.Benchmark.Entities;
6 |
7 | public sealed class CsvHelperMappingForCustomObject : CsvClassMap
8 | {
9 | public CsvHelperMappingForCustomObject()
10 | {
11 | Map(m => m.CustomTypeColumn).Name("Custom Type Column").Index(3).TypeConverter();
12 | Map(m => m.GuidColumn).Name("Guid Column").Index(2);
13 | Map(m => m.IntColumn).Name("Int Column").Index(1);
14 | Map(m => m.StringColumn).Name("String Column").Index(0);
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Mapping/DelimitedSampleRecordLayout.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Mapping
2 | {
3 | using FlatFile.Benchmark.Entities;
4 | using FlatFile.Delimited.Implementation;
5 |
6 | public sealed class DelimitedSampleRecordLayout : DelimitedLayout
7 | {
8 | public DelimitedSampleRecordLayout()
9 | {
10 | this.WithDelimiter(";")
11 | .WithQuote("\"")
12 | .WithMember(x => x.Cuit)
13 | .WithMember(x => x.Nombre)
14 | .WithMember(x => x.Actividad, c => c.WithName("AnotherName"));
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Mapping/FixedSampleRecordLayout.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Mapping
2 | {
3 | using FlatFile.Benchmark.Entities;
4 | using FlatFile.FixedLength.Implementation;
5 |
6 | public sealed class FixedSampleRecordLayout : FixedLayout
7 | {
8 | public FixedSampleRecordLayout()
9 | {
10 | this.WithMember(x => x.Cuit, c => c.WithLength(11))
11 | .WithMember(x => x.Nombre, c => c.WithLength(160))
12 | .WithMember(x => x.Actividad, c => c.WithLength(6));
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Mapping/FlatFileMappingForCustomObject.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Benchmark.Mapping
2 | {
3 | using FlatFile.Benchmark.Converters;
4 | using FlatFile.Benchmark.Entities;
5 | using FlatFile.Delimited.Implementation;
6 |
7 | public sealed class FlatFileMappingForCustomObject : DelimitedLayout
8 | {
9 | public FlatFileMappingForCustomObject()
10 | {
11 | this.WithHeader()
12 | .WithDelimiter(",")
13 | .WithMember(m => m.StringColumn, c => c.WithName("String Column"))
14 | .WithMember(m => m.IntColumn, c => c.WithName("Int Column"))
15 | .WithMember(m => m.GuidColumn, c => c.WithName("Guid Column"))
16 | .WithMember(m => m.CustomTypeColumn, c => c.WithName("Custom Type Column").WithTypeConverter());
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FlatFile.Benchmark")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("EPAM Systems")]
12 | [assembly: AssemblyProduct("FlatFile.Benchmark")]
13 | [assembly: AssemblyCopyright("Copyright © EPAM Systems 2014")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("26c53229-856a-4060-90bc-151ffd28e789")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/FlatFile.Benchmark/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Base/FieldSettingsBaseAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Attributes.Base
2 | {
3 | using System;
4 | using FlatFile.Core.Base;
5 | using FlatFile.Core.Extensions;
6 |
7 | [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
8 | public abstract class FieldSettingsBaseAttribute : Attribute, IFieldSettings
9 | {
10 | public int? Index { get; set; }
11 |
12 | public bool IsNullable
13 | {
14 | get { return !string.IsNullOrEmpty(NullValue); }
15 | }
16 |
17 | public string NullValue { get; set; }
18 |
19 | public Type Converter { get; set; }
20 |
21 | public ITypeConverter TypeConverter
22 | {
23 | get { return (ITypeConverter) ReflectionHelper.CreateInstance(Converter, true); }
24 | }
25 |
26 | protected FieldSettingsBaseAttribute(int index)
27 | {
28 | Index = index;
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Base/LayoutBaseAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Attributes.Base
2 | {
3 | using System;
4 |
5 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
6 | public abstract class LayoutBaseAttribute : Attribute
7 | {
8 | public bool HasHeader { get; set; }
9 | }
10 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Entities/PropertyDescription.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Attributes.Entities
2 | {
3 | using System;
4 | using System.Reflection;
5 |
6 | internal class PropertyDescription
7 | {
8 | public PropertyInfo Property { get; set; }
9 | public Attribute[] Attributes { get; set; }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Extensions/AttributeUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 |
6 | namespace FlatFile.Core.Attributes.Extensions
7 | {
8 | internal static class AttributeUtil
9 | {
10 | public static T GetAttribute(this FieldInfo field, bool inherited = true) where T : Attribute
11 | {
12 | return field.GetAttributes(inherited).FirstOrDefault();
13 | }
14 |
15 |
16 | public static T GetAttribute(this MemberInfo member, bool inherited = true) where T : Attribute
17 | {
18 | return member.GetAttributes(inherited).FirstOrDefault();
19 | }
20 |
21 |
22 | public static T GetAttribute(this PropertyInfo property, bool inherited = true) where T : Attribute
23 | {
24 | return property.GetAttributes(inherited).FirstOrDefault();
25 | }
26 |
27 |
28 | public static T GetAttribute(this MethodInfo method, bool inherited = true) where T : Attribute
29 | {
30 | return method.GetAttributes(inherited).FirstOrDefault();
31 | }
32 |
33 |
34 | public static T GetAttribute(this ParameterInfo param, bool inherited = true) where T : Attribute
35 | {
36 | return param.GetAttributes(inherited).FirstOrDefault();
37 | }
38 |
39 |
40 | public static T GetAttribute(this Type type, bool inherited = true) where T : Attribute
41 | {
42 | return type.GetAttributes(inherited).FirstOrDefault();
43 | }
44 |
45 |
46 | public static IEnumerable GetAttributes(this MemberInfo member, bool inherited = true) where T : Attribute
47 | {
48 | return (T[]) member.GetCustomAttributes(typeof (T), inherited);
49 | }
50 |
51 |
52 | public static IEnumerable GetAttributes(this PropertyInfo property, bool inherited = true)
53 | where T : Attribute
54 | {
55 | return (T[]) property.GetCustomAttributes(typeof (T), inherited);
56 | }
57 |
58 |
59 | public static IEnumerable GetAttributes(this FieldInfo field, bool inherited = true) where T : Attribute
60 | {
61 | return field.GetCustomAttributes(typeof (T), inherited).Cast();
62 | }
63 |
64 |
65 | public static IEnumerable GetAttributes(this MethodInfo method, bool inherited = true) where T : Attribute
66 | {
67 | return method.GetCustomAttributes(typeof (T), inherited).Cast();
68 | }
69 |
70 |
71 | public static IEnumerable GetAttributes(this ParameterInfo param, bool inherited = true)
72 | where T : Attribute
73 | {
74 | return param.GetCustomAttributes(typeof (T), inherited).Cast();
75 | }
76 |
77 |
78 | public static IEnumerable GetAttributes(this Type type, bool inherited = true) where T : Attribute
79 | {
80 | return type.GetCustomAttributes(typeof (T), inherited).Cast();
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Extensions/TypeExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Attributes.Extensions
2 | {
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using FlatFile.Core.Attributes.Entities;
7 |
8 | internal static class TypeExtensions
9 | {
10 | public static IEnumerable GetTypeDescription(this Type targetType) where TAttribute : Attribute
11 | {
12 | var properties = from p in targetType.GetProperties()
13 | where Attribute.IsDefined(p, typeof(TAttribute))
14 | let attr = p.GetCustomAttributes(typeof(TAttribute), true)
15 | select new PropertyDescription { Property = p, Attributes = attr.Cast().ToArray() };
16 |
17 | return properties;
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/FlatFile.Core.Attributes.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {5849B415-6CC1-4615-A94F-265F5BD6F234}
8 | Library
9 | Properties
10 | FlatFile.Core.Attributes
11 | FlatFile.Core.Attributes
12 | v3.5
13 | 512
14 |
15 | NET35
16 | ..\
17 | true
18 |
19 |
20 | true
21 | full
22 | false
23 | bin\$(Configuration)\$(Framework)\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 |
28 |
29 | pdbonly
30 | true
31 | bin\$(Configuration)\$(Framework)\
32 | TRACE
33 | prompt
34 | 4
35 | bin\$(Configuration)\$(Framework)\$(AssemblyName).XML
36 |
37 |
38 |
39 |
40 | v4.0
41 | AnyCPU
42 | true
43 | full
44 | false
45 | bin\$(Configuration)\$(Framework)\
46 | DEBUG;TRACE
47 | prompt
48 | 4
49 |
50 |
51 |
52 |
53 | v4.0
54 | AnyCPU
55 | pdbonly
56 | true
57 | bin\$(Configuration)\$(Framework)\
58 | TRACE
59 | prompt
60 | 4
61 | bin\$(Configuration)\$(Framework)\$(AssemblyName).XML
62 |
63 |
64 |
65 |
66 | v4.5
67 | AnyCPU
68 | true
69 | full
70 | false
71 | bin\$(Configuration)\$(Framework)\
72 | DEBUG;TRACE
73 | prompt
74 | 4
75 |
76 |
77 |
78 |
79 | v4.5
80 | AnyCPU
81 | pdbonly
82 | true
83 | bin\$(Configuration)\$(Framework)\
84 | TRACE
85 | prompt
86 | 4
87 | bin\$(Configuration)\$(Framework)\$(AssemblyName).XML
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 | {1cb90052-b97a-4ad4-b9fd-20a22914d129}
109 | FlatFile.Core
110 |
111 |
112 |
113 |
120 |
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Infrastructure/ILayoutDescriptorProvider.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Attributes.Infrastructure
2 | {
3 | using FlatFile.Core.Base;
4 |
5 | public interface ILayoutDescriptorProvider
6 | where TFieldSettings : IFieldSettings
7 | where TLayoutDescriptor : ILayoutDescriptor
8 | {
9 | TLayoutDescriptor GetDescriptor();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/FlatFile.Core.Attributes/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("FlatFile.Core.Attributes")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("FlatFile.Core.Attributes")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("3825eaff-28bf-4e11-bce8-5fcb63273c03")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 | [assembly: InternalsVisibleTo("FlatFile.Tests")]
38 | [assembly: InternalsVisibleTo("FlatFile.Delimited.Attributes")]
39 | [assembly: InternalsVisibleTo("FlatFile.FixedLength.Attributes")]
40 |
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/FieldSettingsBase.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | using System.Reflection;
4 |
5 | public interface IFieldSettings
6 | {
7 | int? Index { get; set; }
8 | bool IsNullable { get; }
9 | string NullValue { get; }
10 | ITypeConverter TypeConverter { get; }
11 | }
12 |
13 | public interface IFieldSettingsContainer : IFieldSettings
14 | {
15 | PropertyInfo PropertyInfo { get; }
16 | }
17 |
18 | public abstract class FieldSettingsBase : IFieldSettingsContainer
19 | {
20 | protected FieldSettingsBase(PropertyInfo propertyInfo)
21 | {
22 | PropertyInfo = propertyInfo;
23 | }
24 |
25 | protected FieldSettingsBase(IFieldSettings settings)
26 | {
27 | Index = settings.Index;
28 | IsNullable = settings.IsNullable;
29 | NullValue = settings.NullValue;
30 | }
31 |
32 | protected FieldSettingsBase(PropertyInfo propertyInfo, IFieldSettings settings) : this(settings)
33 | {
34 | PropertyInfo = propertyInfo;
35 | }
36 |
37 | public int? Index { get; set; }
38 | public bool IsNullable { get; set; }
39 | public string NullValue { get; set; }
40 | public ITypeConverter TypeConverter { get; set; }
41 | public PropertyInfo PropertyInfo { get; set; }
42 | }
43 |
44 | public class PropertySettingsContainer where TPropertySettings : IFieldSettings
45 | {
46 | public int Index { get; set; }
47 | public TPropertySettings PropertySettings { get; set; }
48 | }
49 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/FieldsContainer.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Reflection;
6 |
7 | public class AutoOrderedFieldsContainer : FieldsContainer
8 | where TFieldSettings : IFieldSettingsContainer
9 | {
10 | private int _currentPropertyId = 0;
11 |
12 | public override void AddOrUpdate(PropertyInfo propertyInfo, TFieldSettings settings)
13 | {
14 | settings.Index = _currentPropertyId++;
15 |
16 | base.AddOrUpdate(propertyInfo, settings);
17 | }
18 | }
19 |
20 | public class FieldsContainer : IFieldsContainer
21 | where TFieldSettings : IFieldSettings
22 | {
23 | protected Dictionary> Fields { get; private set; }
24 |
25 | public FieldsContainer()
26 | {
27 | Fields = new Dictionary>();
28 | }
29 |
30 | public virtual void AddOrUpdate(PropertyInfo propertyInfo, TFieldSettings settings)
31 | {
32 | var propertySettings = new PropertySettingsContainer
33 | {
34 | PropertySettings = settings,
35 | Index = settings.Index.GetValueOrDefault()
36 | };
37 |
38 | Fields[propertyInfo] = propertySettings;
39 | }
40 |
41 | public virtual IEnumerable OrderedFields
42 | {
43 | get
44 | {
45 | return Fields.Values
46 | .OrderBy(settings => settings.Index)
47 | .Select(x => x.PropertySettings)
48 | .AsEnumerable();
49 | }
50 | }
51 | }
52 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/FlatFileEngine.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using FlatFile.Core;
7 | using FlatFile.Core.Exceptions;
8 |
9 | ///
10 | /// Class FlatFileEngine.
11 | ///
12 | /// The type of the t field settings.
13 | /// The type of the t layout descriptor.
14 | public abstract class FlatFileEngine : IFlatFileEngine
15 | where TFieldSettings : IFieldSettings
16 | where TLayoutDescriptor : ILayoutDescriptor
17 | {
18 | ///
19 | /// The handle entry read error func
20 | ///
21 | private readonly Func _handleEntryReadError;
22 |
23 | ///
24 | /// Gets the line builder.
25 | ///
26 | /// The line builder.
27 | protected abstract ILineBulder LineBuilder { get; }
28 |
29 | ///
30 | /// Gets the line parser.
31 | ///
32 | /// The line parser.
33 | protected abstract ILineParser LineParser { get; }
34 |
35 | ///
36 | /// Gets the layout descriptor.
37 | ///
38 | /// The layout descriptor.
39 | protected abstract TLayoutDescriptor LayoutDescriptor { get; }
40 |
41 | ///
42 | /// Initializes a new instance of the class.
43 | ///
44 | /// The handle entry read error.
45 | protected FlatFileEngine(Func handleEntryReadError = null)
46 | {
47 | _handleEntryReadError = handleEntryReadError;
48 | }
49 |
50 | ///
51 | /// Reads the specified stream.
52 | ///
53 | /// The type of the t entity.
54 | /// The stream.
55 | /// IEnumerable<TEntity>.
56 | /// Impossible to parse line
57 | public virtual IEnumerable Read(Stream stream) where TEntity : class, new()
58 | {
59 | var reader = new StreamReader(stream);
60 | string line;
61 | int lineNumber = 0;
62 |
63 | if (LayoutDescriptor.HasHeader)
64 | {
65 | ProcessHeader(reader);
66 | }
67 |
68 | while ((line = reader.ReadLine()) != null)
69 | {
70 | if (string.IsNullOrEmpty(line) || string.IsNullOrEmpty(line.Trim())) continue;
71 |
72 | bool ignoreEntry = false;
73 | var entry = new TEntity();
74 | try
75 | {
76 | if (!TryParseLine(line, lineNumber++, ref entry))
77 | {
78 | throw new ParseLineException("Impossible to parse line", line, lineNumber);
79 | }
80 | }
81 | catch (Exception ex)
82 | {
83 | if (_handleEntryReadError == null)
84 | {
85 | throw;
86 | }
87 |
88 | if (!_handleEntryReadError(line, ex))
89 | {
90 | throw;
91 | }
92 |
93 | ignoreEntry = true;
94 | }
95 |
96 | if (!ignoreEntry)
97 | {
98 | yield return entry;
99 | }
100 | }
101 | }
102 |
103 | ///
104 | /// Processes the header.
105 | ///
106 | /// The reader.
107 | protected virtual void ProcessHeader(StreamReader reader)
108 | {
109 | reader.ReadLine();
110 | }
111 |
112 | ///
113 | /// Tries to parse the line.
114 | ///
115 | /// The type of the t entity.
116 | /// The line.
117 | /// The line number.
118 | /// The entity.
119 | /// true if XXXX, false otherwise.
120 | protected virtual bool TryParseLine(string line, int lineNumber, ref TEntity entity) where TEntity : class, new()
121 | {
122 | LineParser.ParseLine(line, entity);
123 |
124 | return true;
125 | }
126 |
127 | ///
128 | /// Writes the entry.
129 | ///
130 | /// The type of the t entity.
131 | /// The writer.
132 | /// The line number.
133 | /// The entity.
134 | protected virtual void WriteEntry(TextWriter writer, int lineNumber, TEntity entity)
135 | {
136 | var line = LineBuilder.BuildLine(entity);
137 |
138 | writer.WriteLine(line);
139 | }
140 |
141 | ///
142 | /// Writes to the specified stream.
143 | ///
144 | /// The type of the t entity.
145 | /// The stream.
146 | /// The entries.
147 | public virtual void Write(Stream stream, IEnumerable entries) where TEntity : class, new()
148 | {
149 | TextWriter writer = new StreamWriter(stream);
150 |
151 | this.WriteHeader(writer);
152 |
153 | int lineNumber = 0;
154 |
155 | foreach (var entry in entries)
156 | {
157 | this.WriteEntry(writer, lineNumber, entry);
158 |
159 | lineNumber += 1;
160 | }
161 |
162 | this.WriteFooter(writer);
163 |
164 | writer.Flush();
165 | }
166 |
167 | ///
168 | /// Writes the header.
169 | ///
170 | /// The writer.
171 | protected virtual void WriteHeader(TextWriter writer)
172 | {
173 | }
174 |
175 | ///
176 | /// Writes the footer.
177 | ///
178 | /// The writer.
179 | protected virtual void WriteFooter(TextWriter writer)
180 | {
181 | }
182 | }
183 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/LayoutBase.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | using System;
4 | using System.Linq.Expressions;
5 | using System.Reflection;
6 | using FlatFile.Core.Extensions;
7 |
8 | public abstract class LayoutBase
9 | : LayoutDescriptorBase, ILayout
10 | where TFieldSettings : class, IFieldSettings
11 | where TConstructor : IFieldSettingsConstructor
12 | where TLayout : ILayout
13 | {
14 | private readonly IFieldSettingsFactory _fieldSettingsFactory;
15 |
16 | protected LayoutBase(
17 | IFieldSettingsFactory fieldSettingsFactory,
18 | IFieldsContainer fieldsContainer) : base(fieldsContainer)
19 | {
20 | this._fieldSettingsFactory = fieldSettingsFactory;
21 | }
22 |
23 | protected virtual void ProcessProperty(Expression> expression, Action settings)
24 | {
25 | var propertyInfo = GetPropertyInfo(expression);
26 |
27 | var constructor = _fieldSettingsFactory.CreateFieldSettings(propertyInfo);
28 |
29 | if (settings != null)
30 | {
31 | settings(constructor);
32 | }
33 |
34 | FieldsContainer.AddOrUpdate(constructor.PropertyInfo, constructor as TFieldSettings);
35 | }
36 |
37 | protected virtual PropertyInfo GetPropertyInfo(Expression> expression)
38 | {
39 | var propertyInfo = expression.GetPropertyInfo();
40 | return propertyInfo;
41 | }
42 |
43 | public override Type TargetType { get { return typeof (TTarget); } }
44 |
45 | public abstract TLayout WithMember(Expression> expression, Action settings = null);
46 |
47 | public abstract TLayout WithHeader();
48 | }
49 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/LayoutDescriptorBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace FlatFile.Core.Base
5 | {
6 | public class LayoutDescriptorBase : ILayoutDescriptor
7 | where TFieldSettings : IFieldSettings
8 | {
9 | protected IFieldsContainer FieldsContainer { get; private set; }
10 |
11 | public LayoutDescriptorBase(IFieldsContainer fieldsContainer)
12 | {
13 | FieldsContainer = fieldsContainer;
14 | }
15 |
16 | public LayoutDescriptorBase(IFieldsContainer fieldsContainer, Type targetType) : this(fieldsContainer) { TargetType = targetType; }
17 |
18 | public virtual Type TargetType { get; private set; }
19 |
20 | public IEnumerable Fields
21 | {
22 | get { return FieldsContainer.OrderedFields; }
23 | }
24 |
25 | public bool HasHeader { get; protected internal set; }
26 | }
27 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/LineBulderBase.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | public abstract class LineBulderBase : ILineBulder
4 | where TLayoutDescriptor : ILayoutDescriptor
5 | where TFieldSettings : IFieldSettingsContainer
6 | {
7 | private readonly TLayoutDescriptor _descriptor;
8 |
9 | protected LineBulderBase(TLayoutDescriptor descriptor)
10 | {
11 | this._descriptor = descriptor;
12 | }
13 |
14 | protected TLayoutDescriptor Descriptor
15 | {
16 | get { return _descriptor; }
17 | }
18 |
19 | public abstract string BuildLine(T entry);
20 |
21 | protected virtual string GetStringValueFromField(TFieldSettings field, object fieldValue)
22 | {
23 | string lineValue = fieldValue != null
24 | ? fieldValue.ToString()
25 | : field.NullValue ?? string.Empty;
26 |
27 | lineValue = TransformFieldValue(field, lineValue);
28 |
29 | return lineValue;
30 | }
31 |
32 | protected virtual string TransformFieldValue(TFieldSettings field, string lineValue)
33 | {
34 | return lineValue;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Base/LineParserBase.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Base
2 | {
3 | using System;
4 | using FlatFile.Core.Extensions;
5 |
6 | public abstract class LineParserBase : ILineParser
7 | where TLayoutDescriptor : ILayoutDescriptor
8 | where TFieldSettings : IFieldSettingsContainer
9 | {
10 | private readonly TLayoutDescriptor _layout;
11 |
12 | protected LineParserBase(TLayoutDescriptor layout)
13 | {
14 | this._layout = layout;
15 | }
16 |
17 | protected TLayoutDescriptor Layout
18 | {
19 | get { return _layout; }
20 | }
21 |
22 | public abstract TEntity ParseLine(string line, TEntity entity) where TEntity : new();
23 |
24 | protected virtual object GetFieldValueFromString(TFieldSettings fieldSettings, string memberValue)
25 | {
26 | if (fieldSettings.IsNullable && memberValue.Trim('"').Equals(fieldSettings.NullValue, StringComparison.InvariantCultureIgnoreCase))
27 | {
28 | return null;
29 | }
30 |
31 | var type = fieldSettings.PropertyInfo.PropertyType;
32 |
33 | if (fieldSettings.IsNullablePropertyType())
34 | {
35 | type = Nullable.GetUnderlyingType(fieldSettings.PropertyInfo.PropertyType);
36 | }
37 |
38 | memberValue = TransformStringValue(fieldSettings, memberValue);
39 |
40 | object obj;
41 |
42 | if (!fieldSettings.TypeConverter.ConvertFromStringTo(memberValue, type, out obj))
43 | {
44 | obj = memberValue.Convert(type);
45 | }
46 |
47 | return obj;
48 | }
49 |
50 | protected virtual string TransformStringValue(TFieldSettings fieldSettingsBuilder, string memberValue)
51 | {
52 | return memberValue;
53 | }
54 | }
55 |
56 | public static class TypeConverterExtensions
57 | {
58 |
59 | public static bool ConvertFromStringTo(this ITypeConverter converter, string source, Type targetType, out object obj)
60 | {
61 | if (converter != null && converter.CanConvertFrom(typeof(string)) && converter.CanConvertTo(targetType))
62 | {
63 | obj = converter.ConvertFromString(source);
64 | return true;
65 | }
66 |
67 | obj = null;
68 |
69 | return false;
70 | }
71 | }
72 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Exceptions/ParseLineException.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Exceptions
2 | {
3 | using System;
4 | using System.Runtime.Serialization;
5 |
6 | [Serializable]
7 | public class ParseLineException : Exception
8 | {
9 | public ParseLineException(string message, Exception innerException, string line, int lineNumber)
10 | : base(message, innerException)
11 | {
12 | Line = line;
13 | LineNumber = lineNumber;
14 | }
15 |
16 | public ParseLineException(string message, string line, int lineNumber)
17 | : base(message)
18 | {
19 | Line = line;
20 | LineNumber = lineNumber;
21 | }
22 |
23 | public ParseLineException(string line, int lineNumber)
24 | {
25 | Line = line;
26 | LineNumber = lineNumber;
27 | }
28 |
29 | public int LineNumber { get; private set; }
30 | public string Line { get; private set; }
31 |
32 | protected ParseLineException(SerializationInfo info, StreamingContext context, string line, int lineNumber)
33 | : base(info, context)
34 | {
35 | Line = line;
36 | LineNumber = lineNumber;
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/FlatFile.Core/Extensions/ExpressionExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Extensions
2 | {
3 | using System;
4 | using System.Linq.Expressions;
5 | using System.Reflection;
6 |
7 | internal static class ExpressionExtensions
8 | {
9 | ///
10 | /// Gets the name of the member specified
11 | ///
12 | /// The type referenced
13 | /// The type of the member referenced
14 | /// The expression referencing the member
15 | /// The name of the member referenced by the expression
16 | public static string GetMemberName(this Expression> expression)
17 | {
18 | return expression.GetMemberExpression().Member.Name;
19 | }
20 |
21 | ///
22 | /// Gets the name of the member specified
23 | ///
24 | /// The type referenced
25 | /// The expression referencing the member
26 | /// The name of the member referenced by the expression
27 | public static string GetMemberName(this Expression> expression)
28 | {
29 | return expression.GetMemberExpression().Member.Name;
30 | }
31 |
32 | public static string GetMemberName(this Expression> expression)
33 | {
34 | return expression.GetMemberExpression().Member.Name;
35 | }
36 |
37 | public static PropertyInfo GetPropertyInfo(this Expression> expression)
38 | {
39 | return expression.GetMemberExpression().Member as PropertyInfo;
40 | }
41 |
42 | public static PropertyInfo GetPropertyInfo(this Expression> expression)
43 | {
44 | return expression.GetMemberExpression().Member as PropertyInfo;
45 | }
46 |
47 | public static MemberInfo GetMemberInfo(this Expression> expression)
48 | {
49 | return expression.GetMemberExpression().Member;
50 | }
51 |
52 | public static MemberExpression GetMemberExpression(this Expression> expression)
53 | {
54 | if (expression == null)
55 | throw new ArgumentNullException("expression");
56 |
57 | return GetMemberExpression(expression.Body);
58 | }
59 |
60 | public static MemberExpression GetMemberExpression(this Expression> expression)
61 | {
62 | if (expression == null)
63 | throw new ArgumentNullException("expression");
64 | return GetMemberExpression(expression.Body);
65 | }
66 |
67 | public static MemberExpression GetMemberExpression(this Expression> expression)
68 | {
69 | if (expression == null)
70 | throw new ArgumentNullException("expression");
71 | return GetMemberExpression(expression.Body);
72 | }
73 |
74 | public static MemberExpression GetMemberExpression(this Expression> expression)
75 | {
76 | if (expression == null)
77 | throw new ArgumentNullException("expression");
78 |
79 | return GetMemberExpression(expression.Body);
80 | }
81 |
82 | private static MemberExpression GetMemberExpression(Expression body)
83 | {
84 | if (body == null)
85 | throw new ArgumentNullException("body");
86 |
87 | MemberExpression memberExpression = null;
88 | if (body.NodeType == ExpressionType.Convert)
89 | {
90 | var unaryExpression = (UnaryExpression)body;
91 | memberExpression = unaryExpression.Operand as MemberExpression;
92 | }
93 | else if (body.NodeType == ExpressionType.MemberAccess)
94 | memberExpression = body as MemberExpression;
95 |
96 | if (memberExpression == null)
97 | throw new ArgumentException("Expression is not a member access");
98 |
99 | return memberExpression;
100 | }
101 | }
102 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Extensions/FieldsSettingsExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Extensions
2 | {
3 | using System;
4 | using FlatFile.Core.Base;
5 |
6 | public static class FieldsSettingsExtensions
7 | {
8 | public static bool IsNullablePropertyType(this TFieldSettings fieldSettings)
9 | where TFieldSettings : IFieldSettingsContainer
10 | {
11 | return fieldSettings.PropertyInfo.PropertyType.IsGenericType
12 | && fieldSettings.PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Extensions/ReflectionHelper.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using System.Reflection;
3 |
4 | namespace FlatFile.Core.Extensions
5 | {
6 | using System;
7 | using System.Collections.Generic;
8 | using System.Linq.Expressions;
9 |
10 | ///
11 | /// Class ReflectionHelper.
12 | ///
13 | public static class ReflectionHelper
14 | {
15 | static readonly object CacheLock = new object();
16 | static readonly Dictionary Cache = new Dictionary();
17 |
18 | ///
19 | /// Creates an instance of type using the default constructor.
20 | ///
21 | ///
22 | /// if set to true [cached].
23 | /// T.
24 | public static T CreateInstance(bool cached = false) { return (T) CreateInstance(typeof (T), cached); }
25 |
26 | ///
27 | /// Creates an instance of type using the default constructor.
28 | ///
29 | /// Type of the target.
30 | /// if set to true [cached].
31 | /// System.Object.
32 | public static object CreateInstance(Type targetType, bool cached = false)
33 | {
34 | if (targetType == null) return null;
35 |
36 | var ctorInfo = targetType.GetConstructor(Type.EmptyTypes);
37 |
38 | return CreateInstance(ctorInfo, cached);
39 | }
40 |
41 | ///
42 | /// Creates an instance of type using the specified constructor parameters.
43 | ///
44 | /// Type of the target.
45 | /// if set to true [cached].
46 | /// The constructor arguments.
47 | /// System.Object.
48 | public static object CreateInstance(Type targetType, bool cached = false, params object[] parameters)
49 | {
50 | if (targetType == null) return null;
51 | if (parameters == null || !parameters.Any()) return CreateInstance(targetType, cached);
52 |
53 | var ctorInfo = targetType.GetConstructor(parameters.Select(a => a.GetType()).ToArray());
54 |
55 | return CreateInstance(ctorInfo, cached, parameters);
56 | }
57 |
58 | static object CreateInstance(ConstructorInfo ctorInfo, bool cached, object[] parameters = null)
59 | {
60 | if (ctorInfo == null) return null;
61 | var hasArguments = parameters != null && parameters.Any();
62 |
63 | Delegate ctor;
64 | lock (CacheLock)
65 | {
66 | if (!Cache.TryGetValue(ctorInfo, out ctor) || !cached)
67 | {
68 | if (hasArguments)
69 | {
70 | var ctorArgs = ctorInfo.GetParameters().Select((param, index) => Expression.Parameter(param.ParameterType, String.Format("Param{0}", index))).ToArray();
71 | // ReSharper disable once CoVariantArrayConversion
72 | ctor = Expression.Lambda(Expression.New(ctorInfo, ctorArgs), ctorArgs).Compile();
73 | }
74 | else
75 | {
76 | ctor = Expression.Lambda(Expression.New(ctorInfo)).Compile();
77 | }
78 | }
79 |
80 | if (cached) CacheCtor(ctorInfo, ctor);
81 | }
82 | return hasArguments ? ctor.DynamicInvoke(parameters) : ctor.DynamicInvoke();
83 | }
84 |
85 | static void CacheCtor(ConstructorInfo key, Delegate ctor) { if (!Cache.ContainsKey(key)) Cache.Add(key, ctor); }
86 | }
87 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Extensions/TypeChangeExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Extensions
2 | {
3 | using System;
4 | using System.ComponentModel;
5 |
6 | public static class TypeChangeExtensions
7 | {
8 | public static object Convert(this string input, Type targetType)
9 | {
10 | var converter = TypeDescriptor.GetConverter(targetType);
11 | if (converter != null)
12 | {
13 | return converter.ConvertFromString(input);
14 | }
15 | return targetType.GetDefaultValue();
16 | }
17 |
18 | public static T Convert(this string input)
19 | {
20 | return (T) Convert(input, typeof (T));
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/src/FlatFile.Core/Extensions/TypeExtensions.cs:
--------------------------------------------------------------------------------
1 | namespace FlatFile.Core.Extensions
2 | {
3 | using System;
4 | using System.Linq.Expressions;
5 |
6 | public static class TypeExtensions
7 | {
8 | public static object GetDefaultValue(this Type type)
9 | {
10 | if (type == null)
11 | {
12 | throw new ArgumentNullException("type");
13 | }
14 |
15 | // We want an Func