gpt4 book ai didi

c# - InvalidOperationException : Can't compile a NewExpression with a constructor declared on an abstract class

转载 作者:行者123 更新时间:2023-12-03 15:35:58 28 4
gpt4 key购买 nike

尝试从mongodb检索对象时出现错误:

InvalidOperationException: Can't compile a NewExpression with a constructor declared on an abstract class



我的课是:
public class App 
{
public List<Feature> Features { get; set; }
}

public abstract class Feature
{
public string Name { get; set; }
}

public class ConcreteFeature : Feature
{
public string ConcreteProp { get; set; }
}

不知道为什么会有抽象方面的问题。我看到,mongodb记录了 _t: "ConcreteFeature"类型名称,它具有反序列化的所有功能。我在抽象类中没有构造函数。

有想法吗?

最佳答案

我需要列出BsonClassMap的“KnownTypes”以使其工作:

BsonClassMap.RegisterClassMap<Feature>(cm => {
cm.AutoMap();
cm.SetIsRootClass(true);

var featureType = typeof(Feature);
featureType.Assembly.GetTypes()
.Where(type => featureType.IsAssignableFrom(type)).ToList()
.ForEach(type => cm.AddKnownType(type));
});

这样,即使您将新类型添加到1个程序集中,也无需触摸代码。更多信息 here

关于c# - InvalidOperationException : Can't compile a NewExpression with a constructor declared on an abstract class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57015856/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com