我正在尝试使用 ServiceStack 将对象添加到 redis 哈希。但是,当我尝试发送到 redis 哈希的对象之一具有 null 属性时,它会中断,获取未设置为 String 异常实例的 String 引用。
有人可以指出正确的方法来做我想做的事,在这种情况下,将一个对象存储为 redis 哈希,而它的一些属性设置为 null。
这是一个显示问题的片段:
public interface SomeInterface
{
string Id {get;set;}
string Name {get;set;}
DateTime? DateScheduled {get;set;}
DateTime? DateUnscheduled {get;set;}
ISomeInterface SomeNeededObject {get;set};
}
public class SomeClass : SomeInterface
{
public string Id {get;set;
public string Name {get;set;}
public DateTime? DateScheduled {get;set;}
public DateTime? DateUnscheduled {get;set;}
public ISomeInterface SomeNeededObject {get;set};
// Other useful properties/methods
}
public class SomeService :Service
{
public void SomeMethod( ISomeInterface objectToSendToHash)
{
SomeClass c = new SomeClass()
{
Id = "1",
Name = "Name1",
DateScheduled = DateTime.Now
}; // creates a SomeClass with DateUnscheduled and SomeNeededObject properties set to null
using (IRedisClient redisClient = this.MyRedisClient)
{
var redis = redisClient.As<ISomeInterface>();
redis.StoreAsHash(objectToSendToHash); //throws exception because either DateUnscheduled, or SomeNeededObject properties are null!
}
}
}