Using reflection to retrieve a value from a list

I have a simple method which retrieves a table from an azure mobile service.

public static async List<T>GetDataFromListTable<T>() { var data = await MobileService.GetTable<T>().ToListAsync(); return data.Count != 0 ? data : null; }

This works fine.

What I am trying to do is have another method that takes a parameter name which is returned from the service and return the value of that parameter. So far I have this

public static async Task<T> GetDataFromTable<T>(string paramName) { var k = Activator.CreateInstance(typeof(T)); var members = typeof(T).GetProperties().Select(t=>t.Name).ToList(); if (!members.Contains(paramName)) return (T)k; var mn = typeof(T).GetProperties()[members.IndexOf(paramName)]; var data = GetDataFromListTable<T>(); var retval = data.Select(t => t.mn); }

The issue is obviously that I can't do the Linq query as T doesn't contain mn. I can also not use

var retval = data.Select(t=>t.paramName);

as paramname is a just a string representation of a member within a class.

In a nutshell...

method 1 has the parameter name, grabs a list from method 2. From the returned list in method 2, find the parameter name and return the associated value.

Is there a way to do what I'm trying to do?


You can do:

var retval = data.Select(t => mn.GetGetMethod().Invoke(t, null));


var retval = data.Select(t => mn.GetValue(t, null)); <hr>

You can also simplify your code with something like this (not tested, sorry):

public static async Task<T> GetDataFromTable<T>(string paramName) { var k = Activator.CreateInstance(typeof(T)); var mn = typeof(T).GetProperty(paramName); if (mn == null) return (T)k; var data = GetDataFromListTable<T>(); var retval = data.Select(t => mn.GetGetMethod().Invoke(t, null)); ... }


I think using expression trees would be more convenient since you're working with collections. Your method signature needs to incorporate the types T and TResult since it is using Select which returns an IEnumerable<TResult>.

public static async Task<IEnumerable<TResult>> SelectData<T, TResult>( string propertyName ) { if(string.IsNullOrWhiteSpace(propertyName)) { return Enumerable.Empty<TResult>(); } var dataTask = GetTableData<T>(); var tType = Expression.Parameter(typeof(T), "t"); var property = Expression.Property(tType, propertyName); var selectExpression = Expression.Lambda<Func<T, TResult>>(property, tType) .Compile(); return (await dataTask).Select(selectExpression); }


Isn't it possible to do this

var retval = data.Select(t => mn.GetValue(t, null));


  • Karate: Query Param values are getting encoded
  • When do superclasses not have a default constructor?
  • Jenkins Pipeline Groovy: Read default parameter value from another job?
  • How to create Android Handler in a Java plugin called from Unity
  • Richfaces editable dataTable not setting updated values in Bean
  • how to call mdi form by function
  • EditText.SetText() changes my softkeyboard input type in a custom adapter
  • Launch AutoCAD 2015 from .Net process
  • Executing nodejs script file in PHP using exec()
  • How can I test TCP socket status in Perl?
  • Templates, Inheritance, and Virtual Methods (C++)
  • Spring integration warning Referenced bean 'org.springframework.scheduling.support.PeriodicTrig
  • How to pass the contents of a file using `cat` to `_values` (zsh completion)
  • wcstombs() has invalid output on Android
  • Peek MSMQ message with infinite timeout
  • How to extract element-path from XMLType Node?
  • IsOrderedBy Extension Method
  • Excel VBA URLDownloadToFile Error for https Ressource
  • Azure webjobs output logs indexing taking very long
  • converting text file into xml using php?
  • Unable to get column index with table.getColumn method using custom table Model
  • ADO and msqli connections very slow
  • custom UITableViewCell with image for highlighting
  • Marklogic : Query response time is very high
  • How to test if a URL from an Eclipse bundle is a directory?
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Meteor helpers not available in Angular template
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • ILMerge & Keep Assembly Name
  • Large data - storage and query
  • How can I estimate amount of memory left with calling System.gc()?
  • WOWZA + RTMP + HTML5 Playback?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • Trying to get generic when generic is not available
  • How does Linux kernel interrupt the application?
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++