C# 将所有日期存储为BsonDocuments
我觉得这是可能的,但我似乎找不到。我想将我的mongo驱动程序配置为将任何C# 将所有日期存储为BsonDocuments,c#,mongodb,datetime,C#,Mongodb,Datetime,我觉得这是可能的,但我似乎找不到。我想将我的mongo驱动程序配置为将任何DateTime对象存储为BsonDocument mongo c#驱动程序允许您在全局范围内设置某些约定,这样您就不需要对所有内容进行注释,日期-时间选项也可以这样做吗 例如,我想删除以下注释: [BsonDateTimeOptions(Representation=BsonType.Document)] 从我的所有DateTime属性。有人能给我指出正确的方向吗?早就应该这样做了,但答案是使用约定打包和设置 Conve
DateTime
对象存储为BsonDocument
mongo c#驱动程序允许您在全局范围内设置某些约定,这样您就不需要对所有内容进行注释,日期-时间选项也可以这样做吗
例如,我想删除以下注释:
[BsonDateTimeOptions(Representation=BsonType.Document)]
从我的所有
DateTime
属性。有人能给我指出正确的方向吗?早就应该这样做了,但答案是使用约定打包和设置
ConventionRegistry.Register(
"Dates as utc documents",
new ConventionPack
{
new MemberSerializationOptionsConvention(typeof(DateTime), new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document)),
},
t => true);
早该这么做了,但答案是使用约定打包和设置
ConventionRegistry.Register(
"Dates as utc documents",
new ConventionPack
{
new MemberSerializationOptionsConvention(typeof(DateTime), new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document)),
},
t => true);
当我试图验证devshorts提供的答案是否有效时,我得到了一个编译时错误(因为ConventionPack的Add方法(由集合初始值设定项语法调用)需要一个IConvention 建议的解决方案几乎正确,只需稍加修改:
ConventionRegistry.Register(
"dates as documents",
new ConventionPack
{
new DelegateMemberMapConvention("dates as documents", memberMap =>
{
if (memberMap .MemberType == typeof(DateTime))
{
memberMap .SetSerializationOptions(new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document));
}
}),
},
t => true);
如果我们需要在多个地方使用此约定,我们可以将其打包到一个类中,如下所示:
public class DateTimeSerializationOptionsConvention : ConventionBase, IMemberMapConvention
{
private readonly DateTimeKind _kind;
private readonly BsonType _representation;
public DateTimeSerializationOptionsConvention(DateTimeKind kind, BsonType representation)
{
_kind = kind;
_representation = representation;
}
public void Apply(BsonMemberMap memberMap)
{
if (memberMap.MemberType == typeof(DateTime))
{
memberMap.SetSerializationOptions(new DateTimeSerializationOptions(_kind, _representation));
}
}
}
然后像这样使用它:
ConventionRegistry.Register(
"dates as documents",
new ConventionPack
{
new DateTimeSerializationOptionsConvention(DateTimeKind.Utc, BsonType.Document)
},
t => true);
当我试图验证devshorts提供的答案是否有效时,我得到了一个编译时错误(因为ConventionPack的Add方法(由集合初始值设定项语法调用)需要一个IConvention 建议的解决方案几乎正确,只需稍加修改:
ConventionRegistry.Register(
"dates as documents",
new ConventionPack
{
new DelegateMemberMapConvention("dates as documents", memberMap =>
{
if (memberMap .MemberType == typeof(DateTime))
{
memberMap .SetSerializationOptions(new DateTimeSerializationOptions(DateTimeKind.Utc, BsonType.Document));
}
}),
},
t => true);
如果我们需要在多个地方使用此约定,我们可以将其打包到一个类中,如下所示:
public class DateTimeSerializationOptionsConvention : ConventionBase, IMemberMapConvention
{
private readonly DateTimeKind _kind;
private readonly BsonType _representation;
public DateTimeSerializationOptionsConvention(DateTimeKind kind, BsonType representation)
{
_kind = kind;
_representation = representation;
}
public void Apply(BsonMemberMap memberMap)
{
if (memberMap.MemberType == typeof(DateTime))
{
memberMap.SetSerializationOptions(new DateTimeSerializationOptions(_kind, _representation));
}
}
}
然后像这样使用它:
ConventionRegistry.Register(
"dates as documents",
new ConventionPack
{
new DateTimeSerializationOptionsConvention(DateTimeKind.Utc, BsonType.Document)
},
t => true);
很好,我也更新了我的答案来修正我的打字错误。很好,我也更新了我的答案来修正我的打字错误。