【问题标题】:Access object properties by dynamically generating their names from somewhere else(enum)通过从其他地方动态生成对象属性(枚举)来访问对象属性
【发布时间】:2023-03-09 19:01:01
【问题描述】:

我正在尝试对我的代码进行一些优化。我有一个enum

enum Months { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }

以及下面带有现成类对象的代码:

oi.Jan = Math.Round(Convert.ToDecimal(adjAllocation[0]), 2).ToString();
oi.Feb = Math.Round(Convert.ToDecimal(adjAllocation[1]), 2).ToString();
oi.Mar = Math.Round(Convert.ToDecimal(adjAllocation[2]), 2).ToString();
oi.Apr = Math.Round(Convert.ToDecimal(adjAllocation[3]), 2).ToString();
oi.May = Math.Round(Convert.ToDecimal(adjAllocation[4]), 2).ToString();
oi.Jun = Math.Round(Convert.ToDecimal(adjAllocation[5]), 2).ToString();
oi.Jul = Math.Round(Convert.ToDecimal(adjAllocation[6]), 2).ToString();
oi.Aug = Math.Round(Convert.ToDecimal(adjAllocation[7]), 2).ToString();
oi.Sep = Math.Round(Convert.ToDecimal(adjAllocation[8]), 2).ToString();
oi.Oct = Math.Round(Convert.ToDecimal(adjAllocation[9]), 2).ToString();
oi.Nov = Math.Round(Convert.ToDecimal(adjAllocation[10]), 2).ToString();
oi.Dec = Math.Round(Convert.ToDecimal(adjAllocation[11]), 2).ToString();

我正在尝试做这样的事情(伪代码):

oi.[Months[i]] = Math.Round(Convert.ToDecimal(adjAllocation[0]), 2).ToString();

我不能只将一个数组放在我从中创建对象的类中。我需要属性是我正在调用的现成类的字符串。

【问题讨论】:

    标签: c# class properties enums


    【解决方案1】:

    这不是优化,而是简化的尝试。反射可以做到这一点,但似乎根本没有简化:

    public enum Months {
        Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
    }
    
    public partial class SomeClass {
        public String Jan { get; set; }
        public String Feb { get; set; }
        public String Mar { get; set; }
        public String Apr { get; set; }
        public String May { get; set; }
        public String Jun { get; set; }
        public String Jul { get; set; }
        public String Aug { get; set; }
        public String Sep { get; set; }
        public String Oct { get; set; }
        public String Nov { get; set; }
        public String Dec { get; set; }
    
        public SomeClass(IConvertible[] array) {
            var count=(
                from Months month in Enum.GetValues(typeof(Months))
                let index=(int)month
                where index<array.Length
                select
                    typeof(SomeClass).InvokeMember(
                        Enum.GetName(typeof(Months), month),
                        BindingFlags.SetProperty|BindingFlags.Instance|BindingFlags.Public,
                        default(Binder), this,
                        new object[] { Math.Round(array[index].ToDecimal(null), 2).ToString() }
                        )
                ).Count();
        }
    }
    

    如果数组adjAllocation 可以转换为十进制,那么它将是IConvertible 的数组。所以把它传给SomeClass的构造函数,就大功告成了。

    【讨论】:

    • 你会如何自己简化它?
    • @AngelicCore:对不起,我不明白你的评论......你能再描述一下你的意思吗?
    • 对不起,如果你是我,你会如何简化上面的代码?就我个人而言,我希望使用数组索引器或类似的东西,而不是每个月写一行。问题是接收此类对象的函数无法读取数组。
    【解决方案2】:

    System.Enum-class 提供辅助方法来支持使用enums。
    例如,有Enum.GetName-方法来访问特定值。来自MSDN

    using System;
    
    public class GetNameTest {
        enum Colors { Red, Green, Blue, Yellow };
        enum Styles { Plaid, Striped, Tartan, Corduroy };
    
        public static void Main() {
    
            Console.WriteLine("The 4th value of the Colors Enum is {0}", Enum.GetName(typeof(Colors), 3));
        Console.WriteLine("The 4th value of the Styles Enum is {0}", Enum.Get.   Name(typeof(Styles), 3));
        }
    }
    // The example displays the following output: 
    //       The 4th value of the Colors Enum is Yellow 
    //       The 4th value of the Styles Enum is Corduroy
    

    【讨论】:

    • 我可以使用该名称从 obj 访问具有相同名称的属性吗?如上:A.Jan A.Feb A.Mar 我可以动态地给出属性名称吗?
    • 检查System.Enum中的其他方法,不过你不能像数组一样使用枚举。
    • GetName-方法返回一个string,所以如果你可以在你想要的位置使用string,那么可以。
    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2011-08-10
    • 2016-05-03
    • 1970-01-01
    相关资源
    最近更新 更多