我试图在C#中的泛型类型参数中使用多态.我已经在SO(审查其他几个问题1,2,3,4,5,6),但我仍然不清楚为什么这不工作(或者如果它甚至允许的).
我有以下课程:
public class Base { }
public class Derived<T> : Base { }
public class Foo { }
Run Code Online (Sandbox Code Playgroud)
和我的派生泛型类型的实例:
var derived = new Derived<Foo>();
Run Code Online (Sandbox Code Playgroud)
以下陈述都是正确的:
derived is Object
derived is Base
derived is Derived<Foo>
Run Code Online (Sandbox Code Playgroud)
当我尝试将我的派生类用作另一个泛型类型中的类型参数时,我得到一些意外的行为.鉴于以下惰性实例:
var lazy = new Lazy<Derived<Foo>>();
Run Code Online (Sandbox Code Playgroud)
以下是真实的:
lazy is Lazy<Derived<Foo>>
Run Code Online (Sandbox Code Playgroud)
但是当我预期它们是真的时,这些都是错误的:
lazy is Lazy<Object>
lazy is Lazy<Base>
Run Code Online (Sandbox Code Playgroud)
为什么是这样?它们应该是真的还是我误解了仿制药是如何工作的?
我感觉好像错过了一些明显的事情。
我想像DbContext
通常使用常规EF一样在LinqPad中测试我的EF Core 。
我有一个简单的DbContext
:
public class NotificationManagerContext : DbContext
{
public virtual DbSet<Notification> Notifications { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经使用新的EF Core 2.0.1驱动程序在LinqPad中注册了它:
在MCV Core应用程序中,我将在应用程序构建器中注册EF和SQL驱动程序,并将其传递给连接字符串。我看不到如何在LinqPad中执行相同的配置?
我正在使用LinqPad中NuGetVersion
的NuGet.Versioning
包.我正在尝试Dump()
检查它的属性,但不是通常的转储我只是得到字符串表示.
例如,这个:
var v = new NuGetVersion("1.0.0");
v.Dump();
Run Code Online (Sandbox Code Playgroud)
在输出窗口中显示以下内容:
1.0.0
Run Code Online (Sandbox Code Playgroud)
有人知道为什么LinqPad ToString()
在某些类型被转储时运行,以及如何更改此行为?
我有以下DTO:
public class Dto
{
public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图根据FA维基使用这种语法覆盖属性的比较:
public void Override_test()
{
// Arrange
var actual = new Dto { Date = DateTime.Now };
var expected = new Dto { Date = DateTime.Now };
// Act
// Assert
actual.ShouldBeEquivalentTo(expected, options =>
options.Using<DateTime>(x => x.Subject.Should().BeCloseTo(DateTime.Now)));
}
Run Code Online (Sandbox Code Playgroud)
但是测试没有编译.我收到此错误:
Cannot implicitly convert type 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>.Restriction<System.DateTime>' to 'FluentAssertions.Equivalency.EquivalencyAssertionOptions<FluentAssertions.ShouldBeEquivalentTo.Override.Dto>'
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议正确的语法吗?
我们需要更新集合中的所有文档以改变它们的形状。我们想在不同的集合中记录更改的审计,我们将在其中存储包含旧版本和新版本的文档。为了使这个更改原子化,我们使用了一个存储过程。
我们面临的问题是从存储过程更新另一个集合。似乎审计文档总是写入存储过程所属的集合中。
我写了一个示例存储过程:
function sample(prefix) {
var context = getContext();
var fooCollection = context.getCollection();
var barCollection = context.getCollection("BarCollection");
// Query documents and take 1st item.
var isAccepted = fooCollection.queryDocuments(
fooCollection.getSelfLink(),
'SELECT * FROM root r',
function (err, feed, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!feed || !feed.length) context.getResponse().setBody('no docs found');
else {
var fooDoc = feed[0];
var barDoc …
Run Code Online (Sandbox Code Playgroud) 我是TypeScript的新手,因为我已经完成了任何严肃的JavaScript开发,所以我可能会遗漏一些明显的东西.
我正在尝试使用TypeScript在Angular 1应用程序中使用Moment.
我正在使用Angular 1.6.5,Moment 2.17.1和TypeScript 2.17.1.我从npm @types\angular
包安装了Angular类型.Angular的Intellisense等正在使用VS Code.
我在这里将示例代码发布到GitHub:https://github.com/kevinkuszyk/moment-angular-typescript
我的示例应用程序在浏览器中运行,但TypeScript编译器抱怨它无法找到Moment:
app/controller.ts(3,20): error TS2503: Cannot find namespace 'moment'.
app/controller.ts(6,30): error TS2304: Cannot find name 'moment'.
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我尝试添加以下///
编译器指令:
/// <reference path="../node_modules/moment/moment.d.ts" />
Run Code Online (Sandbox Code Playgroud)
但这并没有修复TypeScript编译器:
app/controller.ts(5,20): error TS2503: Cannot find namespace 'moment'.
app/controller.ts(8,30): error TS2304: Cannot find name 'moment'.
Run Code Online (Sandbox Code Playgroud)
接下来,我尝试使用其文档中的说明导入Moment :
import * as moment from 'moment';
Run Code Online (Sandbox Code Playgroud)
但这会使TypeScript生成不同的erorr:
app/controller.ts(13,5): error TS2686: 'angular' refers to a UMD global, but the current file is a …
Run Code Online (Sandbox Code Playgroud) c# ×4
javascript ×2
linqpad ×2
.net ×1
.net-core ×1
angularjs ×1
azure ×1
azure-devops ×1
generics ×1
momentjs ×1
polymorphism ×1
tfs-workitem ×1
typescript ×1