【发布时间】:2016-01-14 04:04:55
【问题描述】:
假设您有一个带有 ItemsSource-Property (DataGrid.ItemsSource) 的网格。此属性是在运行时设置的。可能的对象如下:
public partial class InstantFeedbackCollectionViewModel<TEntity, TPrimaryKey, TUnitOfWork>
: InstantFeedbackCollectionViewModelBase<TEntity, TEntity, TPrimaryKey, TUnitOfWork>
稍后在运行时我想捕获一个事件并想检查网格的 ItemsSource 是否属于上述类型。
通常我会这样做:
if (typeof(datagrid.ItemsSource) is InstantFeedbackCollectionViewModel) then ...
但是我怎样才能用这个泛型类做到这一点呢?
更新:
在第二步中,我想在 InstantFeedbackCollectionViewModel 中执行一个方法。类似的东西:
if (datagrid.ItemsSource.GetType().GetGenericTypeDefinition() == typeof(InstantFeedbackCollectionViewModel<,,>) {
var instFeedbackCollectionViewModel = grid.ItemsSource;
instFeedbackCollectionViewModel.ExecuteMyMethod();
}
有人知道怎么做吗?
【问题讨论】:
标签: c# .net class generics typeof