Use string as a property in lambda expression c# -
this question has answer here:
i have method pass string , list in to. i'm trying achieve convert string lambda expression property.
private somemethod(string mytypestring, list<values> typelist) { foreach(var type in typelist.where(x => x."mytypestring" > datetime.now)) { //do loop } }
is there way that?
you can try using reflection:
using system.reflection; ... private void somemethod(string mytypestring, list<values> typelist) { propertyinfo pi = typeof(values) .getproperty(mytypestring, system.reflection.bindingflags.instance | system.reflection.bindingflags.nonpublic | // private/internal/protected system.reflection.bindingflags.public); // property exists, can read , returns datetime if (null == pi) return; // or throw exception else if (!pi.canread) return; // or throw exception else if (pi.propertytype != typeof(datetime)) return; // or throw exception foreach(var type in typelist .where(x => (datetime) (pi.getvalue(x, null)) > datetime.now)) { //do loop } }
Comments
Post a Comment