views:

62

answers:

4

Hello,

this seems odd to me:

if(customerList.Count > 0)
{
   if(typeof(customerList[0]).IsReferenceType)
   {
      // do what I want
   }
}

How would you do it?

A: 
bool isReferenceType = !(customerList[0] is ValueType);

EDIT

Or are you looking for something like:

bool listIsOfReferenceTypeObjects = !myList.GetType().GetGenericArguments()[0].IsValueType;
herzmeister der welten
What do you guys think why I did IF(customerList.Count > 0) ...
Lisa
Do I really need to tell not accessing List`s index 0 when the list.Count is zero?
Lisa
+2  A: 
  1. To determine whether the first item in a list is an object of a reference type:

    bool isReferenceType = !(customerList[0] is ValueType);
    
  2. To determine whether a list is a List<T> for some T that is a reference type:

    var listType = customerList.GetType();
    if (!listType.IsGeneric || listType.GetGenericTypeDefinition() != typeof(List<>))
        // It’s not a List<T>
        return null;
    return !listType.GetGenericArguments()[0].IsValueType;
    
Timwi
The most Exception I got because the customerList is empty and there is no index... :P
Lisa
How could I retrieve the isReferenceType when the List has no objects? but List<T> says its of type T like Customer. Actually I do not add a customer Object to find out my List takes customers of type Customer...
Lisa
@Lisa: You’re probably looking for the second solution in my answer. (It’s still not very clear though. Why can’t you just say what you’ve got and what you need?)
Timwi
A: 

You are probably trying to determine the actual type of the generic parameter of a generic collection. Like determining at runtime what is a T of a particular List<T>. Do this:

Type collectionType = typeof(customerList);
Type parameterType = collectionType.GetGenericArguments()[0];
bool isReference = !parameterType.IsValueType;
Adesit
A: 

ok that worked and I get no exception, when the customerList is empty.

Type collectionType = customerList.GetType();
   Type parameterType = collectionType.GetGenericArguments()[0];
   bool isReference = !parameterType.IsValueType;

@Adesit you get a point because your sample was right except the first line :P

Lisa
@Lisa, this is precisely identical to the second point in my answer, except that mine doesn’t crash when it’s not a generic type.
Timwi
@Timwi Hm you edited your solution, maybe I haven`t read it. I just need to know wether the 'T' of a List is a value type (int) or reference type (custom class). On my side nothing crashes.
Lisa
@Lisa: Of course it crashes! Try passing in *anything* that isn’t a generic type, e.g. a `string`...
Timwi
I havent checked your suggestion yet because somehow I feel we mean different things...: I will make an example. The user could plug a List<Customer> (Reference type) to my DataSource or a List<int> (Value type). I want to check for those types because if he plugs a List<int> I will throw an exception, because only a Reference type is allowed having a property which can be again a value type.
Lisa