0

I have the following stored procedure DB.GetData() that returns a result of type:

IEnumerable<GetData_Result>

From the DB.GetData(), how to get the Data Type of the IEnumerable, which in this case GetDataResult_Result, and then pass it as SomeType_Result as follow:

  var model = new SomeViewModel() {
     var names = typeof(SomeType_Result).GetProperties().Select(i => i.Name);
       }
usefulBee
  • 9,250
  • 10
  • 51
  • 89
  • 1
    I think Jon Skeet has answered a very similar question here that may help you. http://stackoverflow.com/questions/293905/reflection-getting-the-generic-parameters-from-a-system-type-instance – xDaevax Jun 02 '14 at 18:48

1 Answers1

1

You can use Type.GetGenericArguments

var argumentType = IEnumerable<GetData_Result>.GetType().GetGenericArguments()[0];

var model = new SomeViewModel() 
{
    var names = argumentType.GetProperties().Select(i => i.Name);
 }
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • When trying somoething like this: GetData_Result argumentType = IEnumerable.GetType().GetGenericArguments()[0] I get an error that I "Cannot implicitly convert type 'System.Type' to 'GetData_Result' – usefulBee Jun 02 '14 at 19:09
  • 1
    `GetGenericArguments` returns a `Type[]`, not an instance of the type. When selecting the `0 index` you get back a `Type` object, which is what you want – Yuval Itzchakov Jun 02 '14 at 19:11