标签: nullable

如何反序列化 Nullable<bool>?

我正在尝试Nullable<bool>从我的 XML 文件中反序列化 a 。我的期望是在我的 XMLElement 中找不到的 XMLAttribute 是null,如果找到,它将是truefalse。序列化也一样。如果它不为空,我的变量将被写入。

无论如何,每次我试图反序列化我的 XML 时,InvalidOperationException都会被抛出。

我的班级看起来像这样

[XMLArray("Users")]
public class User
{
    [XMLAttribute("copy")]
    public bool? copy;
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c# xml nullable xml-deserialization

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

在spring中自动将空列表转换为null

我想让所有DAO方法返回一个空集合而不是null.我怎么能用AOP弹簧做到这一点?

collections aop spring nullable

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

@Nullable似乎不适用于@AssistedInject

我有一个看起来像这样的构造函数:

@Inject
public MyClass(
        @Named("config") String configFile,
        @Named("template") String templateFile,
        CachedSettings settings,
        @Assisted String channelId,
        @Nullable @Assisted("NetworkA") NetworkInterface localNetworkInterfaceA,
        @Nullable @Assisted("NetworkB") NetworkInterface localNetworkInterfaceB) {
Run Code Online (Sandbox Code Playgroud)

我得到以下错误(两次,每个参数一次)

1) null returned by binding at my.company.package.MyClassFactory.create()
 but parameter 4 of my.company.package.MyClass.<init>() is not @Nullable
  while locating java.net.NetworkInterface annotated with @com.google.inject.assistedinject.Assisted(value=NetworkA)
   for parameter 4 at my.company.package.MyClass.<init>(MyClass.java:24)
  while locating my.company.package.MyClass annotated with interface com.google.inject.assistedinject.Assisted
Run Code Online (Sandbox Code Playgroud)

知道什么是错的吗?我在这个问题上发现了另外两个问题,其中一个说这是一个依赖性问题,我认为我没有,另一个说这是一个Eclipse问题,我确实使用过,但我刷新,清理并重建了我的问题. maven项目从头开始,所以我不确定问题是什么.

我正在使用javax.annotation.Nullable,它应该在运行时保留.我还应该尝试什么?

java nullable guice assisted-inject

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

com.sun.istack.internal.Nullable 注释和 Guice

当 Guice 说它可以识别这里的任何 @Nullable 注释(http://code.google.com/p/google-guice/wiki/UseNullable)时,这是否意味着我可以使用 com.sun.istack.internal.Nullable 提供的Java 7 运行时成功了吗?

包名是 com.sun.istack.internal 的事实让我相信我可能不应该使用它,但它使用起来非常方便......

java null nullable guice

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

Scala中的null引用如何对齐?

Liskov替换原则:B是A的子类型,如果任何人可以用A做一个人可以用B做什么.那么为什么Null是Scala中所有类的子类型,即使通过上面的定义它不是任何类的子类型?无法将Null定义为实现所有可能的方法,每个方法都返回null?Null在Scala中的定义方式是什么原因?

null scala nullable nullpointerexception

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

比较两个空的nullables

C#5.0规范在第7.1.3章中介绍

https://msdn.microsoft.com/en-us/library/ms228593.aspx

false如果一个或两个操作数都是,则提升的运算符产生值null.

但是测试也是这个MSDN链接

http://msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.100).aspx

int? num1 = 10;
int? num2 = null;

// Change the value of num1, so that both num1 and num2 are null.
num1 = null;
if (num1 == num2)
{
  // The equality comparison returns true when both operands are null.
  Console.WriteLine("num1 == num2 returns true when the value of each is null");
}

/* Output:
 * num1 == num2 returns true when the value of each is null
 */ …
Run Code Online (Sandbox Code Playgroud)

c# nullable lifted-operators

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

从值中减去可为空的十进制空值

我今天在 C# 中遇到了一个带有可为空小数的奇怪行为。

从某个值中减去空值给出空值。

例子:

2300.00 - null = null :(

using System;

public class Program
{
    public static void Main()
    {
        decimal? a=2300.00m;
        decimal? b=null;

        var result=a-b;

        Console.WriteLine("Result is {0}",result);
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么我得到的是 null 而不是 2300?

小提琴

.net c# nullable decimal c#-4.0

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

检查ObjC属性在运行时是否可为空

我试图在运行时弄清楚类的属性是否可以为空.例如:

@interface A : NSObject

+ (NSSet<NSString *> *)nullableProperties;

@property (readonly, nonatomic) NSString *description;

@property (readonly, nonatomic, nullable) NSError *error;

@end

@implementation A

+ (NSSet<NSString *> *)nullableProperties {
  // Code that identifies nullable properties.
}

@end
Run Code Online (Sandbox Code Playgroud)

nullableProperties应该在这种情况下,返回一个NSSet@"error".

property_getAttributesfunction可以提供一些属性的信息(这里有更多信息).不幸的是,它没有提供有关该属性是否被声明为可空的信息.

我想避免nullableProperties为我需要知道的可空属性的每个类实现.

nullable introspection objective-c declared-property

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

如何使 C# 类的 Object 属性可选?

我正在尝试在 C# 中创建一个模型类,其中我需要对象/列表属性作为可选属性:

public class Customer
{
        [JsonProperty("Custid")]
        public string CustId { get; set; }
        [JsonProperty("CustName")]
        public string CustName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
public class Store
{
        [JsonProperty("id")]
        public string Id { get; set; }
        [JsonProperty("Name")]
        public string? Name { get; set; }
        [JsonProperty("Customer")]
        public List<Customer>? Customers{ get; set; }            *//Error 1*
        [JsonProperty("OtherProperty")]
        public object? OtherProperty{ get; set; }                *//Error 2*
}
Run Code Online (Sandbox Code Playgroud)

上面的代码给出的错误为:-
错误 1:类型“对象”必须是不可为空的值类型,才能将其用作泛型类型或方法“可空”中的参数“T”
错误 2:类型“ List' 必须是不可为 null 的值类型,以便将其用作泛型类型或方法 'Nullable' 中的参数 'T'

请向我解释上述情况并为我提供替代解决方案。

.net c# nullable non-nullable

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

在 Kotlin 中对两个可为空的 Int 求和的最易读方法是什么?

我尝试执行以下操作:

val a: Int? = 1
val b: Int? = 1

a?.plus(b)
Run Code Online (Sandbox Code Playgroud)

但它不会编译,因为plus需要一个Int.

我还尝试创建一个 biLet 函数:

fun <V1, V2, V3> biLet(a: V1?, b: V2?, block: (V1, V2) -> V3): V3? {
    return a?.let {
        b?.let { block(a, b) }
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

val result = biLet(a, b) { p1, p2 -> p1 + p2 }
Run Code Online (Sandbox Code Playgroud)

但对于看似简单的事情来说,似乎需要做很多工作。有没有更简单的解决方案?

nullable kotlin

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