Dav*_*ave 3 c# linq anonymous-types anonymous-class
我是匿名课程的新手,今天我想我遇到了第一个案例,我觉得我真的可以使用它们.我正在编写一个方法,可以将临时数据存储在类中,并且因为该类在该方法之外没有任何意义,所以使用匿名类确实对我有意义(至少在它做的时候) ).
在开始编码之后,我肯定会做出一些让步.我喜欢将计算之类的内容分配给临时变量,以便在调试期间我可以在逻辑块中一次验证一些计算.然后我想为最终值分配更简单的东西.该值将在匿名类中.
问题是,为了简明地实现我的匿名类代码,我想使用LINQ.这里的问题是我认为你不能在声明中做这样的临时计算. 或者你呢?
这是我想要做的一个人为的例子:
namespace AnonymousClassTest
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
ObservableCollection<RectanglePoints> Points { get; set; }
public class RectanglePoints
{
public Point UL { get; set; }
public Point UR { get; set; }
public Point LL { get; set; }
public Point LR { get; set; }
}
public class DontWantThis
{
public double Width { get; set; }
public double Height { get; set; }
}
private Dictionary<string,string> properties = new Dictionary<string,string>();
private Dictionary<string,double> scaling_factors = new Dictionary<string,double>();
private void Sample()
{
// not possible to do temp variables, so need to have
// longer, more unreadable assignments
var widths_and_heights = from rp in Points
select new
{
Width = (rp.UR.X - rp.UL.X) * scaling_factors[properties["dummy"]],
Height = (rp.LL.Y - rp.UL.Y) * scaling_factors[properties["yummy"]]
};
// or do it in a for loop -- but then you have to use a concrete
// class to deal with the Width and Height storage
List<DontWantThis> other_widths_and_heights = new List<DontWantThis>();
foreach( RectanglePoints rp in Points) {
double base_width = rp.UR.X - rp.UL.X;
double width_scaling_factor = scaling_factors[properties["dummy"]];
double base_height = rp.LL.Y - rp.UL.Y;
double height_scaling_factor = scaling_factors[properties["yummy"]];
other_widths_and_heights.Add( new DontWantThis
{
Width=base_width * width_scaling_factor,
Height=base_height * height_scaling_factor
});
}
// now we want to use the anonymous class, or concrete class, in the same function
foreach( var wah in widths_and_heights)
Console.WriteLine( String.Format( "{0} {1}", wah.Width, wah.Height));
foreach( DontWantThis dwt in other_widths_and_heights)
Console.WriteLine( String.Format( "{0} {1}", dwt.Width, dwt.Height));
}
public Window1()
{
InitializeComponent();
Points = new ObservableCollection<RectanglePoints>();
Random rand = new Random();
for( int i=0; i<10; i++) {
Points.Add( new RectanglePoints { UL=new Point { X=rand.Next(), Y=rand.Next() },
UR=new Point { X=rand.Next(), Y=rand.Next() },
LL=new Point { X=rand.Next(), Y=rand.Next() },
LR=new Point { X=rand.Next(), Y=rand.Next() }
} );
}
Sample();
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:除非您实际将键添加到词典中,否则不要尝试运行它 :)
在LINQ中创建匿名类非常棒,但迫使我在一行中进行计算.想象一下,计算方法比我所展示的要长.但它类似于我将进行一些字典查找以获取特定值.调试可能很痛苦.
具体类的使用解决了使用临时变量的问题,但是我不能简洁地完成所有事情.是的,我意识到我在说我正在寻求简洁,同时要求能够在我的LINQ语句中保存临时变量时,我有点矛盾.
在循环使用Points时,我开始尝试创建一个匿名类,但很快意识到我无法存储它!您不能使用List,因为它只会丢失该类的整个匿名性.
任何人都可以建议一种方法来实现我正在寻找的东西吗?还是一些中间地带?我在StackOverflow上已经阅读了一些其他问题,但它们都没有和我的完全相同.
假设我理解正确,问题是你必须在一个表达式中设置所有属性.这绝对是匿名类型的情况.
但是,您不必在该表达式中全部内联.我建议如果你的属性是基于复杂的表达式,你可以将这些表达式分解为辅助方法:
var complex = new {
First = ComputeFirstValue(x, y),
Second = ComputeSecondValue(a, b)
...
};
Run Code Online (Sandbox Code Playgroud)
如果您是白盒测试的粉丝(我),这还有额外的潜在好处,您可以单独测试每个辅助方法.
这不会避免存在一个大的匿名类型初始化表达式,但这意味着工作将被分解.
| 归档时间: |
|
| 查看次数: |
3638 次 |
| 最近记录: |