-1

I have a class as

public class Customer
{
 public string Name {get; set;}
 
 public string Email {get;set;}

 //etc.
}

Somewhere in my application, I need to access these properties & to save myself from hardcoding property names, I am thinking to try something like the below but not finding any way to do so.

public string Get(string propertyName,string value)
{
   _customersList.SingleOrDefault(x => x.[propertyName] == value)
}

How can I access the customer properties dynamically or say by argument name?

Thanks!

Zara Khan
  • 134
  • 2
  • 16
Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • 2
    [Reflection](https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/reflection) is a powerful toolset for tasks like this. what have you ***tried yourself*** so far? what problems did you encounter? what have you researched? please **edit** your question to include more information. – Franz Gleichmann Jul 14 '21 at 09:24
  • 1
    Does this answer your question? [Creating a LINQ Select expression dynamically from string column names](https://stackoverflow.com/questions/41676566/creating-a-linq-select-expression-dynamically-from-string-column-names) – phuzi Jul 14 '21 at 09:25
  • 2
    What are you trying to save with your `Get()` method? Why not use `SingleOrDefault()` the way you did? How do you plan in calling your `Get()` method? It looks like it will result in the usage of magic strings. What are you trying to gain with such a (small) method? – Progman Jul 14 '21 at 09:30
  • _"& to save myself from hardcoding property name"_ ... that seems a bit fishy to me. What problem are trying to solve in this way? – Fildor Jul 14 '21 at 09:44

2 Answers2

2

Reflection is what you are looking for.

Example:

var customer = new Customer();
typeof(Customer).GetProperty("Name").SetValue(customer, "John"); // name set to 'John'  

In your case, you might want something like:

public string Get(string propertyName,string propValue)
{
   _customersList.SingleOrDefault(x => typeof(Customer).GetProperty(propertyName).GetValue(x) == propValue);
}
Ergis
  • 1,105
  • 1
  • 7
  • 18
  • How to deal when the property is nested I mean `Customer` class has another type/class reference `Address` ? – Kgn-web Jul 14 '21 at 09:50
  • `GetProperty` gives you a `PropertyInfo`. Study this object as it provides many info, from which, for example, `PropertyType`, which gives you the type. Can be string, int etc. If that property is supposed to be `Address`, maybe create a custom interface which these classes of yours implement (e.x `IMyModel`). Then you can check: ```if( typeof(Customer).GetProperty(propertyName).PropertyType is IMyModel ) ``` and so on – Ergis Jul 14 '21 at 10:33
0

I assume you probably want to take a look at Expandoobject (or maybe DynamoObject).

Basically you can do something like this:

dynamic sampleObject = new ExpandoObject();

sampleObject.test = "Dynamic Property";
Console.WriteLine(sampleObject.test);
Console.WriteLine(sampleObject.test.GetType());
// This code example produces the following output:
// Dynamic Property
// System.String

Then you could just access the properties. With DynamoObject you can inherit and override specific members to do somethine like you want.

Example copied from: https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=net-5.0

Sascha
  • 10,231
  • 4
  • 41
  • 65