c# - How can I pass a property of a class as a parameter of a method? -
I have a class in which there are dozens or more properties that represent different financial areas. I have another class which needs to be calculated separately on each of those areas. The code inside those calculation methods is the same, which calculates except the field.
Is there any way that I can pass a property name as a parameter and there is a method that works for all the properties instead of 12 methods for each property?
Besides, I'm sure it can be done through reflection, but I have seen in the second code where Lambda is used in this kind of fashion and wondering if This is a candidate where it can be used.
According to the request, here is an example:
public class financial information {public virtual date time audit date {get; Set; } Public Virtual Decimal ReleasedFederalAmount {get; Set; } Public Virtual Decimal ReleasedNonFederalAmount {get; Set; } Releasing public virtual decimalsStateMount {Received; Set; } Public Virtual Decimal ReleasedLocalAmount {get; Set; } Public Virtual Decimal ReleasedPrivateAmount {get; Set; } // more fields like this} Public Category Financial Lane () {Public Virtual Date Time? Get Started {Get; Set; } Public Virtual Date Time? End date {Received; Set; } Public Virtual ILITIT & lt; FinancialInfo & gt; Financial (Receive; Set;} // Not Actual Implementation, But You Get This Decision Public Decline GetTotalReleasedFederalAmountByDate () {If (start == blank and end date == empty) 0; decimal total = 0; foreign Currency (in financial cases) (if (some rules) if (some embarrassment) total + = fi .related ferrelem; and if (some other condition) if (some other social condition) total + = fi. Related fedralum; and if ( Other Conditions) Total + = Relayed Fidelum;} Total Return;} Public Decimal GetTotalReleatedNonFederalAmountByDate () {// The same argument as above method, // but it access.releasedNonFederalAmount;} // More methods just like the previous, just financial information} Accessing different // members
My goal is to create a method called GetTotalAmountByDate () just by one method and pass it on the initial date, and the end date and the name of the property (ReleasedFederalAmount Or ReleasedLocalAmount, etc.) to access it Ykta is.
I am hoping that it tried correctly what I've shown. You do not need a reflection if your property is all numeric and is equally a single type of form. Can be treated in - assuming a decimals
.
Something like this should do the trick:
Secure decimal computerization (datetime? Start, date time? Expiration date, fx & lt; financial information, decimals & Gt; PropertiesTosom) {If (beginDate == faucet & amp; expiry date == zero) returned 0; Decimal total = 0; Foreign currency (in financial cases) (if (some rules) if (some in-laws) total + = chromosome (fi); and if (some other condition) if (some author condition) total = = chromosome (fi); Other Kandison) Total + = Chromosome (Fi);} Total Return;}
Then you can provide the proper named version for all your specific cases:
Public Decimal GetTotalReleasedFederalAmountByDate () {Return ComputeFinancialSum (Start, End Date, (X) = & gt; x.ReleasedFederalAmount);} Generic Decimal GetTotalReleatedNonFederalAmountByDate () {Return ComputeFinancialSum (Beginners, End Date, (x) = & gt; x.ReleasedNonFederalAmount);} // Other Versions ....
Comments
Post a Comment