小编Lar*_*ael的帖子

避免在域模型中使用JsonIgnore属性

我有一个包含多个实体类的域模型组件.在另一个组件中,我使用Json.NET序列化实现了实体存储库.我希望在序列化期间忽略一些实体属性,因此直接的解决方案是用JsonIgnore属性装饰这些属性.但是,在原则上,我想避免在我的域模型中引用其他组件 - 包括第三方库,如Json.NET.

我知道我可以创建一个自定义合同解析器,如此处所述,但很难概括序列化的内容和不在各种实体中序列化的内容.通常我想忽略所有只读属性,但是例如集合有例外:

public List<Pixel> Pixels
{
    get { return this.Pixels; }
}
Run Code Online (Sandbox Code Playgroud)

我也可以为这里描述的每个实体创建一个专用的合同解析器,但对我来说这似乎是一个高维护的解决方案 - 特别是对于众多实体.

理想的解决方案是,如果Json.NET支持.NET框架中的某些属性,但我甚至找不到合适的候选者......

我想Ignore在我的域模型中创建自己的自定义属性,并制作一个自定义合约解析器,它使用反射来检测此属性,并在序列化时忽略修饰的属性.但这真的是给定问题的最佳解决方案吗?

.net c# json.net

11
推荐指数
1
解决办法
5041
查看次数

无法转换通用对象

我有以下概念模型:

public interface IFoo<out T>
{
    T Data { get; }
}

public struct Foo<T> : IFoo<T>
{
    public Foo(T data) : this()
    {
        Data = data;
    }

    public T Data { get; private set; }
}

public class FooService<T> 
{
    ...

    public Foo<T> Get(string id)
    {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我尝试以一种概念上等同于此的方式使用它:

// Create and register a few FooService instances
ServiceLocator.Register(new FooService<DateTime>(), "someServiceId");
ServiceLocator.Register(new FooService<double?>(), "anotherServiceId");

// Retrieve a particular FooService instance and call the Get method
var fooService = (FooService<object>)ServiceLocator.Get("someServiceId"); …
Run Code Online (Sandbox Code Playgroud)

c# generics covariance

6
推荐指数
1
解决办法
1034
查看次数

无法使用Json.NET 8.0.1反序列化具有字节数组属性的对象

在升级代码库以使用Json.NET 8.0.1之后,一些反序列化会失败.使用Json.NET 7.0.1一切正常.显然,它是byte[]导致问题的类型属性的反序列化.如果我删除该byte[]属性它工作正常.我可以使用这个简单的控制台应用程序重现行为:

internal class Program
{
    private static void Main(string[] args)
    {
        Dictionary<string, Account> accounts;
        var jsonSerializerSettings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Objects,
            TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
        };

        using (var streamReader = new StreamReader("accounts.json"))
        {
            var json = streamReader.ReadToEnd();
            accounts = JsonConvert.DeserializeObject<Dictionary<string, Account>>(json, jsonSerializerSettings);
        }

        foreach (var account in accounts)
        {
            Debug.WriteLine(account.Value.Name);
        }
    }
}

internal class Account
{
    public string Id { get; set; }

    public string Name { get; set; }

    public byte[] …
Run Code Online (Sandbox Code Playgroud)

c# serialization json.net

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

如何使用 AutoFixture 自动生成包含只读列表的对象?

我有一个概念上看起来像这样的课程:

public class Entity
{
    private readonly List<double> _values = new List<double>();

    ...

    public List<double> Values
    {
        get
        {
            return _values;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在单元测试中,我想使用 AutoFixture 提供随机实体列表:

var entities = this.fixture.CreateMany<Entity>().ToList();
Run Code Online (Sandbox Code Playgroud)

但是,正如我所期望的(希望...),没有将自动生成的值添加到 Entity 对象的 Values 属性中。我尝试将值列表更改为非只读,并向 Values 属性添加一个设置器,这解决了问题,但是没有更好的替代方法吗?

c# autofixture

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

标签 统计

c# ×4

json.net ×2

.net ×1

autofixture ×1

covariance ×1

generics ×1

serialization ×1