亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

豐富枚舉 - 可能嗎?

豐富枚舉 - 可能嗎?

C#
大話西游666 2023-12-17 19:59:37
我正在尋找一種解決方案來將不可變數據存儲在我的代碼而不是數據庫中。在具體情況下,我想處理單位。這是重量單位的示例(它們不會改變,所以可以將它們存儲在我的代碼中):public class Unit    {        public Unit() { }        public Unit(string name, string symbol, double factor, Unit baseUnit, UnitType unitType)        {            this.Name = name;            this.Symbol = symbol;            this.Factor = factor;            this.BaseUnit = baseUnit;            this.UnitType = unitType;        }        public int Id { get; set; }        public string Name { get; set; }        public UnitType UnitType { get; set; }        public string Symbol { get; set; }        public string NamePlural { get; set; }        public Unit BaseUnit { get; set; }        public double Factor { get; set; }    }    public static class TimeUnits    {        public static Unit second = new Unit("second", "s", 1, null, UnitTypes.Time);        public static Unit microsecond = new Unit("microsecond", "μs", 0.000001, second, UnitTypes.Time);        public static Unit millisecond = new Unit("millisecond", "ms", 0.001, second, UnitTypes.Time);        public static Unit minute = new Unit("minute", "min", 60.0, second, UnitTypes.Time);        public static Unit hour = new Unit("hour", "h", 3600.0, second, UnitTypes.Time);        public static Unit day = new Unit("day", "d", 24.0, hour, UnitTypes.Time);        public static Unit week = new Unit("week", "w", 7, day, UnitTypes.Time);    }如前所述,我不想將這些不可變信息存儲在數據庫中,以避免 ef-core 在從數據庫檢索數據時執行額外的聯合。對于性別,我只需使用枚舉: public enum Gender{    male = 1,    female = 2,    not_applicable = 9,    dont_want_to_share = 10}我想為單位提供類似的解決方案。但枚舉只有一個 ID 和一個名稱。對于上面顯示的單位或其他情況,我需要額外的屬性(例如,factore、unitType 等)。非常感謝您提供有關如何實現此目標的任何提示,以便 ef core 像使用枚舉一樣加載這些值。
查看完整描述

2 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

屬性是一種方法,但如果您想避免使用反射,您可以實現一個包含您需要的所有內容的類,類似于:


public enum UnitType

{

    Second,

    Microsecond,

    Millisecond,

    Minute,

    Hour,

    Day,

    Week

}


public class Unit

{

    public string Name { get; private set; }


    public string Symbol { get; private set; }


    public double Factor { get; private set; }


    public Unit Base { get; private set; }


    public Unit(UnitType unit, bool isBase = false)

    {

        Name = GetUnitName(unit);


        Symbol = GetUnitSymbol(unit);


        Factor = GetUnitFactor(unit);


        if (!isBase)

            Base = GetUnitBase(unit);

    }


    private string GetUnitName(UnitType unit)

    {

        switch (unit)

        {

            case UnitType.Second:

                return "second";

            case UnitType.Microsecond:

                return "microsecond";

            case UnitType.Millisecond:

                return "millisecond";

            case UnitType.Minute:

                return "minute";

            case UnitType.Hour:

                return "hour";

            case UnitType.Day:

                return "day";

            case UnitType.Week:

                return "week";

            default:

                return null;

        }

    }


    private string GetUnitSymbol(UnitType unit)

    {

        switch (unit)

        {

            case UnitType.Second:

                return "s";

            case UnitType.Microsecond:

                return "μs";

            case UnitType.Millisecond:

                return "ms";

            case UnitType.Minute:

                return "min";

            case UnitType.Hour:

                return "h";

            case UnitType.Day:

                return "d";

            case UnitType.Week:

                return "w";

            default:

                return null;

        }

    }


    private double GetUnitFactor(UnitType unit)

    {

        switch (unit)

        {

            case UnitType.Second:

                return 1;

            case UnitType.Microsecond:

                return 0.000001;

            case UnitType.Millisecond:

                return 0.001;

            case UnitType.Minute:

                return 60.0;

            case UnitType.Hour:

                return 3600.0;

            case UnitType.Day:

                return 24.0;

            case UnitType.Week:

                return 7;

            default:

                return 0;

        }

    }


    private Unit GetUnitBase(UnitType unit)

    {

        switch (unit)

        {

            case UnitType.Microsecond:

                return new Unit(UnitType.Second, true);

            case UnitType.Millisecond:

                return new Unit(UnitType.Second, true);

            case UnitType.Minute:

                return new Unit(UnitType.Second, true);

            case UnitType.Hour:

                return new Unit(UnitType.Minute, true);

            case UnitType.Day:

                return new Unit(UnitType.Hour, true);

            case UnitType.Week:

                return new Unit(UnitType.Day, true);

            default:

                return null;

        }

    }



}

用法 :


// initiate a new Unit instance. 

var unit = new Unit(UnitType.Week);


// Get values

var name = unit.Name;

var symbol = unit.Symbol;

var factor = unit.Factor;


// In case if some units doesn't have base

if (unit.Base != null)

{

    var baseName = unit.Base.Name;

    var baseSymbol = unit.Base.Symbol;

    var baseFactor = unit.Base.Factor;


}

這只是一個簡單的示例,尚未經過充分測試,只是向您展示另一種可以實現的方法。


您還可以使用隱式運算符來獲取值 例子 :


public class Unit

{

  ......


    public static implicit operator double(Unit v) => v.Factor;


    public static implicit operator string(Unit v) => v.Symbol;


}

隱式獲取值:


var symbol = (string) unit; // will return the v.Symbol

var factor = (double) unit; // will return the v.Factor

此外,我沒有展示UnitTypes.Time,因為沒有太多關于它的代碼,但我認為示例和想法足以為您的想法提供所需的推動力。


查看完整回答
反對 回復 2023-12-17
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

我不確定你到底想做什么,但我認為屬性將是一個不錯的選擇。您可以像這樣在每個單元枚舉字段上設置屬性,并在需要時通過反射檢索它們。


    [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]

sealed class UnitTypeAttribute : Attribute

{

    public UnitType UnitType{ get; set; }

    public UnitAttribute(UnitType unitT)

    {

        UnitType= unitT;

    }

}


    enum Unit

{

    [UnitType(UnitTypes.Time)]

    Second,


    [UnitType(UnitTypes.Time)]

    MicroSecond,


    [UnitType(UnitTypes.Time)]

    Hour

}

然后,當你想檢索它時,使用此方法(可以通用)


       public static UnitType GetUnitTypeAttribute(Unit unit)

    {

        var memberInfo = typeof(Unit).GetMember(unit.ToString());

        var result = memberInfo[0].GetCustomAttributes<UnitTypeAttribute>(false)


        return ((UnitType)result).UnitType;

    }


查看完整回答
反對 回復 2023-12-17
  • 2 回答
  • 0 關注
  • 261 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號