我在VS2015 C#interactive中运行了以下代码片段并得到了一些非常奇怪的行为.
> double divide(double a, double b)
. {
. try
. {
. return a / b;
. }
. catch (DivideByZeroException exception)
. {
. throw new ArgumentException("Argument b must be non zero.", exception);
. }
. }
> divide(3,0)
Infinity
> 3 / 0
(1,1): error CS0020: Division by constant zero
> var b = 0;
> 3 / b
Attempted to divide by zero.
>
Run Code Online (Sandbox Code Playgroud)
为什么该方法返回无穷大而3/0引发错误并且3/b抛出格式化错误?我可以强制分裂抛出错误而不是返回无穷大吗?
如果我重新格式化方法
double divide(double a, double b)
{
if ( …
Run Code Online (Sandbox Code Playgroud) 编辑:有一个类似的问题在这里,但解决方案只建议变通方法,并没有提供分析上市公司问题或如何解决它的原因.这个问题可能仍然是重复的.
编辑2:事实证明这个问题只发生在调试期间,尽管它没有发生在早期.更换后(TCheck)null
与null as TCheck
测试通过跑的时候,但调试时抛出异常.
ORIGINAL POST: 我在单元测试中有一个方法,看起来像这样
internal void EqualityAssert<TCheck, TEquatable>(TEquatable item, ... )
where TCheck : class, IEquatable<TEquatable>, TEquatable
{
// Various equality assertions that are passing
// ...
// A == null
Assert.Throws<NullReferenceException>(
() => ((IEquatable<TEquatable>)item).Equals((TCheck)null));
}
Run Code Online (Sandbox Code Playgroud)
此方法由各种单元测试调用,并且每个测试都失败,因为遇到了"未处理的NullReferenceException",它正好在预期的位置.
Assert.Throws我之前的工作正常,但我无法弄清楚改变了什么来打破它.
澄清 我正在使用MVVM解决方案.我在ViewModels和Views之间有一对一的映射.我见过的所有解决方案都遵循第一种方法,其中View类型由IoC容器解析,并具有ViewModel作为依赖关系.我需要以某种方式扭转这种局面.
原帖:
我目前正在尝试重构一个简单的数据库查看应用程序,从Caliburn Micro到Prism (我很新).该应用程序当前使用ViewModel-First方法,ShellViewModel维护一个绑定到TabControl的ViewModel列表.我找不到如何在Prism中实现类似的方法.我见过的所有解决方案都使用了第一种视图方法,但我有多个状态都映射到一种类型的视图,需要将这些状态分开.
我有没有办法配置棱镜在视图模型分配到区域时自动注入视图?
谢谢.
我正在尝试加载一个 png 文件(其他格式是一个选项),以便在面向 .netstandard 1.4 的项目中的 OpenTk 中渲染为纹理,该项目不支持 System.Drawing 库。
我可以为此找到的每个 OpenTk 示例都取决于 System.Drawing.Bitmap 类。
这是我想在没有 System.Drawing 库的情况下创建的那种方法的示例,来自此Jitter Physics OpenGL Demo的纹理类
void CreateFromBitmap(System.Drawing.Bitmap image)
{
image.RotateFlip(RotateFlipType.RotateNoneFlipY);
GL.GenTextures(1, out name);
GL.BindTexture(TextureTarget.Texture2D, name);
// set pixel unpacking mode
GL.PixelStore(PixelStoreParameter.UnpackSwapBytes, 0);
GL.PixelStore(PixelStoreParameter.PackRowLength, 0);
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
GL.PixelStore(PixelStoreParameter.UnpackSkipRows, 0);
GL.PixelStore(PixelStoreParameter.UnpackSkipPixels, 0);
BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Requieres OpenGL >= 1.4
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.GenerateMipmap, 1); // 1 = True
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在lua 5.2中编写一个curry函数.我的代码看起来像这样:
function add(a, b)
return a + b
end
function curry(func, value)
return (function (...)
return func(value, table.unpack(arg))
end)
end
add2 = curry(add, 2)
print(add2(3))
Run Code Online (Sandbox Code Playgroud)
arg
但是该参数不包含传递给add2函数的值.
当我尝试从Lua文档运行示例时,它会出错,因为arg是nil.
printResult = ""
function print (...)
for i,v in ipairs(arg) do -- arg is nil
printResult = printResult .. tostring(v) .. "\t"
end
printResult = printResult .. "\n"
end
Run Code Online (Sandbox Code Playgroud)
如果不起作用,如何在5.2中使用可变长度函数?
正如用户@siffiejoe指出的那样,我的功能只是部分应用,而不是正确的currying.这是我使用接受的答案中的错误修复在lua中实现正确的curry函数.
function curry(func, params)
return (function (...)
local args = params or {}
if #args + …
Run Code Online (Sandbox Code Playgroud)