I'm going around in cricles changing and rechanging not understanding why I'm not getting the values I want to, maybe someone can shed some light on it.
I have this object
var result = new GetRatePlanListResponse
{
RatePlanId = prodRpsResult.Id,
RatePlanName = prodRpsResult.Name,
EffectiveStartDate = prodRpsResult.EffectiveStartDate,
EffectiveEndDate = prodRpsResult.EffectiveEndDate,
BillingPeriod = prodRpsResult.PRDP_BillingFrequency__c.HasValue ? prodRpsResult.PRDP_BillingFrequency__c.Value.ToString() : null,
ShippingSchedule = prodRpsResult.PRDP_DeliveryFrequency__c.HasValue ? prodRpsResult.PRDP_DeliveryFrequency__c.Value.ToString() : null,
UsageLevel = prodRpsResult?.PRDP_UsageLevel__c,
IncludedUnits = prodRpsResult?.PRDP_NumberofPackages__c,
BasePrice = new RatePlanBasePrice
{
Currency = pricing != null ? pricing.Currency : string.Empty,
Price = pricing != null && pricing.Price != null ? pricing.Price.Value : 0
}
};
Then I call this fucntion with that object has argument:
public void PriceConverter<T>(ref T obj)
{
Type t = obj.GetType();
foreach (var prop in t.GetProperties())
{
if (Enum.GetNames(typeof(RootPrices)).Any(x => x.ToLower() == prop.Name.ToLower()))
{
prop.SetValue(obj, ((int)(double.Parse((string)prop.GetValue(obj)) * 100)).ToString(), null);
}
}
}
My problem is that for some reason I can't access the values Currency and Price. how can I do that?
EDIT:
Ok, guys thanks for the info,, I decided to change my approach since I can't get a way to change a generic object property if they have other objects inside since they respresent a different reference. Although I'm changing it I would appreciate if someone know a way to recursivly or iteratively change a referenced object and all the object it references and can provide a solution it would help me out in the future when I'm given a task to transform multiple objects data of pre-existing code base which have similar fields.
You could just call
PriceConverter<T>
recursively on the value of eachproperty
int
. You will need to implement some sort of filtering if you don't want to callPriceConverter<T>
on properties of a specific type.For the simple example you've given, you might be able to get away with only getting readable, mutable properties. Also, it doesn't look like the
ref
keyword is necessary since you're potentially mutating properties ofobj
, but not actually changing the reference.