1

コードを最適化しようとしています。私は持っています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();

オブジェクトを作成しているクラスに配列を入れることはできません。私が呼び出している既製のクラスの文字列であるプロパティが必要です。

4

2 に答える 2

1

System.Enums の操作をサポートする補助メソッドを提供する -classがありますenum
たとえばEnum.GetName、特定の値にアクセスするための -method があります。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
于 2013-04-28T09:30:07.740 に答える
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を 10 進数に変換できる場合は、 の配列になりますIConvertible。のコンストラクタに渡せSomeClassば完了です。

于 2013-04-28T10:52:41.813 に答える