C# 转换对象的货币属性
我的项目具有以下结构:C# 转换对象的货币属性,c#,C#,我的项目具有以下结构: public struct Money { public CurrencyCodes Currency; public decimal Amount; } public class Foo { public Money AdultFare { get; set; } public Money ChildFare { get; set; } public Money BabyFare { get; set; } public
public struct Money
{
public CurrencyCodes Currency;
public decimal Amount;
}
public class Foo
{
public Money AdultFare { get; set; }
public Money ChildFare { get; set; }
public Money BabyFare { get; set; }
public Money AdultFee { get; set; }
public Money ChildFee { get; set; }
public Money BabyFee { get; set; }
public Money TotalFare { get; set; }
public Money TotalFee { get; set; }
}
现在我需要将所有Foo货币字段从一种货币转换为另一种货币。
什么是最佳的解决方案设计?使用反射?还有其他想法吗?代替
V1
,V2
。V10
创建一个列表:
List<Money> V = new List<Money>();
V.Add (new Money()); //repeat 10 times
编辑后的建议
List<Money> AllMoneyFields = new List<Money>();
public Foo()
{
AllMoneyFields = new List<Money>
{AdultFare,ChildFare,BabyFare,AdultFee,ChildFee,BabyFee,TotalFare,TotalFee};
}
创建一个列表,而不是
V1
,V2
…V10
:
List<Money> V = new List<Money>();
V.Add (new Money()); //repeat 10 times
编辑后的建议
List<Money> AllMoneyFields = new List<Money>();
public Foo()
{
AllMoneyFields = new List<Money>
{AdultFare,ChildFare,BabyFare,AdultFee,ChildFee,BabyFee,TotalFare,TotalFee};
}
我建议将所有这些
V
字段设置为一个数组,如下所示:
public class Foo
{
public Money[] V { get; set; } // instantiate as "new Money[10]"
}
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (Money m in V)
m = m.ConvertTo(newCurrency);
}
然后,您可以遍历V
数组并轻松地转换每个数组,如下所示:
public class Foo
{
public Money[] V { get; set; } // instantiate as "new Money[10]"
}
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (Money m in V)
m = m.ConvertTo(newCurrency);
}
或者,如果您不想创建数组,实际上可以使用反射,正如您所建议的:
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (var p in typeof(Foo).GetProperties().Where(prop => prop.PropertyType == typeof(Money)))
{
Money m = (Money)p.GetValue(this, null);
p.SetValue(this, m.ConvertTo(newCurrency), null);
}
}
编辑:您需要使用我的第二个建议,即反射,因为您的变量不是列表形式。我建议将所有
V
字段设置为一个数组,如下所示:
public class Foo
{
public Money[] V { get; set; } // instantiate as "new Money[10]"
}
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (Money m in V)
m = m.ConvertTo(newCurrency);
}
然后,您可以遍历V
数组并轻松地转换每个数组,如下所示:
public class Foo
{
public Money[] V { get; set; } // instantiate as "new Money[10]"
}
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (Money m in V)
m = m.ConvertTo(newCurrency);
}
或者,如果您不想创建数组,实际上可以使用反射,正如您所建议的:
// in class Foo
public void ConvertAllMoney(CurrencyCodes newCurrency)
{
foreach (var p in typeof(Foo).GetProperties().Where(prop => prop.PropertyType == typeof(Money)))
{
Money m = (Money)p.GetValue(this, null);
p.SetValue(this, m.ConvertTo(newCurrency), null);
}
}
编辑:您需要使用我的第二个建议,即反射,因为您的变量不是列表形式。为什么不首先使用
数组
或集合
?在设计中没有意义!请解释您的场景的更多细节。我从来没有见过这种不能通过集合或数组来解决的变量列表。好的,我来编辑这个问题。仅供参考Money
是一个问题。这样会给你自己带来很多麻烦,你确定你不想让它成为一个类吗?你为什么不首先使用数组
或集合
?在设计上没有意义!请解释您的场景的更多细节。我从来没有见过这种不能通过集合或数组来解决的变量列表。好的,我来编辑这个问题。仅供参考Money
是一个问题。这样你会给自己带来很多麻烦,你确定不想让它成为一个类吗?我已经编辑了这个问题,将字段用作数组没有多大意义。@EltonGarcia为你提供了更多想法。我已经编辑了这个问题,将字段用作数组没有多大意义。@EltonGarcia为你提供了更多想法。