1

IDE: Visual studio 2010, C#, .NET 4.0, Winforms application. Before I begin see this class:

public class Car
{
   private string _break;         

   public string Break
   {
      get { return _break; }
      set { _break = value; }
   }
}

And I have another class:

public class Runner
{
   Car cObj = new Car();

   string propertyName = "Break";
   //cobj.Break = "diskBreak"; I can do this but I have property name in string format  

   cobj[propertyName] = "diskBreak"; // I have the property name in string format
   // and I want to make it's Property format please suggest how to to this?
}

I have the property name in string format and I want to convert this in property and want to initialize it. Please tell me how to perform this, I think it is possible using reflection. But I don't have that knowledge.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122
  • 1
    The correct term is *reflection*. It's fairly easy what you want to do; just a few statements. First find how you can retrieve a property from an object, then find how you can set a value to that retrieved property. – Jeroen Vannevel May 28 '14 at 17:00
  • I have the property name in string format i.e. "Break", and I want to convert this in obj.Break so I can assign value to it., This is just an example I have to use this concept in my application – user3684582 May 28 '14 at 17:03
  • @Jeroen Vannevel, I know its reflection, spelling mistake. but its better if you can tell the solution instead of finding spelling mistakes. – user3684582 May 28 '14 at 17:05
  • 1
    I did give you the solution; I just told you what to do instead of giving you the code (which is 2 lines). Look at the linked duplicate if you want the code without any attempts from your side. – Jeroen Vannevel May 28 '14 at 17:07

2 Answers2

5

You can use either reflection or ExpandoObject, if you really don't need classes.

// 1. Reflection
public void SetByReflection(){
    Car cObj = new Car();
    string propName = "Break";
    cObj.GetType().GetProperty(propName).SetValue(cObj, "diskBreak");
    Console.WriteLine (cObj.Break);
}

// 2. ExpandoObject
public void UseExpandoObject(){
    dynamic car = new ExpandoObject();
    string propName = "Break";
    ((IDictionary<string, object>)car)[propName] = "diskBreak";
    Console.WriteLine (car.Break);
}

An always interesting alternative is to use "static" reflection, if you can get away using expressions and not strings - most likely unnecessary in your case, but thought I might as well contrast the different approaches.

// 3. "Static" Reflection
public void UseStaticReflection(){
    Car car = new Car();
    car.SetProperty(c => c.Break, "diskBreak");
    Console.WriteLine (car.Break);
}

public static class PropExtensions{
    public static void SetProperty<T, TProp>(this T obj, Expression<Func<T, TProp>> propGetter, TProp value){       
        var propName = ((MemberExpression)propGetter.Body).Member.Name;
        obj.GetType().GetProperty(propName).SetValue(obj, value);
    } 
}
DavidN
  • 5,117
  • 2
  • 20
  • 15
  • Good one! Just one small notion to 3.: Casting of MemberExpression can be done a bit safer. And you can get property info from expression itself. That way you are sure you got the property passed in and not just any MemberExpression. See e.g.: http://stackoverflow.com/questions/23849611/linq-expressions-as-params/23850566#23850566 – Edin May 28 '14 at 17:23
2

You can use Reflection to do this, for sample:

// create a Car object
Car cObj = new Car();

// get the type of car Object
var carType = typeof(cObj);

// get the propertyInfo object respective about the property you want to work
var property = carType.GetProperty("Break");

// set the value of the property in the object car
property.SetValue(cObj, "diskBreak");
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194