我正在研究在Win 7上将Json Obejct反序列化到.net VS2015中的类。
public class1
{
[JsonProperty(TypeNameHandling = TypeNameHandling.Objects)]
public Class1 data1;
public Class2 data2;
}
public abstract Class1
{
some functions
}
public subClass1 : Class1
{
public string myData1 { get; set; }
public string myData2 { get; set; }
}
在我的反序列化代码中:
var mo = MyObject as JObject;
ParentMyClass = mo.ToObject<MyClass>();
我知道不能实例化抽象类。 因此,subClass1(这是Class1的实现之一)被序列化。 但是,反序列化后,subClass1为空。
我做错了吗?
更新: 因为类的代码太复杂,所以我只是简化了逻辑。
答案 0 :(得分:0)
调用class StretchyHeaderLayout: UICollectionViewFlowLayout {
var cache = [UICollectionViewLayoutAttributes]()
override func prepare() {
super.prepare()
cache.removeAll()
guard let collectionView = collectionView else { return }
let sections = [Int](0..<collectionView.numberOfSections)
for section in sections {
let items = [Int](0..<collectionView.numberOfItems(inSection: section))
for item in items {
let indexPath = IndexPath(item: item, section: section)
if let attribute = layoutAttributesForItem(at: indexPath) {
cache.append(attribute)
}
}
}
if let header = layoutAttributesForSupplementaryView(ofKind: StretchyCollectionHeaderKind, at: IndexPath(item: 0, section: 0)) {
cache.append(header)
}
}
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let visibleAttributes = cache.filter { rect.contains($0.frame) || rect.intersects($0.frame) }
guard let collectionView = collectionView else { return visibleAttributes }
// Find the header and stretch it while scrolling.
guard let header = visibleAttributes.filter({ $0.representedElementKind == StretchyCollectionHeaderKind }).first else { return visibleAttributes }
header.frame.origin.y = collectionView.contentOffset.y
header.frame.size.height = headerHeight.home - collectionView.contentOffset.y
header.frame.size.width = collectionView.frame.size.width
return visibleAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attributes = super.layoutAttributesForItem(at: indexPath as IndexPath)?.copy() as! UICollectionViewLayoutAttributes
guard collectionView != nil else { return attributes }
attributes.frame.origin.y = headerHeight.home + attributes.frame.origin.y
return attributes
}
override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: StretchyCollectionHeaderKind, with: indexPath)
}
override var collectionViewContentSize: CGSize {
get {
guard let collectionView = collectionView else { return .zero }
let numberOfSections = collectionView.numberOfSections
let lastSection = numberOfSections - 1
let numberOfItems = collectionView.numberOfItems(inSection: lastSection)
let lastItem = numberOfItems - 1
guard let lastCell = layoutAttributesForItem(at: IndexPath(item: lastItem, section: lastSection)) else { return .zero }
return CGSize(width: collectionView.frame.width, height: lastCell.frame.maxY + sectionInset.bottom)
}
}
}
时,Newtonsoft.Json库将尝试在该行下创建一个实例。问题是您无法实例化摘要。您可以通过在代码中的某个地方调用mo.ToObject<Class1>();
来看到自己的情况。这会给你错误。
也就是说,您可以做的一件事就是创建另一个继承抽象类的类。
var x = new Class1();
这样,您将拥有一个可以实例化的类。然后,您可以执行此操作。
public class NonAbstract : Class1 {}