访问子类中的值作为List<>;
本文关键字:List lt gt 子类 访问 | 更新日期: 2023-09-27 18:27:51
我已经创建了ParseObject的子类。我有一个List类型的属性。当我尝试使用属性获取值时,我会得到一个null;如果我使用属性名称作为索引访问该值,我会得到期望的值,即字符串列表。
我希望这些技术是等效的,通过属性访问更可取,因为它不会让你暴露在拼写错误中。有人能帮助我理解(a)为什么它们不等价,以及(b)我如何通过房产成功获取价值吗?
示例:
[ParseClassName( "MyThing" )]
public class MyThing : ParseObject
{
[ParseFieldName( "Name" )]
public string Name
{
get { return GetProperty< string >( "Name" ); }
set { SetProperty< string >( value, "Name" ); }
}
[ParseFieldName( "Notes" )]
public List< string > Notes
{
get { return GetProperty< List< string > >( "Notes" ); }
set { SetProperty< List< string > >( value, "Notes" ); }
}
}
elsewhere...
var name = aThing.Name; // I get the expected name
var asProp = aThing.Notes; // I get *null*
var asIndex = aThing["Notes"]; // I get an array of strings
事实证明,您必须将类型声明为IList<>,而不是List<>。我误解了Parse文档中所说的支持的数据类型:
objects that implement IList<T>
意味着可以实际使用IList<>的实现。并非如此:Parse类型从其GetProperty<IList>是Parse.Internal.FlexibleListWrapper。具体值与SetProperty<>兼容,而不是GetProperty<>。
我想,这个故事的寓意是,Parse不会在意你向它抛出的任何具体类型的转换,这似乎足够合理。如果您想在属性声明中保留具体类型,您可以始终在getter中进行转换。