我正在考虑将项目从 cef (CefSharp) 迁移到 WebView2 WPF。我的初步测试表明 WebView2 API 具有我需要的该端口的接口。我担心我会错过一些可能阻止我切换到 WebView2 的东西,我会在过渡的后期阶段解决这个问题。如果有人经历过这个过程,请分享我是否需要了解可能成为此过渡的障碍的事情。WebView2 中是否缺少来自 cef 的重要 API?
为什么 C# 编译器允许它编译并在运行时抛出运行时异常?
class Program
{
static void Main(string[] args)
{
IEnumerable<Test> list = new List<Test>() { new Test() };
foreach(IDisposable item in list)
{
}
}
}
public class Test
{
}
Run Code Online (Sandbox Code Playgroud)
这确实可以使用任何接口进行编译,并且如果您将 IDisposable 替换为具体类,则它不会编译。
为什么该程序打印“子名称”而不是“基本名称”?
using System;
class Program
{
static void Main(string[] args)
{
var child = new Child();
Console.WriteLine(((INamedObject)child).Name);
Console.ReadLine();
}
}
interface INamedObject
{
string Name { get; }
}
class Base : INamedObject
{
string INamedObject.Name
{
get
{
return "Base Name";
}
}
}
class Child : Base, INamedObject
{
public string Name
{
get
{
return "Child Name";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望当我将子类型转换为INamedObject时,将调用INamedObject显式实现的Name属性。但是会发生什么,就是调用了Child.Name属性。
为什么当我这样声明Child类时(从Child中删除INamedObject):
class Child : …
Run Code Online (Sandbox Code Playgroud) 我有规范模式实现,我想改变它以支持逆变.然而,出现了有趣的问题.
public interface ISpecification<in T>
{
Func<T, bool> Predicate { get; }
bool IsSatisfiedBy(T entity);
}
public class Specification<T> : ISpecification<T>
{
public Specification(Func<T, bool> predicate)
{
this.Predicate = predicate;
}
public Func<T, bool> Predicate
{
get;
private set;
}
public bool IsSatisfiedBy(T x)
{
return Predicate.Invoke(x);
}
public static Specification<T> operator &(Specification<T> left, ISpecification<T> right)
{
return new Specification<T>((x) => left.Predicate(x) && right.Predicate(x));
}
}
Run Code Online (Sandbox Code Playgroud)
你可以期待这项工作
new Specification<DerivedClass>((x) => true) & new Specification<BaseClass> ((x) => true)
Run Code Online (Sandbox Code Playgroud)
但如果我颠倒了参数的顺序,它就不再编译了
new Specification<BaseClass>((x) …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个电子邮件应用程序,我想在 WebView2 中显示邮件消息。我想让用户选择下载或不下载 Html 邮件消息中的远程资源(出于安全原因)。如何取消WebView2中请求的资源?
我尝试订阅事件 webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
但是当调用事件处理程序时,我看不到取消请求的方法。