我有违反规则的习惯,如果它使我的代码更简洁或我的API更方便使用,并且如果我能在特定情况下巧妙地做到这一点.我想知道我是否可以逃脱以下这种侵权行为.
下图包括单个Child和单个Parent类.事实上,我正在描述我的应用程序中的常见情况.我对这个问题的决定会影响很多课程,这就是我提出这个问题的原因.
public sealed class Child : INotifyPropertyChanged
{
private Child()
{
//initialize basic state
}
public Child( Parent parent )
: this() //invoke parameterless contructor to initialize basic state
{
this.Parent = parent; //the last thing before the public ctor exits
}
public Parent Parent
{
get { return parent; }
set
{
if ( value != parent ) {
parent = value;
// initialize state that depends on the presence of a Parent instance …Run Code Online (Sandbox Code Playgroud) 我正在以独立于EF-Core的方式构建和执行我的查询,所以我依赖于IQueryable<T>获得所需的抽象级别.我正在等待等待SingleAsync()电话的等待ToAsyncEnumerable().Single()电话.我也用ToListAsync()电话取代ToAsyncEnumerable().ToList()电话.但我刚刚发现了这个ToAsyncEnumerable()方法,所以我不确定我是否正确使用它.
为了澄清我所指的扩展方法,它们的定义如下:
SingleAsync并在命名空间和程序ToListAsync集中的EntityFrameworkQueryableExtensions类上定义Microsoft.EntityFrameworkCore.ToAsyncEnumerable在程序集AsyncEnumerable的System.Linq命名空间中的类上定义System.Interactive.Async.当查询针对EF-Core运行时,调用ToAsyncEnumerable().Single()/ToList()与SingleAsync()/ToListAsync()功能和性能的等效性相比如何?如果没有,那么它们有何不同?
linq entity-framework system.reactive entity-framework-core .net-core
调用特定事件的处理程序的顺序取决于该特定事件的实现。例如,使用多案例委托的默认后备存储,处理程序将按照它们注册的顺序被调用。但是类设计者/实现者可能使用add和remove关键字来为事件访问器提供不同的后备存储,因此调用顺序也会不同。
.NET 框架基础库本身是否存在事件文档准确描述其调用顺序的情况?无论是否存在,依赖于此类记录的命令是否被认为是可接受的做法(例如,对于我自己实施和记录的事件)?为什么或者为什么不?
在以下程序中......
using System;
class Program
{
static Parent parent;
static void Main( string[] args )
{
parent = new Parent();
// The program hereafter runs for a long time and occasionally
// causes parent.SomeEvent to be raised.
}
}
class Parent
{
public event EventHandler SomeEvent;
public Parent()
{
new Handler( this );
}
}
class Handler
{
public Handler( Parent parent )
{
parent.SomeEvent += parent_SomeEvent;
}
void parent_SomeEvent( object sender, EventArgs e )
{
// Does something important …Run Code Online (Sandbox Code Playgroud) 我想检查以下内容
typeof( ICollection<> ).GetTypeInfo().IsAssignableFrom( targetProperty.PropertyType.GetTypeInfo() )
Run Code Online (Sandbox Code Playgroud)
传入的参数IsAssignableFrom是一个IList<Something>.但它正在返回虚假.
以下内容也返回false.
typeof( ICollection<> ).GetTypeInfo().IsAssignableFrom( targetProperty.PropertyType.GetTypeInfo().GetGenericTypeDefinition() )
Run Code Online (Sandbox Code Playgroud)
即使以下情况也会返回false.
typeof( ICollection<> ).GetTypeInfo().IsAssignableFrom( typeof(IList<>) )
Run Code Online (Sandbox Code Playgroud)
难道后者肯定会回归真实吗?
如果targetProperty.PropertyType可以是任何类型,我怎样才能得到正确的结果?它可以是a List<T>,a ObservableCollection<T>,a ReadOnlyCollection<T>,自定义集合类型等.
通常,C#编译器对方法绑定和类型参数推断很聪明.但我似乎已经难倒了.
class Obj
{
void Handler( int a, int b ) { }
Obj() { Method( "", Handler ); }
public void Method<T>( T t, Action<T> action ) { }
public void Method<T, U>( T t, Action<U, U> action ) { }
}
Run Code Online (Sandbox Code Playgroud)
该Method调用导致编译器错误:
参数2:无法从'方法组'转换为'System.Action'.
为什么编译器没有注意到调用符合第二个重载?我可以通过使调用更明确,如在Method<string, int>( "", Handler )或中编译它Method( "", (Action<int, int>)Handler ).但为什么这有必要呢?