IVSoftware.Portable.Csv.SingleInstanceSerializer
1.0.3-rc
Prefix Reserved
This is a prerelease version of IVSoftware.Portable.Csv.SingleInstanceSerializer.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package IVSoftware.Portable.Csv.SingleInstanceSerializer --version 1.0.3-rc
NuGet\Install-Package IVSoftware.Portable.Csv.SingleInstanceSerializer -Version 1.0.3-rc
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="IVSoftware.Portable.Csv.SingleInstanceSerializer" Version="1.0.3-rc" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IVSoftware.Portable.Csv.SingleInstanceSerializer --version 1.0.3-rc
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: IVSoftware.Portable.Csv.SingleInstanceSerializer, 1.0.3-rc"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install IVSoftware.Portable.Csv.SingleInstanceSerializer as a Cake Addin #addin nuget:?package=IVSoftware.Portable.Csv.SingleInstanceSerializer&version=1.0.3-rc&prerelease // Install IVSoftware.Portable.Csv.SingleInstanceSerializer as a Cake Tool #tool nuget:?package=IVSoftware.Portable.Csv.SingleInstanceSerializer&version=1.0.3-rc&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
IVSoftware.Portable.Csv.SingleInstanceSerializer
These extensions reflect the properties of a given class T. The basic capabilities are:
- Generate a comma-delimited header string, based on the property names of class T.
- Given an instance of class T, generate a comma-delimited string containing values corresponding to the order and count of the generated header.
- Given a comma-delimited string containing values corresponding to the order and count of the generated header, generate a class instance.
The [CsvIgnore]
attribute makes a property invisible to header and values generators.
Version 1.0.3 adds advanced capability:
- Extract an instance from a CSV file where the header and values do not strictly follow the format of the generated header. In this case, a class can be designed with properties that correspond to the values of interest in the file. Other values in other columns will be safely ignored. In general, this would be used to compose a Master Record that cherry picks values from multiple csv files.
The [HeaderText]
attribute makes a property mappable to header names in the file that are different from the property names in the class.
The type conversion has been greatly expanded, and now supports almost any System type that has a static Parse
method.
Methods
/// <summary>
/// Enumerates the public R/W property names of an instance of @type as comma-delimited string.
/// </summary>
/// <returns>Comma delimited list of names.</returns>
public static string GetCsvHeader(this Type @type);
/// <summary>
/// Enumerates the public R/W property names of an instance of @type as array.
/// </summary>
/// <returns>String array of names.</returns>
public static string[] GetCsvHeaderArray(this Type @type);
/// <summary>
/// Deserializes an instance of @type from comma delimited string
/// based on names obtained from GetCsvHeader().
/// </summary>
/// <returns>Instance of type T</returns>
public static T FromCsvLine<T>(this Type @type, string csvLine);
/// <summary>
/// Deserializes an instance of @type from comma delimited string
/// based on names obtained from GetCsvHeader().
/// </summary>
/// <returns>Comma delimited enumuration of public R/W property values.</returns>
public static string ToCsvLine<T>(this T instance);
/// <summary>
/// Fuzzy deserialization of an instance of @type from comma delimited string, meaning
/// that it's tolerant of names in header that aren't directly mapped to the object.
/// </summary>
/// <returns>Instance of type T</returns>
public static T Extract<T>(this Type @type, string unqualifiedHeader, string csvLine, bool ignoreCase = false);
Attribute
/// <summary>
/// Opt-out this property from Header and Instance serialization.
/// </summary>
[CsvIgnore]
Usage
public MainForm()
{
InitializeComponent();
listBox.DataSource = Persons;
listBox.DisplayMember = "Name";
buttonToCsv.Click += (sender, e) =>
{
if (Persons.Any() || warnEmpty())
{
List<string> builder = new List<string>
{
typeof(Person).GetCsvHeader(),
};
foreach (var person in Persons)
{
builder.Add(person.ToCsvLine());
}
Directory.CreateDirectory(Path.GetDirectoryName(_filePathCsv));
File.WriteAllLines(_filePathCsv, builder.ToArray());
Process.Start("notepad.exe", _filePathCsv);
}
};
buttonFromCsv.Click += async (sender, e) =>
{
UseWaitCursor = true;
await localClearAndWait();
if (File.Exists(_filePathCsv))
{
string[] lines = File.ReadAllLines(_filePathCsv);
if (lines.FirstOrDefault() is string header)
{
for (int i = 1; i < lines.Length; i++)
{
var line = lines[i];
if (!string.IsNullOrEmpty(line))
{
Persons.Add(typeof(Person).FromCsvLine<Person>(line));
}
}
}
else
{
Debug.Assert(false, "Expecting header");
}
}
UseWaitCursor = false;
};
async Task localClearAndWait()
{
if(Persons.Any())
{
Persons.Clear();
await Task.Delay(1000); // Observe the cleared list
}
}
}
bool warnEmpty()
{
return !DialogResult.Cancel.Equals(
MessageBox.Show(
"Person list is empty. Save anyway?",
"Confirm",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question
));
}
BindingList<Person> Persons { get; } = new BindingList<Person>();
StackOverflow
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.