How to call a property of an object with a variable in C #, for example. customer. & FIELDNAME

In C # there is a way to call an object property with a variable, something like this:

string fieldName = "FirstName";
Console.WriteLine(customer.&fieldName);

Answer:

Great, thanks for the quick answers, here is what I tried to do:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace TestLinqFieldIndex
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { ID = 1, FirstName = "Jim", LastName = "Smith" });
            customers.Add(new Customer { ID = 2, FirstName = "Joe", LastName = "Douglas" });
            customers.Add(new Customer { ID = 3, FirstName = "Jane", LastName = "Anders" });

            var customer = (from c in customers
                            where c.ID == 2
                            select c).SingleOrDefault();

            string[] fieldNames = { "FirstName", "LastName" };
            foreach (string fieldName in fieldNames)
            {
                Console.WriteLine("The value of {0} is {1}.", fieldName, customer.GetPropertyValue(fieldName));
            }

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string GetPropertyValue(string fieldName)
        {
            PropertyInfo prop = typeof(Customer).GetProperty(fieldName);
            return prop.GetValue(this, null).ToString();
        }

    }
}
+3
source share
4 answers

You can do this with reflection.

PropertyInfo prop = typeof(Customer).GetProperty ("FirstName");
Console.WriteLine (prop.GetValue (customer, null));

You can even get the value of private property. To do this, you need to look at the overloaded method GetProperty, which accepts flag bindings.

+6
source

You cannot do it out of the box; you will have to use the object PropertyInfoand reflection:

Apple myApple = new Apple("Golden Delicious", Ripeness.Fresh);

// ...

var p = typeof(Apple).GetProperty("Variety");
Console.WriteLine(p.GetValue(myApple, null)); // "Golden Delicious"

, # 4 . , .:)

+3

. , .

, , . ?

, - - . , , , , , .

As Frederick says below, you can use reflection, but the type argument is raised again, and you may find that you get runtime errors that you could not take into account during development.

+1
source

Just use the API Reflection.

The following example shows how to get the value of a property that returns a string:

public string GetValue(string propertyName, object MyInstance)
{
  Type instanceType = typeof(MyInstance);
  // Set the BindingFlags depending on your property type or simply skip them.
  PropertyInfo pi = instanceType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

  // If this is an indexer property, provide the index as the second parameter,
  // else just supply null.
  return (pi.GetValue(MyInstance, null).ToString());
}
+1
source

Source: https://habr.com/ru/post/1705240/


All Articles