Setting / getting class properties by string name

I am trying to set the value of a property in a class using a string. For example, my class has the following properties:

myClass.Name myClass.Address myClass.PhoneNumber myClass.FaxNumber 

All fields have a string type, so I know in advance that this is always a string. Now I want to be able to set properties using a string, as you could do with a DataSet . Something like that:

 myClass["Name"] = "John" myClass["Address"] = "1112 River St., Boulder, CO" 

Ideally, I want to just assign a variable and then set the property using the name of this string from a variable:

 string propName = "Name" myClass[propName] = "John" 

I read about reflection and maybe that was the way to do it, but I'm not sure how to set it up while keeping access to the property in the class intact. I want to still be able to use:

 myClass.Name = "John" 

Any code examples would be really good.

+63
set reflection c # properties get
Apr 23 2018-12-12T00:
source share
4 answers

You can add the indexer property, pseudocode:

 public class MyClass { public object this[string propertyName] { get{ // probably faster without reflection: // like: return Properties.Settings.Default.PropertyValues[propertyName] // instead of the following Type myType = typeof(MyClass); PropertyInfo myPropInfo = myType.GetProperty(propertyName); return myPropInfo.GetValue(this, null); } set{ Type myType = typeof(MyClass); PropertyInfo myPropInfo = myType.GetProperty(propertyName); myPropInfo.SetValue(this, value, null); } } } 
+85
Apr 23 2018-12-12T00:
source share

Add to any Class :

 public class Foo { public object this[string propertyName] { get { return this.GetType().GetProperty(propertyName).GetValue(this, null); } set { this.GetType().GetProperty(propertyName).SetValue(this, value, null); } } public string Bar { get; set; } } 

Then you can use like:

 Foo f = new Foo(); // Set f["Bar"] = "asdf"; // Get string s = (string)f["Bar"]; 

Source: Get property value from string using reflection in C #

+28
Jul 23 '14 at 19:59
source share

You can add an indexer to your class and use reflection for properties:

 using System.Reflection; public class MyClass { public object this[string name] { get { var properties = typeof(MyClass) .GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { if (property.Name == name && property.CanRead) return property.GetValue(this, null); } throw new ArgumentException("Can't find property"); } set { return; } } } 
+2
Apr 23 2018-12-12T00:
source share

Maybe something like this?

  public class PropertyExample { private readonly Dictionary<string, string> _properties; public string FirstName { get { return _properties["FirstName"]; } set { _properties["FirstName"] = value; } } public string LastName { get { return _properties["LastName"]; } set { _properties["LastName"] = value; } } public string this[string propertyName] { get { return _properties[propertyName]; } set { _properties[propertyName] = value; } } public PropertyExample() { _properties = new Dictionary<string, string>(); } } 
-one
Apr 23 2018-12-12T00:
source share



All Articles