小编Zac*_*OIR的帖子

为什么TEventArgs在.NET生态系统的标准事件模式中没有逆变?

在.NET中了解有关标准事件模型的更多信息时,我发现在引入C#中的泛型之前,处理事件的方法由此委托类型表示:

//
// Summary:
//     Represents the method that will handle an event that has no event data.
//
// Parameters:
//   sender:
//     The source of the event.
//
//   e:
//     An object that contains no event data.
public delegate void EventHandler(object sender, EventArgs e);
Run Code Online (Sandbox Code Playgroud)

但是在C#2中引入泛型之后,我认为这个委托类型是使用泛型重写的:

//
// Summary:
//     Represents the method that will handle an event when the event provides data.
//
// Parameters:
//   sender:
//     The source of the event.
//
//   e:
// …
Run Code Online (Sandbox Code Playgroud)

.net c# contravariance .net-core

23
推荐指数
1
解决办法
918
查看次数

Dotnet发布不发布DLL到发布目录

我想发布我自己的.NET Core(2.2)应用程序,但是一个特定的NuGet包(Microsoft.Management.Infrastructure)永远不会发布到该publish文件夹(因为.dll文件中不存在).

我正在使用该命令dotnet publish -c Release --self-contained -r win-x64 ConsoleApp1.在visual studio中运行应用程序时,一切正常.但是在运行已编译的可执行文件时,我得到一个FileNotFoundException:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.Management.Infrastructure,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'.该系统找不到指定的文件.在ConsoleApp1.Program.Main(String [] args)

重现

1)创建一个简单的.NET Core控制台应用程序(我测试了2.1和2.2,没有区别)
2)添加NuGet包Microsoft.Management.Infrastructure
3)将以下代码添加到应用程序:

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var sessionOptions = new DComSessionOptions
            {
                Timeout = TimeSpan.FromSeconds(30)
            };

            CimSession Session = CimSession.Create("localhost", sessionOptions);

            var processors = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_Processor");
            foreach (CimInstance processor in processors)
            {
                Console.WriteLine(processor.CimInstanceProperties["Name"].Value);
            }

            Console.ReadLine(); …
Run Code Online (Sandbox Code Playgroud)

c# nuget .net-core

9
推荐指数
1
解决办法
569
查看次数

为什么接口 IOrderedEnumerable<T> 在 T 中不是协变的?

我正在查看 IOrderedEnumerable 的声明,令我惊讶的是它的 TElement 类型参数不是协变的。

public interface IOrderedEnumerable<TElement> : IEnumerable<TElement>, IEnumerable
{
    IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
}
Run Code Online (Sandbox Code Playgroud)

它没有成为协变的原因是什么?

c# covariance

5
推荐指数
1
解决办法
198
查看次数

为什么Objective-C编译器需要知道方法签名?

为什么Objective-C编译器需要在编译时知道在将对象推迟到运行时(即动态绑定)时将在对象上调用的方法的签名?例如,如果我写[foo someMethod],为什么编译器需要知道签名someMethod

objective-c objective-c-runtime method-signature

4
推荐指数
1
解决办法
169
查看次数

代表方差规则的怪异示例

在埃里克·利珀特(Eric Lippert)的博客文章中,简短地介绍了协方差和逆方差或方差,以及在《Cut in Nutshell》之类的书中指出:

如果要定义通用委托类型,则好的做法是:

  • 将仅在返回值上使用的类型参数标记为协变(out)。
  • 将仅在参数上使用的所有类型参数标记为反(in)。

这样做可以通过尊重类型之间的继承关系来使转换自然地工作。

因此,我正在对此进行试验,并且找到了一个相当奇怪的示例。

使用此类的层次结构:

class Animal { }

class Mamal : Animal { }
class Reptile : Animal { }

class Dog : Mamal { }
class Hog : Mamal { }

class Snake : Reptile { }
class Turtle : Reptile { }
Run Code Online (Sandbox Code Playgroud)

在尝试使用方法组到委托的转换和委托到委托的转换时,我编写了以下代码片段:

 // Intellisense is complaining here  
 Func<Dog, Reptile> func1 = (Mamal d) => new Reptile();

 // A local method that has the same return type and same parameter …
Run Code Online (Sandbox Code Playgroud)

c# delegates covariance contravariance

2
推荐指数
1
解决办法
222
查看次数

检查 Kusto 语言中是否存在表?

有没有办法使用 kusto 语言以编程方式检查日志分析中是否存在表?

例如,假设我想检查工作区是否包含类似于以下内容的 VMConnection 表:

IF OBJECT_ID('*objectName*', 'U') IS NOT NULL 
Run Code Online (Sandbox Code Playgroud)

或者

IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END
Run Code Online (Sandbox Code Playgroud)

azure-log-analytics azure-data-explorer

2
推荐指数
1
解决办法
1711
查看次数