在 WinForms 中,常见的初始化函数是初始化引用变量(例如)
class SomeClass : Form {
Button b;
SomeClass() {
InitializeComponents();
}
SomeClass(Container x) {
InitializeComponents();
}
void InitializeComponents() {
b = new Button();
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见, b 始终初始化为非空值。但是,C# 8 仍然会抱怨 SomeClass() 没有初始化不可为 null 的值 b。
当然我可以将 b 标记为可空(按钮?b)但是,现在我会在每次使用 b 时收到警告,因为未检查可空性(它不能为空...)
解决此问题的最佳方法是什么。是否有可用于将 InitializeComponent 标记为始终由构造函数调用的属性?
请注意,这是 WinForms 中非常常见的模式(每个组件...)
尤瓦尔
我正在尝试将 Web API 从 .NET Core 2.2 迁移到 .NET Core 3.0,但我偶然发现了以下内容:
public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null)
{
var tags = context.Tag.AsNoTracking();
if (tagIds != null)
tags = tags.Where(t => tagIds.Contains(t.TagId));
return tags
.ToList() // explicit client evaluation in 3.0
.ToDictionary(t => t.TagId, t => t);
}
Run Code Online (Sandbox Code Playgroud)
这用于生成与此类似的 SQL 语句:
SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
这对于正确索引的列和少量IN值非常有效。
现在我收到以下错误提示List<>.Contains不再支持翻译:
System.InvalidOperationException: '无法翻译 LINQ 表达式'Where( source: DbSet, predicate: (t) => (Unhandled parameter: __tagIds_0).Contains(t.TagId))'。以可翻译的形式重写查询,或通过插入对 …
entity-framework-core .net-core-3.0 entity-framework-core-3.0
以前我问过一个问题没有得到充分回答,因此我决定重新制定我的问题以了解发生了什么:
这是我的类层次结构:
interface I
{
void f();
}
class A : I
{
// non virtual method
public void f()
{
Debug.Log("---->> A ");
}
}
class B : A
{
// non overriding but hiding class A method
public void f()
{
Debug.Log("---->> B ");
}
}
class C : I
{
// non virtual method
public void f()
{
Debug.Log("---->> C ");
}
}
Run Code Online (Sandbox Code Playgroud)
这是执行代码:
Random rnd = new Random();
var randomI = rnd.Next(0, 2);
I i = …Run Code Online (Sandbox Code Playgroud) 我想与从外部(继承类)调用的另一个方法共享数据库上下文,而不创建新的上下文,除非正在释放它。我想检查上下文是否已处理,以便我可以创建新的上下文。
这是休息 API。有多个实体的批量上传,我想共享事务,因此如果一个实体失败,它将不会提交到数据库
我想知道为什么不显式使用 IServiceProvider 来解决依赖项而不是单独注入每个依赖项。换句话说,为什么要使用这种方法:
public class A
{
private B _b;
private C _c;
private D _d;
private E _e;
public A(B b, C c, D d, E e)
{
_b = b;
_c = c;
_d = d;
_e = e;
}
}
Run Code Online (Sandbox Code Playgroud)
而不是这个:
public class A
{
private B _b;
private C _c;
private D _d;
private E _e;
public A(IServiceProvider sp)
{
_b = (b) sp.GetService(typeof(b));
_c = (c) sp.GetService(typeof(c));
_d = (d) sp.GetService(typeof(d));
_e = (e) sp.GetService(typeof(e)); …Run Code Online (Sandbox Code Playgroud) 怎么避免呢?简化代码:
static void Main()
{
int[] board = { 1 };
CheckMove(board);
//board = 2
}
static void CheckMove(int[] board)
{
board[0] = 2;
}
Run Code Online (Sandbox Code Playgroud)
完整代码(WIP):
enum Sym
{
E, X, O
}
class Program
{
static void Main(string[] args)
{
Sym[,] board = new Sym[3,3];
int x, y;
while (End(board) == 2)
{
Display(board);
Console.WriteLine("Make your move - column: ");
x = Convert.ToInt32(Console.ReadLine())-1;
Console.WriteLine("Make your move - row: ");
y = …Run Code Online (Sandbox Code Playgroud) 我一直在弄清楚如何在使用 Include 时通过 Entity Framework Core 编写关于过滤相关实体的查询,假设我有以下两个类:
public class Order
{
public int OrderId {get; set;}
public String CreatedBy{get; set;}
public virtual Collection<OrderDetail> OrderDetails { get; set; } = new Collection<OrderDetail>();
}
public class OrderDetail
{
public Int64? OrderDetailID { get; set; }
public Int64? OrderID { get; set; }
public string ProductName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如果我想查找由“Jason”创建的所有订单,并且哪个订单详细信息的产品名称等于“Apple”,则在 sql 中它会是:隐藏复制代码
SELECT *
FROM Orders O
INNER JOIN OrderDetail OD ON O.OrderId = OD.OrderId
WHERE O.CreationUser = 'Jason' …Run Code Online (Sandbox Code Playgroud) 我目前正在测试一个应用程序,但它向我抛出了一个错误的 JSON 转义序列,但是我没有看到问题......
我可能忽略了一些东西,所以一双新鲜的眼睛可能会有用。
messageContents = "{\"command\":\"cue\",\"channel\":1,\"uid\":\"aesd-deaf\",\"type\":\"standard\",\"waitforexecute\":true,\"duration\":0,\"scene\":[{\"name\":\"Scene1\",\"fields\":[{\"Quad1\":\"F:\\TestFolder\\mill.jpg\"}]}]}";
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
{"Bad JSON escape sequence: \\T. Path 'scene[0].fields[0].Quad1', line 1, position 150."}
Run Code Online (Sandbox Code Playgroud)
任何人都可以发现错误吗?谢谢,肯尼斯
将ArrayPool与引用类型一起使用的正确方法是什么?
我以为它将充满用默认构造函数“更新”的对象。
例如,在下面的代码中,Foobars当您第一次从ArrayPool中租用时,所有的都为null。
2个问题:
由于返回的对象.Rent最初都是null,因此是否需要先用初始化的对象填充数组池?
返回租用的物品时,我需要清除每个物品吗?例如,foobar.Name = null; foobar.Place = null等等。
public class Program
{
public class Foobar {
public string Name {get;set;}
public string Place {get;set;}
public int Index {get;set;}
}
public static void Main()
{
ArrayPool<Foobar> pool = ArrayPool<Foobar>.Shared;
var foobars = pool.Rent(5);
foreach(var foobar in foobars) {
// prints "true"
Console.WriteLine($"foobar is null? ans={foobar == null}");
}
}
}
Run Code Online (Sandbox Code Playgroud) 将 ASP.NET Core Identity 更改为使用int而不是 GUID(根据此博客文章)后,我收到以下错误:
ArgumentException: 395438ed-1cd9-4420-8a58-3f3b1f550bfc 不是 Int32 的有效值。参数名称:值 System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) Microsoft.AspNetCore.Identity.UserStoreBase.ConvertIdFromString(string id) Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore.FindByIdAsync(string userId, CancellationToken) cancelToken) Microsoft.AspNetCore.Identity.UserManager.FindByIdAsync(string userId) Microsoft.AspNetCore.Identity.UserManager.GetUserAsync(ClaimsPrincipal principal) Microsoft.AspNetCore.Identity.SignInManager.ValidateSecurityStampAsync(ClaimsPrincipalPrincipal) Microsoft.AspNetCore.Identity.SecurityStampValidAsyncator (CookieValidatePrincipalContext 上下文)Microsoft.AspNetCore.Authentication。
尝试查询数据库时。关于为什么会这样的任何线索?
c# ×9
.net ×1
.net-core ×1
arrays ×1
asp.net-core ×1
c#-8.0 ×1
interface ×1
json ×1
syntax-error ×1
winforms ×1