小编rod*_*ito的帖子

比较没有拳击的平等结构

我遇到了一个适用于结构的扩展方法,(SomeStruct)并返回该值是否等于default(SomeStruct)(当调用无参数构造函数时).

public static bool IsDefault<T> (this T value)
    where T : struct
{
    return (!EqualityComparer<T>.Default.Equals(value, default(T)));
}
Run Code Online (Sandbox Code Playgroud)

这让我想知道结构是否被装箱.这纯粹是出于好奇,因为根据上下文有拳击/传递值的优点/缺点.

假设:

  1. 以下第一种方法是非法的,因为结构不会隐式覆盖相等运算符==/!=.
  2. 第二个"出现"以避免拳击.
  3. 第三种方法应该始终对结构进行封装,因为它正在调用object.Equals(object o).
  4. 第四个有两个重载可用(object/T)所以我认为它也将避免拳击.但是,目标结构需要实现IEquatable<T>接口,使得辅助扩展方法不是很有用.

变化:

public static bool IsDefault<T> (this T value)
    where T : struct
{
    // Illegal since there is no way to know whether T implements the ==/!= operators.
    return (value == default(T));
}

public static bool IsDefault<T> (this T value)
    where T : struct …
Run Code Online (Sandbox Code Playgroud)

.net c# boxing equality value-type

7
推荐指数
1
解决办法
2041
查看次数

Making an HTTPs request on AWS ApiGatewayV2 websocket connection to Respond, or Delete it

UPDATE

This issue is partially resolved, the problem now lies in authenticating the ApiGateway request. I am unsure of how to acquire the necessary tokens to send with the request so that it is valid, because this is a [serverless-framework] service so I can't use the AWS Console to copy paste the tokens into the request's json data. Moreover, I wouldn't know what json key they'd have to be under anyways. So I guess this question has changed considerably in …

amazon-web-services node.js aws-lambda aws-api-gateway serverless-framework

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