.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 ICollection , but T is different across properties - I have some ICollection & LT; String & gt; , some ICollection & LT; Integer & gt; , etc.

I can easily identify which type of property are 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}}    

You know that this 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 

it becomes difficult to implement Icicle> but it may be normal. It seems that you are in a better position :)

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -