-3

If need a way of telling a method which property of a object should be set. Here's an example:

public class Person
{
    public int A { get; set; }
    public int B { get; set; }
}

public class PersonController
{
    public void Create(int x)
    {
        var p = new Person();
        // How to tell if A or B should be set?
        p.A = x;
        // or
        p.B = x;
    }
}

Now this is a very simple example but imagine that I don't know what kind of object I need to modify.

How can I then tell which property needs to be set - A or B?

Nicklas Møller Jepsen
  • 1,248
  • 2
  • 16
  • 34
  • 3
    Make 2 methods? One sets A, the other sets B? – Borgleader Sep 11 '14 at 15:53
  • Maybe I wasn't specific enough. I need a generic method to handle this. – Nicklas Møller Jepsen Sep 11 '14 at 15:54
  • You need polymorphism http://msdn.microsoft.com/en-us/library/ms173152.aspx. – Polyfun Sep 11 '14 at 15:55
  • Can you be even more specific? Why do you need a generic method? – Richard Ev Sep 11 '14 at 15:55
  • you will need to give the value and something that can help you to identify the property to modify -a string, int or enum for example- – Smog Sep 11 '14 at 15:56
  • 1
    Your example doesn't quite show what the problem is. The method should determine what property to initialize with a parameter of a given type when more than one property of that type exist or is it something else? – Konstantin Dinev Sep 11 '14 at 15:56
  • 5
    @NicklasJepsen This smells of X/Y problem. Could you explain why you need this? – Borgleader Sep 11 '14 at 15:56
  • X/Y problem? And yes, I can. As I said, this is a very simple example. I need this for a generic way of logging messages. Now this works fine for Console logging, Debug, text files etc. Then I need to add database logging and here the log framework I'm writing do not know anything about the database. It is up to the consumer of the log framework to inject the database logging logic, but the it is still the log framework that should handle the logging. Now in the database I have several relations and depending on the log message, I need to know which relation should be set. – Nicklas Møller Jepsen Sep 11 '14 at 16:02
  • And why the downvotings? – Nicklas Møller Jepsen Sep 11 '14 at 16:02

2 Answers2

1

There are multiple ways of doing it. Here is a list, starting with the most desirable approach to the least desirable:

  1. Make two separate methods, CreateA and CreateB
  2. Pass an Action<Person> for post-initialization
  3. Make an enum WhichOneToSet {SetA, SetB}, and pass a value to Create as a second parameter
  4. Make a variable external to Create. Use that variable to determine the item to be set.

Here is an illustration of approach number three:

public void Create(Action<Person> postInit) {
    var p = new Person();
    postIniti(p);
    ...
}

The caller can call it like this:

PersonController ctrl = new PersonController();
ctrl.Create(p => p.A = 123);
ctrl.Create(p => p.B = 456);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You can use reflection to do this:

using System.Reflection;

public class PersonController
{
    public void Create(int x, string propName)
    {
        var p = new Person();
        obj.GetType().InvokeMember(propName,
           BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
           Type.DefaultBinder, obj, x);
    }
}

For more info see post Set object property using reflection

Community
  • 1
  • 1
malkassem
  • 1,937
  • 1
  • 20
  • 39