正如我所看到的,没有提供原生nameof-keyword 如C#已建成打字稿.但是,出于与C#相同的原因,我希望能够以类型安全的方式引用属性名称.
当使用jQuery插件(Bootstrap-Tagsinput)或需要配置属性名称的其他库时,这在TypeScript中特别有用.
它可能看起来像:
const name: string = nameof(Console.log);
// 'name' is now equal to "log"
Run Code Online (Sandbox Code Playgroud)
name在Console.log重构和重命名时,赋值也应该改变.
到目前为止,在TypeScript中使用此类功能的最近方法是什么?
如何解析设备路径中带有驱动器号的路径?
例如,转换
\Device\HarddiskVolume4\Windows\System32\RuntimeBroker.exe
Run Code Online (Sandbox Code Playgroud)
进入
C:\Windows\System32\RuntimeBroker.exe
Run Code Online (Sandbox Code Playgroud)
假设HarddiskVolume4映射到C:这台计算机上。
我发现了这个问题,但我想在 C# 中使用它。
我编写了一个扩展方法,IServiceCollection其中包含一个选项委托.我的问题是我必须首先验证配置的选项(例如过滤掉null值),因为继续并实例化依赖于这些选项的服务是不安全的.
public static class ServiceCollectionExtensions {
public static void AddServices(
this IServiceCollection services,
Action<ServiceOptions> configureOptions)
{
// Configure service
services.AddSingleton<IAbstraction, Implementation>();
// Validate options here...
// Configure options
services.Configure(configureOptions);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在此验证在不调用委托的情况下正确指定了选项configureOptions?我不想依赖默认值,ServiceOptions因为我想强制设置一些设置.
我自己多次问过这个问题.我试图找到一些关于这个的博客文章,甚至挖到Roslyn源代码,但没有找到任何完整的答案.
基本上,通过一些现代的C#语言特性,编译器将采用一些语法糖并将其转换为更低级的C#代码.其中一些例子是:
using()生成一个try-finally绝对处理IDisposableIEnumerable<T>with的yield return函数会将该函数转换为实现为状态机的迭代器async必须返回Task<T>(或类似)并且也将变为状态机,可以从引擎盖下的程序事件循环重新输入所以,这些都是很好的功能,但编译器总是强制执行特定类型IEnumerable<T>,Task<T>并且IDisposable.这些类型是否以某种方式融入编译器?然而,编译器以某种方式绑定到标准库并不是真的,即使mscorlib只是普通的C#代码提供了通用功能吗?
我无法想象,因为编程语言是如此抽象和一般.正如我所看到的await,只要类型具有GetAwaiter扩展方法,就有可能进行任何操作.这对我来说听起来更抽象.
编辑
另外,如果有人能指出我在编译器中指定所需预定义类型的源代码,请告诉我!
假设我们有以下服务:
interface IService { }
interface IService<T> : IService {
T Get();
}
Run Code Online (Sandbox Code Playgroud)
在 ASP.Net-Core 中,在我们注册了一些不同的实现后,T我们可以获得所有注册的服务,如下所示:
IEnumerable<IService> services = serviceProvider.GetServices<IService>();
Run Code Online (Sandbox Code Playgroud)
现在,因为我需要从其他接口访问泛型类型参数,但这不是一个选项。如何在IService<T>不丢失泛型类型的情况下检索 的所有实现?就像是:
IEnumerable<IService<T>> services = serviceProvider.GetServices<IService<T>>();
foreach (var s in services) {
Method(s);
}
// Here we have a generic method I don't have control over.
// I want to call the method for each `T` registered in DI
void Method<T>(IService<T> service) {
Type t = typeof(T); // This here will resolve to the …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class Item<TItem>
where TItem : Item<TItem>
{
void GetReference()
{
TItem item = this;
}
}
Run Code Online (Sandbox Code Playgroud)
这里TItem item = this;生成编译器错误"无法Item<TItem>隐式转换为TItem".
但为什么我们需要转换呢?我们已经定义了约束where TItem : Item<TItem>,因此可以认为根本不需要转换,因为这两种类型是相同的,不是吗?
顺便说一下,显式转换是可用的.这也在编译器错误中说明.
在键盘快捷键上,我希望能够启动 Windows 命令提示符,并将工作目录设置为在解决方案资源管理器(或文本编辑器)中选择的当前项目目录。不幸的是,我无法在 Visual Studio 设置中找到合适的键盘快捷键。
在 Visual Studio 和 Visual Studio Code 中可以实现这样的事情吗?如果是这样,我该如何设置?
我有两个版本的LINQ选择器,它对非泛型类型进行一些过滤并返回一个通用枚举.考虑我们将使用相同的参数完全枚举两者source,我想知道哪个版本的性能更高:
public static IEnumerable<ICollection<TData>> OfType1<TData>(this IEnumerable<ICollection> source) =>
source
.Select(c => c as ICollection<TData>)
.Where(c => !(c is null));
public static IEnumerable<ICollection<TData>> OfType2<TData>(this IEnumerable<ICollection> source) =>
source
.Where(c => c is ICollection<TData>)
.Select(c => c as ICollection<TData>);
Run Code Online (Sandbox Code Playgroud)
在我看来,这一切归结为我们之间的差异is,as因为OfType2我们首先过滤(而不是最后),因此可以减少第二个算子.在第一种情况下,我们需要对可枚举中的所有元素执行两个操作.
那么这里的表现最好呢?引擎盖下如何做is和as不同?(链接到源代码欢迎!)
C# 8 添加了对异步迭代器块的支持,因此您可以等待事物并返回一个IAsyncEnumarator而不是一个IEnumerable:
public async IAsyncEnumerable<int> EnumerateAsync() {
for (int i = 0; i < 10; i++) {
yield return i;
await Task.Delay(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
使用如下所示的非阻塞消费代码:
await foreach (var item in EnumerateAsync()) {
Console.WriteLine(item);
}
Run Code Online (Sandbox Code Playgroud)
这将导致我的代码运行大约 10 秒。但是,有时我想await foreach在所有元素都被消耗掉之前突破。随着break然而,我们需要等到期待已久目前Task.Delay已经完成。我们如何在不等待任何悬而未决的异步任务的情况下立即跳出该循环?
c# ×7
asp.net-core ×2
generics ×2
types ×2
.net-core ×1
c#-8.0 ×1
constraints ×1
drive-letter ×1
implicit ×1
linq ×1
path ×1
performance ×1
roslyn ×1
shortcut ×1
typescript ×1
winapi ×1
windows ×1