2012-03-16 45 views
0

所以我正在写一个C#应用程序,使用.net/c#4.0 我有一个方法,它采用自定义类型和字典。 我重复使用它的各种各样的东西,但由于某种原因,我想不出一种方法来封装逻辑。问题是这条线寻找一种方法来重用我的功能,在一个小的方式变化

if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyA) > 
_Rule.Value) 

在另一个使用它可能是

if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyB) > 
_Rule.Value) 

,在各种情况变化是我使用来比较规则值属性的唯一的事。出于某种原因,我想不出一种重用它的方式,因为我没有传入某个函数的值,因为值是在函数中派生的。我怎样才能写出一个函数来抽象出它需要知道它需要导出哪些值并传递这些信息,即传递它需要检查的属性而不是该属性的值。

int a; 
    for (int z= 0;z<=2;z++) 
    { 
    a = (z * z) * 24; 
    for (; (a%24) <= _Rule.AlertEndTime; a++) 
    { 
     if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyA) > 
_Rule.Value) 
     { 

      EnqueueRuleTrigger(_Rule); 
      break; 
     } 
    } 
    } 

我一直重写这个方法直列无论我需要它与真正的财产....这显然是很浪费的,需要在许多地方进行任何改变。 在此先感谢

+0

如果我不明白,我甚至不具备OB在循环之外的属性的对象引用。我想封装一些包含循环逻辑[不会改变]的东西以及向方法提供定制属性的能力。事情如果我有4个条件是完全相同的,除了他们评估的属性,所以我刚刚复制并粘贴了整个代码并更改了属性引用。更糟糕的是,每个4有3个变种,其中一个在哪里使用a>,a <和an =所以我真的有这个方法稍微修改了12次。 – Jordan 2012-03-16 08:08:57

回答

0

让你的函数采取一个lambda参数,并把它传递_ => _.PropertyA_ => _.PropertyB等:

void CheckAndEnqueueRulesByProperty (Func<YourObject, string> propertyGetter) 
{ 
    ... 
    if (FastIntParse.FastParse (propertyGetter (
      _dict[_Rule.Key].hourly_data[a])) > _Rule.Value) 
    { 
     ... 
    } 
    ... 
} 

如果你有很多类型的对象的检查与相同的逻辑,使该功能一般。

+0

我不能访问_,直到我在循环中,因为PropertyA和PropertyB是数组中对象的属性[在字典中]谁是索引,直到我在循环中才知道。整个事情是我一直粘贴在一起的“功能”。我想创建一个函数,我可以基本上告诉它使用一个属性,但我还没有引用它。该属性引用必须从调用者传入。如果你的回答涵盖了这个遗憾,我并不完全明白,因为我对兰姆达斯有点新鲜...... – Jordan 2012-03-16 07:39:54

1

您可以使用表达式,然后在方法中拔出财产,然后利用反射的方法

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<PropertyBag> bags = new List<PropertyBag>() 
            { 
             new PropertyBag() {Property1 = 1, Property2 = 2}, 
             new PropertyBag() {Property1 = 3, Property2 = 4} 
            }; 

     Runme(x => x.Property1, bags); 
     Runme(x => x.Property2, bags); 

     Console.ReadLine(); 


    } 

    public static void Runme(Expression<Func<PropertyBag, int>> expression, List<PropertyBag> bags) 
    { 
     var memberExpression = expression.Body as MemberExpression; 
     var prop = memberExpression.Member as PropertyInfo; 

     bags.ForEach(bag => 
       Console.WriteLine(prop.GetValue(bag, null)) 
      ); 
    } 
} 


public class PropertyBag 
{ 
    public int Property1 { get; set; } 
    public int Property2 { get; set; } 
} 

}内配合这个对象最多

1

解决访问问题不同的性质,并与使用不同的布尔函数(<,>,==),你可以使用委托像这样:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace ConsoleApplication1 
{ 
    delegate bool CompareFunction(Fii test, Foo item); 

    class Program 
    { 
     static List<Foo> list = new List<Foo>() { 
      new Foo() { PropertyA = 0, PropertyB = 9 }, 
      new Foo() { PropertyA = 1, PropertyB = 10 } 
     }; 
     static Fii test = new Fii() { PropertyA = 1 }; 

     static void Main(string[] args) 
     { 
      Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyA < item1.PropertyA; }); 
      Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyB > item1.PropertyA; }); 
      Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyA == item1.PropertyA; }); 
      Console.ReadLine(); 
     } 

     static void Bar(List<Foo> list, CompareFunction cmp) 
     { 
      foreach (Foo item in list) 
       if (cmp(test, item)) 
        Console.WriteLine("true"); 
       else 
        Console.WriteLine("false"); 
     } 
    } 

    class Foo 
    { 
     public int PropertyA { get; set; } 
     public int PropertyB { get; set; } 
    } 

    class Fii 
    { 
     public int PropertyA { get; set; } 
    } 
} 
相关问题