C# 通过从其他位置(枚举)动态生成对象属性的名称来访问对象属性
我正在尝试对我的代码进行一些优化。我有一个C# 通过从其他位置(枚举)动态生成对象属性的名称来访问对象属性,c#,class,properties,enums,C#,Class,Properties,Enums,我正在尝试对我的代码进行一些优化。我有一个枚举: 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]),
枚举
:
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();
我正在尝试这样做(伪代码):
我不能把数组放在我制作对象的类中。我需要属性是我正在调用的现成类的字符串 有
System.Enum
-类,它提供辅助方法来支持使用Enum
s.例如,有
Enum.GetName
-方法来访问特定值。发件人:
这不是优化,而是简化的尝试。反射可以做到这一点,但似乎一点也不简化:
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();
}
}
公共枚举月份{
一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月
}
公共部分类{
公共字符串Jan{get;set;}
公共字符串Feb{get;set;}
公共字符串Mar{get;set;}
公共字符串Apr{get;set;}
公共字符串可以{get;set;}
公共字符串Jun{get;set;}
公共字符串Jul{get;set;}
公共字符串Aug{get;set;}
公共字符串Sep{get;set;}
公共字符串Oct{get;set;}
公共字符串Nov{get;set;}
公共字符串Dec{get;set;}
公共SomeClass(IConvertible[]数组){
var计数=(
从Enum.GetValues中的月份(typeof(月))
让索引=(int)月
如果indexCan我可以使用名称从obj访问具有相同名称的属性?如上所述:a.Jan a.Feb a.Mar我可以动态提供属性名称吗?请检查System.Enum
中的其他方法,但是不能像数组一样使用Enum。GetName
-方法返回字符串,因此如果可以使用字符串
在你想要的位置,那么是的。你会如何简化它自己?@AngelicCore:对不起,我不明白你的评论。你能再描述一下你的意思吗?对不起,我的意思是,如果你是我,你会如何简化上面的代码?我个人希望使用数组索引器或类似的工具问题是接收此类对象的函数不能读取数组。
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
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();
}
}