鉴于可变结构通常被认为是邪恶的(例如,为什么可变结构"邪恶"?),是否有可能促使.NET框架的设计者制作System.Windows.Point
和System.Windows.Vector
变异的潜在好处?
我想理解这一点,所以我可以决定是否有必要使我自己的类似结构变得可变(如果有的话).做出决定Point
和Vector
变异可能只是判断错误,但如果有充分理由(例如,表现好处),我想了解它是什么.
我知道我偶然发现了Vector.Normalize()
几次该方法的实现,因为它,惊喜(!),并没有带来新鲜感Vector
.它只是改变了当前的向量.
我一直认为它应该像这样工作:
var vector = new Vector(7, 11);
var normalizedVector = vector.Normalize(); // Bzzz! Won't compile
Run Code Online (Sandbox Code Playgroud)
但它实际上是这样的:
var vector = new Vector(7, 11);
vector.Normalize(); // This compiles, but now I've overwritten my original vector
Run Code Online (Sandbox Code Playgroud)
......所以,似乎不变性只是为了避免混淆是一个好主意,但同样,也许在某些情况下值得存在这种混淆.
可能重复:
使用扩展方法表示的嵌套"来自"LINQ查询
我确定以前曾经问过,但老实说我找不到任何东西.
我很好奇只使用内置的Linq扩展方法,以下内容的等效语法是什么:
var z1 =
from x in xs
from y in ys
select new { x, y };
Run Code Online (Sandbox Code Playgroud)
我可以得到相同的结果:
var z2 = xs.SelectMany(x => ys.Select(y => new { x, y }));
Run Code Online (Sandbox Code Playgroud)
但它产生不同的IL代码,代码有点复杂且难以理解.使用扩展方法有更简洁的方法吗?
这是我写的整个测试方法:
private void Test()
{
var xs = new[] { 1D, 2D, 3D };
var ys = new[] { 4D, 5D, 6D };
var z1 =
from x in xs
from y in ys
select new { x, y };
var z2 = xs.SelectMany(x => …
Run Code Online (Sandbox Code Playgroud) 背景:
我为我的应用程序构建了一个安装程序,所有我的dll和内容文件都被正确地复制到了C:\Program Files\MyCompany\MyApp
目录中.当我从Visual Studio运行我的应用程序时,一切都很好.但是,当我运行我的应用程序的安装版本时,我得到了一个DirectoryNotFoundException
.问题似乎与Environment.CurrentDirectory
.
我原以为Environment.CurrentDirectory
是......
"C:\\Program Files\\MyCompany\\MyApp"
Run Code Online (Sandbox Code Playgroud)
......但实际上......
"C:\\Documents and Settings\\DanThMan"
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?我该如何解决这个问题?
谢谢.
编辑:
好的,嗯.如果我运行"开始"菜单快捷方式,则只会出现此问题 如果我直接运行MyApp.exe,一切都很好.
编辑2:
我想我现在已经深究了这一点.在我的安装程序(Visual Studio SetupProject)中,"开始菜单"快捷方式具有一个名为WorkingFolder
"安装快捷方式目标应用程序的文件夹" 的属性.我不小心设置WorkingFolder
了"MyCompany".它应该是"应用程序文件夹".现在我已正确设置,Environment.CurrentDirectory
再次按预期工作.感谢你的帮助.
编辑3:
但是,阅读下面的警告,我决定使用以下内容作为替代Environment.CurrentDirectory
:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Run Code Online (Sandbox Code Playgroud) 它看起来像是一个IValueFormatter
类型的值object
并返回一个类型的值string
,而a ValueResolver<TSource, TDestination>
取任何类型的值并返回任何类型的值.所以,它更灵活.还有一个问题是,使用a ValueResolver
,您永远不需要将源转换为特定类型 - 您可以在类定义中明确定义它.
鉴于此,为何使用IValueFormatter
?它做了什么不能做的事情ValueResolver
吗?我误解了它是如何工作的吗?
这个问题的灵感来自我与ASP.NET MVC的斗争,但我认为它也适用于其他情况.
假设我有一个ORM生成的模型和两个ViewModel(一个用于"详细信息"视图,一个用于"编辑"视图):
模型
public class FooModel // ORM generated
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public int Age { get; set; }
public int CategoryId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
显示ViewModel
public class FooDisplayViewModel // use for "details" view
{
[DisplayName("ID Number")]
public int Id { get; set; }
[DisplayName("First Name")]
public string FirstName { get; …
Run Code Online (Sandbox Code Playgroud) 假设我有一些代码可以执行一些浮点运算并将值存储在双精度数中.由于某些值无法以二进制形式完美表示,如何在合理程度的确定性下测试相等性?
我如何确定"合理"是什么意思?
可以double.Epsilon
用某种方式吗?
更新
几件事.正如@ ho1所指出的那样,文档double.Epsilon
指出,当比较两个双精度表示相等时,你可能想要一个远大于epsilon的值.以下是文档中的相关段落:
由于其最低有效位数的差异,两个明显等效的浮点数可能无法相等.例如,C#表达式(double)1/3 ==(double)0.33333,不比较相等,因为左侧的除法运算具有最大精度,而右侧的常量仅精确到指定的数字.如果创建一个自定义算法来确定是否可以将两个浮点数视为相等,则必须使用大于Epsilon常量的值来确定两个值相等的可接受的绝对差值.(通常,差异的差异比Epsilon大很多倍.) - http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx
......但问题是,多少倍?
如果它会影响你的答案,我的特殊情况涉及几何计算(例如使用点和向量的点积和交叉积).在某些情况下,您会根据是否A == B
,A > B
或者得出不同的结论A < B
,因此我正在寻找一个关于如何确定等价窗口大小的良好经验法则.
通过一个教程(Professional ASP.NET MVC - Nerd Dinner),我遇到了这段代码:
public IEnumerable<RuleViolation> GetRuleViolations() {
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required","Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
yield return new RuleViolation("Phone# required", "ContactPhone");
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
yield break;
}
Run Code Online (Sandbox Code Playgroud)
我已经读过了yield
,但我想我的理解仍然有点朦胧.它似乎做的是创建一个对象,允许循环遍历集合中的项目而不实际进行循环,除非并且直到它是绝对必要的.
不过,这个例子对我来说有点奇怪.我认为它正在做的是延迟任何 …
我正在考虑两种不同的类设计来处理某些存储库是只读的而其他存储库是读写的情况.(我预见不需要只写存储库.)
类设计1 - 提供基类中的所有功能,然后在子类中公开显示适用的功能
public abstract class RepositoryBase
{
protected virtual void SelectBase() { // implementation... }
protected virtual void InsertBase() { // implementation... }
protected virtual void UpdateBase() { // implementation... }
protected virtual void DeleteBase() { // implementation... }
}
public class ReadOnlyRepository : RepositoryBase
{
public void Select() { SelectBase(); }
}
public class ReadWriteRepository : RepositoryBase
{
public void Select() { SelectBase(); }
public void Insert() { InsertBase(); }
public void Update() { UpdateBase(); }
public …
Run Code Online (Sandbox Code Playgroud) 我最近将一个项目从WPF 3.5转换为WPF 4.0.功能上,一切正常,但我在Aero主题上应用的DataGrid风格突然停止工作.正如您从下面的前/后图片中看到的那样,我的DataGrids从Aero外观加上粗体标题,额外填充和交替行格式变成了简单的"Aero".除了删除所有对WPF工具包的引用(因为DataGrid现在是WPF 4.0的原生),我真的没有改变我的代码/标记.
之前(WPF Toolkit DataGrid)
之后(.NET 4.0 DataGrid)
正如我在之前的一个问题中所了解到的,如果我停止引用Aero资源字典,我能够再次使用自定义DataGrid样式,但是在Windows XP上看起来一切都看起来都是"Luna"(这不是我想要的).
那么,我如何确保我的应用程序始终使用Aero主题,但仍然在WPF 4.0中的主题上应用样式?
这是我的App.xaml代码:
<Application
x:Class="TempProj.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero,
Version=3.0.0.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
<ResourceDictionary Source="/CommonLibraryWpf;component/ResourceDictionaries/DataGridResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
这是我的DataGridResourceDictionary.xaml代码:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="DataGrid_ColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="TextBlock.TextWrapping" Value="WrapWithOverflow" />
</Style>
<Style x:Key="DataGrid_CellStyle" TargetType="DataGridCell">
<Setter Property="Padding" Value="5,5,5,5" />
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding …
Run Code Online (Sandbox Code Playgroud) 这是窗口声明:
<Window
x:Class="Pse.ExperimentBase.SplashWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{StaticResource _windowStyle}"
WindowStartupLocation="CenterScreen"
WindowStyle="None">
Run Code Online (Sandbox Code Playgroud)
这是样式声明:
<Style
x:Key="_windowStyle"
TargetType="Window">
<Setter
Property="Width"
Value="{Binding Path=InitialWindowWidth}" />
<Setter
Property="Height"
Value="{Binding Path=InitialWindowHeight}" />
<Setter
Property="Icon"
Value="Resources/MyIcon.ico" />
<Setter
Property="Background"
Value="{StaticResource _fadedOrangeBrush}" />
<Setter
Property="FontSize"
Value="11" />
</Style>
Run Code Online (Sandbox Code Playgroud)
讨论:
屏幕为1280 X 1024.窗口大小(由InitialWindowWidth,InitialWindowHeight绑定确定)为800 X 600.
窗口打开时,它显示188,141(左,上).这基本上应该是"西北".如果我计算真正的居中值,它应该是240,212(左,上).
线索?
它始终是第一个出现问题的窗口.如果我打开同一窗口的第二个实例,它将显示在正确的位置.
另一个线索?
如果我在打开第一个实例之前创建两个实例,则第二个窗口实例将仅显示在正确的位置.
所以...
Window win1 = windowFactory.CreateSplashWindow();
win1.Show();
Window win2 = windowFactory.CreateSplashWindow();
win2.Show();
win1.Hide();
Run Code Online (Sandbox Code Playgroud)
...抵消win1和win2
但...
Window win1 = windowFactory.CreateSplashWindow();
Window win2 = windowFactory.CreateSplashWindow();
win1.Show();
win2.Show();
win1.Hide();
Run Code Online (Sandbox Code Playgroud)
...抵消win1,但显示win2死亡中心.
所以我的问题是:
这里发生了什么??? …
c# ×8
.net ×3
wpf ×3
xaml ×2
.net-4.0 ×1
architecture ×1
asp.net-mvc ×1
attributes ×1
automapper ×1
class-design ×1
dry ×1
file-io ×1
ienumerable ×1
immutability ×1
join ×1
linq ×1
mapping ×1
mutable ×1
struct ×1
windows-xp ×1
wpf-4.0 ×1
yield ×1