Possible Duplicate:
C# generic list <T> how to get the type of T?
I have a list variable and I want to get the type of that list and cast a second variable to that type. For clarity I added the code below.
Currently I have the following:
return _ValueList.Any<FieldType>(x => x.Equals((FieldType)fieldValueObject));
I want to do something like this:
Type valueType = _ValueList.GetType();
return _ValueList.Any<FieldType>(x => x.Equals((valueType)fieldValueObject));
Any help will be appreciated.
Update:
A invalid cast exception is thrown when fieldValueObject is of type Int16 and I try to cast it to Int32.
Update 2:
I used the following solution:
if (fieldValueObject.GetType() == typeof(Int16))
{
fieldValueObject = Convert.ToInt32(fieldValueObject);
}
return _ValueList.Any<FieldType>(x => x.Equals(fieldValueObject));
It is not pretty but it works.