CloudyWing.Constants 1.2.0

dotnet add package CloudyWing.Constants --version 1.2.0
NuGet\Install-Package CloudyWing.Constants -Version 1.2.0
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="CloudyWing.Constants" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CloudyWing.Constants --version 1.2.0
#r "nuget: CloudyWing.Constants, 1.2.0"
#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 CloudyWing.Constants as a Cake Addin
#addin nuget:?package=CloudyWing.Constants&version=1.2.0

// Install CloudyWing.Constants as a Cake Tool
#tool nuget:?package=CloudyWing.Constants&version=1.2.0

Constants

常數物件,將常數物件重複的行為整理成Constant<T>。

Supported Frameworks

  • .NET Standard 2.1
  • .NET Standard 2.0
  • .NET Framework 4.5

Examples

/// int 可替換任何implement IConvertible的type
[Serializable]
public sealed class WeekDay : Constant<int> {
    private readonly static IList<WeekDay> items;
    public static readonly WeekDay Sunday;
    public static readonly WeekDay Monday;
    public static readonly WeekDay Tuesday;
    public static readonly WeekDay Wednesday;
    public static readonly WeekDay Thursday;
    public static readonly WeekDay Friday;
    public static readonly WeekDay Saturday;

    // 建立一個static constructor來初始化各個物件
    static WeekDay() {
        // 前兩個參數固定為Value和Text,後面可以自行擴充
        Sunday = new WeekDay(0, "星期日", nameof(Sunday));
        Monday = new WeekDay(1, "星期一", nameof(Monday));
        Tuesday = new WeekDay(2, "星期二", nameof(Tuesday));
        Wednesday = new WeekDay(3, "星期三", nameof(Wednesday));
        Thursday = new WeekDay(4, "星期四", nameof(Thursday));
        Friday = new WeekDay(5, "星期五", nameof(Friday));
        Saturday = new WeekDay(6, "星期六", nameof(Saturday));

        items = new List<WeekDay>() {
            Sunday,
            Monday,
            Tuesday,
            Wednesday,
            Thursday,
            Friday,
            Saturday
        };
    }

    // constructor設定成private
    private WeekDay(int value, string text, string name) : base(value, text) {
        Name = name;
    }

    // 定義int強制轉換成WeekDay的operator
    public static explicit operator WeekDay(int value) {
        return items.Where(x => x.Value == value).SingleOrDefault() ??
            throw new InvalidCastException(); // 視情況決定要throw InvalidCastException還是返回初始值
    }

    // 額外擴充的Property
    public string Name { get; }

    // 不要直接回傳IList<WeekDay>,避免被人異動裡面內容
    public static IReadOnlyList<WeekDay> GetItems() => new ReadOnlyCollection<WeekDay>(items);
}

完整範例請參考 Constants.Examples

License

This project is MIT licensed.

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 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • 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.2.0 600 1/23/2020
1.1.0 477 1/21/2020
1.0.0 460 1/20/2020