((MyCustomObject)myObjectType).MyProperty 有巨大的成本吗?
((MyCustomObject)myObjectType).MyProperty has a huge cost?
好吧,我的模型中有一个 ObservableCollection 对象类型:
private ObservableCollection<object> _selectedItems = new ObservableCollection<object>();
public SelectedItems
{
get{return _selectedItems}
set
{
_selectedItems = value;
NotifyPropertyChanged("SelectedItems");
}
}
我正在使用附加的 属性 来更新视图中的选定项目,因此要执行此操作是使用对象类型的通用行为。
好吧,我的问题是在我的视图模型中我需要访问我的类型的属性。所以我这样做:
((MyCustomType)SelectedItems[0]).MyProperty.
但这是一项非常繁琐的工作,我认为这个演员表是有成本的。所以我做了很多次,我想知道是否有更好的解决方案。
也许使用动态类型而不是对象类型?好处是不需要做cast,但是失去了intellisense的特性,调试错误比较困难。
也许还有其他解决方案?
没有 boxing/unboxing 的转换(所以假设 MyCustomObject
是一个 class 而不是结构)没有特别大的开销,但是如果你知道类型是正确的解决方案总是MyCustomObject
就是声明集合为
private ObservableCollection<MyCustomObject> _selectedItems
= new ObservableCollection<MyCustomObject>();
好吧,我的模型中有一个 ObservableCollection 对象类型:
private ObservableCollection<object> _selectedItems = new ObservableCollection<object>();
public SelectedItems
{
get{return _selectedItems}
set
{
_selectedItems = value;
NotifyPropertyChanged("SelectedItems");
}
}
我正在使用附加的 属性 来更新视图中的选定项目,因此要执行此操作是使用对象类型的通用行为。
好吧,我的问题是在我的视图模型中我需要访问我的类型的属性。所以我这样做:
((MyCustomType)SelectedItems[0]).MyProperty.
但这是一项非常繁琐的工作,我认为这个演员表是有成本的。所以我做了很多次,我想知道是否有更好的解决方案。
也许使用动态类型而不是对象类型?好处是不需要做cast,但是失去了intellisense的特性,调试错误比较困难。
也许还有其他解决方案?
没有 boxing/unboxing 的转换(所以假设 MyCustomObject
是一个 class 而不是结构)没有特别大的开销,但是如果你知道类型是正确的解决方案总是MyCustomObject
就是声明集合为
private ObservableCollection<MyCustomObject> _selectedItems
= new ObservableCollection<MyCustomObject>();