IVSoftware.Portable.Csv.SingleInstanceSerializer 1.0.2

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package IVSoftware.Portable.Csv.SingleInstanceSerializer --version 1.0.2                
NuGet\Install-Package IVSoftware.Portable.Csv.SingleInstanceSerializer -Version 1.0.2                
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.2" />                
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.2                
#r "nuget: IVSoftware.Portable.Csv.SingleInstanceSerializer, 1.0.2"                
#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.2

// Install IVSoftware.Portable.Csv.SingleInstanceSerializer as a Cake Tool
#tool nuget:?package=IVSoftware.Portable.Csv.SingleInstanceSerializer&version=1.0.2                

IVSoftware.Portable.Csv.SingleInstanceSerializer

Methods
/// <summary>
/// Enumerates the public R/W property names of an instance of @type.
/// </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.
/// </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);
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

Populate a ListBox with CSV values from a custom class

Product 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.

Version Downloads Last updated
1.0.3 211 12/23/2023
1.0.3-rc 118 12/21/2023
1.0.2 173 11/22/2023
1.0.1 139 11/22/2023