Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting/getting the class properties by string name [duplicate]

What I'm trying to do is setting the value of the property in a class using a string. For example, my class has the following properties:

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

All the fields are of string type so I know ahead of time that it's always a string. Now, I want to be able to set the properties using a string as you could do with a DataSet object. Something like this:

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

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

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

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

myClass.Name = "John" 

Any code examples would be really great.

like image 232
Patratacus Avatar asked Apr 23 '12 15:04

Patratacus


People also ask

What is a get set c#?

The get method returns the value of the variable name . The set method assigns a value to the name variable. The value keyword represents the value we assign to the property. If you don't fully understand it, take a look at the example below.

How to access property in c#?

A property that has both accessors is read-write. In C# 9 and later, you can use an init accessor instead of a set accessor to make the property read-only. Unlike fields, properties aren't classified as variables. Therefore, you can't pass a property as a ref or out parameter.

What is@ property in php?

Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP. Value of property can be different for each instance of class.

How do you access the properties of an object in php?

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property .


2 Answers

You can add indexer property, a 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);         }      } } 
like image 114
Tigran Avatar answered Oct 07 '22 10:10

Tigran


You can add an indexer to your class and use reflection to aces the 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;         }     } } 
like image 22
Dennis Traub Avatar answered Oct 07 '22 08:10

Dennis Traub