.net - How can I find the type of T in a c# Generic collection of T when all I know is the type of the collection? -
I have an array of propertyInfo, representing property properties. T & gt; Some of these properties are of I can easily identify which type of property are You know that this it becomes difficult to implement ICollection , but T is different across properties - I have some
ICollection & LT; String & gt; , some
ICollection & LT; Integer & gt; , etc.
ICollection & lt ;. & Gt; Code>, using GetGenericTypeDefinition () method on , but I'm feeling it
id document item propertyIn [document] documents = item .GetType () GetProperties () .; PropertyInfo property = documentProperties.First (); Type typeOfProperty = property.PropertyType; If (typeOfProperty.IsGenericType) {type typeofproperty = property Property Type GetGenericTypeDefinition (); If (typeOfProperty == typeof (ICollection & lt; & gt;) {// detect the type of ICollection & lt t; t & gt; // and work accordingly}}
ICollection must be & lt; x & gt; but do not know X , That is quite easy with:
if (typeOfProperty.IsGenericype) {type genericDefinition = typeOfProperty.GetGenericTypeDefinition (); if (genericDefinition == typeof (ICollection & lt; & gt;) { // Note that we are calling GetGenericArguments at typeOfProperty, // not genericDefinition type typeArgument = typeOfProperty.GetGenericArgumen Ts () [0]; // typeArgument now you want ...}} when there is some type of type
Icicle> but it may be normal. It seems that you are in a better position :)
Comments
Post a Comment