2009-07-24 18 views
5

मेरे पास एक कक्षा है जिसमें दर्जन या उससे अधिक गुण हैं जो विभिन्न वित्तीय क्षेत्रों का प्रतिनिधित्व करते हैं। मेरे पास एक और वर्ग है जिसे अलग-अलग क्षेत्रों में से प्रत्येक पर कुछ गणना करने की आवश्यकता है। उन गणना विधियों के अंदर कोड उस क्षेत्र को छोड़कर समान है जो यह गणना करता है।मैं किसी विधि के पैरामीटर के रूप में कक्षा की संपत्ति कैसे पास कर सकता हूं?

क्या कोई तरीका है कि मैं एक पैरामीटर के रूप में एक संपत्ति नाम पारित कर सकता हूं और केवल एक तरीका है जो प्रत्येक संपत्ति के लिए 12 विधियों के बजाय सभी प्रदर्शन कार्य करता है?

इसके अलावा, मुझे यकीन है कि इसे प्रतिबिंब के माध्यम से पूरा किया जा सकता है, लेकिन मैंने दूसरे कोड में देखा है जहां लैम्बडा का उपयोग इसी तरह के फैशन में किया जाता है और यह सोच रहा था कि यह एक उम्मीदवार है जहां इसका उपयोग किया जा सकता है।

के रूप में अनुरोध किया है, यहाँ एक उदाहरण है:

public class FinancialInfo 
{ 
    public virtual DateTime AuditDate { get; set; } 
    public virtual decimal ReleasedFederalAmount { get; set; } 
    public virtual decimal ReleasedNonFederalAmount { get; set; } 
    public virtual decimal ReleasedStateAmount { get; set; } 
    public virtual decimal ReleasedLocalAmount { get; set; } 
    public virtual decimal ReleasedPrivateAmount { get; set; } 
    // more fields like this 
} 

public class FinancialLedger() 
{ 
    public virtual DateTime? BeginDate { get; set; } 
    public virtual DateTime? EndDate { get; set; } 
    public virtual IList<FinancialInfo> Financials { get; set; } //not actual implementation, but you get the idea 
    public decimal GetTotalReleasedFederalAmountByDate() 
    { 
     if (BeginDate == null && EndDate == null) 
      return 0; 
     decimal total = 0; 
     foreach (var fi in Financials) 
     { 
      if (someCondition) 
       if (someSubCondition) 
        total += fi.ReleasedFederalAmount; 
      else if (someOtherCondition) 
       if (someOtherSubCondition) 
        total += fi.ReleasedFederalAmount; 
      else if (anotherCondigion) 
       total += fi.ReleasedFederalAmount; 
     } 
     return total; 
    } 
    public decimal GetTotalReleasedNonFederalAmountByDate() 
    { 
     // same logic as above method, 
     // but it accesses fi.ReleasedNonFederalAmount; 
    } 
    // More methods the same as the previous, just accessing different 
    // members of FinancialInfo 
} 

मेरा लक्ष्य सिर्फ) एक विधि GetTotalAmountByDate (बुलाया बनाने के लिए और एक शुरू की तारीख में पारित करने के लिए है, और समाप्ति तिथि और संपत्ति के नाम (ReleasedFederalAmount या जारी किया गया LocalAmount, आदि) इसे उपयोग करने की जरूरत है।

मुझे उम्मीद है कि यह सटीक रूप से दर्शाया गया है कि मैं क्या करने की कोशिश कर रहा हूं।

+0

यह आप एक साझा कर सकते हैं, तो जवाब देने के लिए आसान हो जाएगा इस तरह के एक समारोह का उदाहरण, और वर्ग की संपत्तियों की तरह एक झलक दिखाई दे सकती है। –

+2

मुझे उत्सुकता है कि क्यों आपकी गणना विधि वित्तीय फ़ील्ड क्लास का उदाहरण नहीं ले सकती है, और एक मान (यानी उस संपत्ति का मूल्य जिसे आप गणना करना चाहते हैं) पैरामीटर के रूप में क्यों नहीं ले सकते हैं। क्या आप अपने प्रश्न को प्रकाशित करने के लिए कुछ स्टब कोड पोस्ट कर सकते हैं? –

+0

आपके फ़ंक्शन डिफिनेशन में उसी प्रकार का उपयोग करें, जिसे आपने कक्षा की संपत्ति में उपयोग किया था। –

उत्तर

5

आप प्रतिबिंब की जरूरत नहीं है यदि आपकी गुण सभी संख्यात्मक हैं और एक दूसरे के रूप में सजाया जा सकता है - मान लें कि decimal

कुछ इस तरह चाल करना चाहिए:

protected decimal ComputeFinancialSum(DateTime? beginDate, DateTime? endDate, 
             Func<FinancialInfo,decimal> propertyToSum) 
{ 
    if (beginDate == null && endDate == null) 
     return 0; 
    decimal total = 0; 
    foreach (var fi in Financials) 
    { 
     if (someCondition) 
      if (someSubCondition) 
       total += propertyToSum(fi); 
     else if (someOtherCondition) 
      if (someOtherSubCondition) 
       total += propertyToSum(fi); 
     else if (anotherCondigion) 
      total += propertyToSum(fi); 
    } 
    return total; 
} 

आप कर सकते हैं तो अपने विशिष्ट मामलों के सभी के लिए उचित नाम संस्करण प्रदान:

public decimal GetTotalReleasedFederalAmountByDate() 
{ 
    return ComputeFinancialSum(BeginDate, EndDate, 
           (x) => x.ReleasedFederalAmount); 
} 

public decimal GetTotalReleasedNonFederalAmountByDate() 
{ 
    return ComputeFinancialSum(BeginDate, EndDate, 
           (x) => x.ReleasedNonFederalAmount); 
} 

// other versions .... 
+0

यह वही है जो मैं खोज रहा था, धन्यवाद! –

0

जॉन स्कीट से अच्छे लैम्बा-आधारित सुझाव के अलावा, आप इस तरह कुछ कोशिश कर सकते हैं। (बेशक, यह तरीका है अपने कोड के कुछ काम करता है बदल सकता है।)

public class ValueHolder 
{ 
    object Value; 
} 

public class Main 
{ 
    private ValueHolder value1 = new ValueHolder(); 
    private ValueHolder value2 = new ValueHolder(); 

    public Value1 { get { return value1.Value; } set { value1.Value = value; } } 
    public Value2 { get { return value2.Value; } set { value2.Value = value; } } 

    public ValueHolder CalculateOne(ValueHolder holder ...) 
    { 
    // Whatever you need to calculate. 
    } 

    public CalculateBoth() 
    { 
    var answer1 = CalculateOne(value1); 
    var answer2 = CalculateOne(value2); 
    ... 
    } 
} 
0

यह शायद यहां सबसे कम तकनीक जवाब है, लेकिन क्यों नहीं सिर्फ एक स्विच का उपयोग करें और कई मर्ज "GetTotal ... राशि "काम करता है?

// define some enum for your callers to use 
public enum AmountTypeEnum { 
    ReleasedFederal = 1 
, ReleasedLocal = 2 
} 

public decimal GetTotalAmountByDate(AmountTypeEnum type) 
    { 
     if (BeginDate == null && EndDate == null) 
      return 0; 
     decimal total = 0; 
     foreach (var fi in Financials) 
     { 
      // declare a variable that will hold the amount: 
      decimal amount = 0; 

      // here's the switch: 
      switch(type) { 
       case AmountTypeEnum.ReleasedFederal: 
        amount = fi.ReleasedFederalAmount; break; 
       case AmountTypeEnum.ReleasedLocal: 
        amount = fi.ReleasedLocalAmount; break; 
       default: break; 
      } 

      // continue with your processing: 
      if (someCondition) 
       if (someSubCondition) 
        total += amount; 
      else if (someOtherCondition) 
       if (someOtherSubCondition) 
        total += amount; 
      else if (anotherCondigion) 
       total += amount; 
     } 
     return total; 
    } 

यह सुरक्षित लगता है, क्योंकि अपने सभी तर्क आपके नियंत्रण में रहता है (कोई भी आप पर अमल करने कार्यों गुजर रहा है)।

यह आगे विभाजित किया जा सकता है, तो आप वास्तव में विभिन्न मात्रा के साथ अलग अलग काम करने होंगे:

प्रसंस्करण भाग लें और इसे एक समारोह में बदल जाते हैं:

 private decimal ProcessNormal(decimal amount) { 
      decimal total = 0; 

      // continue with your processing: 
      if (someCondition) 
       if (someSubCondition) 
        total += amount; 
      else if (someOtherCondition) 
       if (someOtherSubCondition) 
        total += amount; 
      else if (anotherCondition) 
       total += amount; 
      return total; 
    } 

public decimal GetTotalAmountByDate(AmountTypeEnum type) 
    { 
     if (BeginDate == null && EndDate == null) 
      return 0; 
     decimal total = 0; 
     foreach (var fi in Financials) 
     { 
      // declare a variable that will hold the amount: 
      decimal amount = 0; 

      // here's the switch: 
      switch(type) { 
       case AmountTypeEnum.ReleasedFederal: 
        amount = fi.ReleasedFederalAmount; 
        total = ProcessNormal(amount); 
        break; 
       case AmountTypeEnum.ReleasedLocal: 
        amount = fi.ReleasedLocalAmount; 
        total = ProcessNormal(amount); 
        break; 
       case AmountTypeEnum.NonReleasedOtherAmount: 
        amount = fi.NonReleasedOtherAmount; 
        total = ProcessSlightlyDifferently(amount); // for argument's sake 
        break; 
       default: break; 
      } 
     } 
     return total; 
    } 
संबंधित मुद्दे

 संबंधित मुद्दे