如果我有一个集合,说 Cells ,如果引用像 Cells [1,1] 给我一个该集合的对象,但成员对象没有该集合对象具有的某个属性。有没有办法从成员调用该属性?如下,假设 StartPosition 是集合的对象类的属性:
Cells [1,1] .StartPosition
或者
<Cells [1,1] .ParentCollection.StartPosition
解决方案
您只能调用在您正在访问的对象上定义的属性。
也就是说,如果要调用集合上的方法,请在集合上调用,
您可以 可以添加对包含集合的引用到您放置的每个项目,如果您设计
您的符号是数组符号,用于2维数组。虽然数组是集合,但大多数.NET集合不被视为数组,即使它们有 indexers 。
If I have a collection, say Cells, and if referenced like so Cells[1,1] it gives me an object of that collection but the member object doesn't have a certain property that the collection object has. Is there a way to call that property from the member? Like as follows, assuming StartPosition is a property of the object class for the collection:
Cells[1,1].StartPosition
or maybe
Cells[1,1].ParentCollection.StartPosition
解决方案
You can only call properties that are defined on the object you are accessing.
That is, if you want to call a method on the collection, call it on the collection, not on the content of the collection.
You could add a reference to the containing collection to each item you put in it, if you design and construct your classes that way.
Note:
Your notation is array notation, for 2 dimensional arrays. Though arrays are collections, most .NET collections are not considered to be arrays, even if they do have indexers.
更多推荐
将集合属性与成员对象一起使用
发布评论