我需要確定List是否包含ICollection,其中T是動態的,並且在編譯時不知道。繼承人我的代碼,以便更好地理解我的意思:確定列表<Type>是否包含ICollection <dynamic>
private void RefreshDataSource<T>(ICollection<T> dataSource) where T : IEquatable<T>
{
dynamic row = view.GetFocusedRow();
//Get's the focused row from a DevExpress-Grid
//I don't know the type because it's MasterDetail and the view can be a DetailView. In this case type T isn't the underlying type.
//Getting all Properties
var dummy = dataSource.FirstOrDefault();
var props = dummy.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
//Now comes the real problem, I need to determine the Detail Datasource
//and to do this I want to check if there is a Property from ICollection<typeof(row)>
//How can I check on ICollection<typeof(row)> instead of row
//IsAssignableFrom would better fit my needs but I don't get how to solve my problem with it.
var detailSource = props.FirstOrDefault(p => p.PropertyType.IsInstanceOfType(row));
}
的代碼被打破的重要點,因此,如果......不知道。在你的眼裏沒有意義;-)。有什麼方法可以檢查ICollection<T>
哪裏T是一個動態類型,只是在運行時知道?
注意,由於MasterDetail關係,給定的T在方法之上並不是行的類型!
UDPATE
我想我需要澄清我的需要。把我想象成一個網格。我得到的是一個ICollection<T>
的數據源,每一行都由T的一個對象表示。現在我使用MasterDetail關係,以便T代表網格中的一個MasterRow。 DetailView的行由任何ICollection<AnyClass>
表示,它被定義爲T上的屬性。
現在我需要從T中確定這個ICollection<AnyClass>
屬性,而不必知道AnyClass在編譯時是什麼。因爲我知道我的DetailView可以這樣做:
dynamic row = view.GetFocusedRow();
所以排式AnyClass,並在運行聞名。但是我怎麼能在T的PropertyCollection中找到這個ICollection<AnyClass>
屬性?這是我的問題。
我喜歡反思,如果你正在描述確切的問題,可以幫助你。你想知道ICollection中的「T」的類型名稱嗎? –
@Karthik AMR不,我的代碼的最後一行是重要的。在那裏,我想檢查道具是否包含一個條目,其中p => p.PropertyType是ICollection類型 –
Sebi
您是否確實指'動態'或T'? – CSharpie