code_junkie

Converting a string to a class name

Nov 13th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. ArrayList<Customer> cust = new ..
  2.  
  3. Type elementType = Type.GetType("Customer");
  4. Type listType = typeof(List<>).MakeGenericType(new Type[] { elementType });
  5.  
  6. object list = Activator.CreateInstance(listType);
  7.  
  8. Type hai = Type.GetType(classString,true);
  9. Object o = (Activator.CreateInstance(hai)); //Or you could cast here if you already knew the type somehow
Add Comment
Please, Sign In to add comment