我班上有一系列儿童用品,我有一个公共访问者.我想提供一个后置条件来确保集合中的项不为空(我知道,在测试2和3中,调用者可以更改集合,但是现在我的目标只是确保,从属性返回的集合没有不包含空项目.
我认为使用Assume和ForAll就足够了,但这没有用
这是我尝试过的3个类的示例代码.除了第一次公开ReadOnlyObservableCollection,2nd- ObservableCollection和3rd- List之外,所有3个案例都是完全相同的.
- ReadOnlyObservableCollection
class Test1
{
public Test1()
{
_children = new ObservableCollection<A>();
_childrenReadOnly = new ReadOnlyObservableCollection<A>(_children);
}
protected readonly ObservableCollection<A> _children;
protected readonly ReadOnlyObservableCollection<A> _childrenReadOnly;
public ReadOnlyObservableCollection<A> Children
{
get
{
Contract.Ensures(Contract.ForAll(Contract.Result<ReadOnlyObservableCollection<A>>(), i => i != null));
Contract.Assume(Contract.ForAll(_childrenReadOnly, i => i != null));
return _childrenReadOnly; // CodeContracts: ensures unproven: Contract.ForAll(Contract.Result<ReadOnlyObservableCollection<A>>(), i => i != null)
}
}
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(_children != null);
Contract.Invariant(_childrenReadOnly != null);
} …Run Code Online (Sandbox Code Playgroud) 我正在学习 TypeScript 和 React,当我遇到这段代码时,我发现!(非空断言)和?.(可选链接)都可以使用。
import { FC, FormEvent, useRef } from "react";
const NewTodo: FC = () => {
const textInputRef = useRef<HTMLInputElement>(null);
function todoSubmitHandler(ev: FormEvent) {
ev.preventDefault();
// v here v
const enteredText = textInputRef.current!?.value;
console.log(enteredText);
}
return (
<form onSubmit={todoSubmitHandler}>
<div>
<label htmlFor="todo-text">Todo Text</label>
<input type="text" id="todo-text" ref={textInputRef} />
</div>
<button type="submit">ADD TODO</button>
</form>
);
};
export default NewTodo;
Run Code Online (Sandbox Code Playgroud)
我所知道的!是,它告诉 Typescript 这个值永远null不是 Nor undefined。另一方面,?.用于防止未找到属性时出现错误,而是返回undefined。在上面的示例中,我可以使用其中一个! …
我有一个混合模式 DLL,其中有一个包含托管和非托管代码的 .cpp 文件。一个简化的重现示例如下所示:
#include "stdafx.h"
#pragma managed // Just for explicitness (doesn't influence results)
#include <msclr\marshal.h>
void Test()
{
System::String^ sName = "";
msclr::interop::marshal_context context;
context.marshal_as<const TCHAR*>(sName);
}
//#pragma unmanaged // uncomment this line to get errors
Run Code Online (Sandbox Code Playgroud)
此代码编译成功,但是如果我取消注释最后一行 ( #pragma unmanaged),则会产生以下错误:
2> Test.cpp
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(48): error C3280: 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler' : a member-function of a managed type cannot be compiled as an unmanaged function
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> …Run Code Online (Sandbox Code Playgroud) 我有一个使用 gdi+ 从某些位图绘制背景的应用程序。一些位图是垂直非线性渐变(例如,它们是 1 像素宽,应该水平拉伸以填充整个控件宽度)。问题在于,对于小图像(如上所述),右侧的某些控制区域未绘制。
我编写了一个测试程序,将 1x1 图像缩放到不同的尺寸,它表明当比例因子足够大时会出现问题
void Draw(HDC hDestDC, int destleft, int desttop, int destwidth, int destheight)
{
COLORREF buffer[] = {0xFF0000FF }; // 1 pixel image
const int width = 1, height = 1;
Gdiplus::Bitmap gdipBitmap(width, height, 4*width, PixelFormat32bppARGB, (BYTE*)buffer);
Gdiplus::ImageAttributes attrs;
Gdiplus::Rect dest(destleft, desttop, destwidth, destheight);
Gdiplus::Graphics graphics(hDestDC);
graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
graphics.DrawImage(&gdipBitmap, dest, 0, 0, width, height, Gdiplus::UnitPixel, &attrs);
}
// OnPaint:
for(int i=0; i<800; ++i)
Draw(hdc, 0, i, i, 1); // scale 1x1 image to different …Run Code Online (Sandbox Code Playgroud) .net ×1
bitmap ×1
c# ×1
c#-4.0 ×1
c++ ×1
c++-cli ×1
clr ×1
collections ×1
gdi+ ×1
image ×1
javascript ×1
mixed-mode ×1
pinvoke ×1
reactjs ×1
scaling ×1
typescript ×1