0

I have an extension method which takes a parameter. Inside the extension method I want to get access to the name of the object which was passed in.

static MyObject Add<T>(this MyObject myObj, T value) {
    // MyObject is dictionary<string, object> for simple example
    myObj.Add(nameof(value), value);
    return myObj;
}

void run() {
  var anotherObject = new AnotherObject() { Name = "Bob" };
  var myObject = new MyObject().Add(anotherObject.Field);
}

When this executes it puts "value", "Bob" whereas i want it to put "Name", "Bob" in the dictionary.

Is this possible using the nameof() function or do I need to re-think the logic? The only thing i can come up with to preserve the general idea is:

static MyObject Add<T>(this MyObject myObj, string name, T value)...

myObject.Add(nameOf(anotherObject.Field), anotherObject.Field);

I didn't like having to repeat the field definition.

Dominic Cotton
  • 789
  • 10
  • 34
  • There is no way to do that. – SLaks Aug 24 '17 at 16:02
  • You could accept an expression tree. – SLaks Aug 24 '17 at 16:02
  • 1
    if you use nameof() the name has to be known at compile time. You can use reflection to access type names at runtime. If you need to call a generic method with the type you reflected you can use https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod(v=vs.110).aspx – Dennis Kuypers Aug 24 '17 at 16:03
  • I only scanned the question, disregard the comment and see my answer :) – Dennis Kuypers Aug 24 '17 at 16:10
  • It is not possible, for the simple reason that the parameter value does not necessarily even come from a variable with a name. For non-by-reference (i.e. not `out` or `ref`) parameters, _any_ value, generated by _any_ means (a variable, expression, method call, property getter, etc.), which is of the appropriate type can be passed. Only some of the value sources even have a name. It doesn't make any sense whatsoever to ask what the name of the passed value is. – Peter Duniho Aug 24 '17 at 19:00

2 Answers2

0

You can access the type name using typeof()

static MyObject Add<T>(this MyObject myObj, T value) {
    // MyObject is dictionary<string, object> for simple example
    myObj.Add(typeof(T).Name, value);
    return myObj;
}
Dennis Kuypers
  • 546
  • 4
  • 16
0

Sample below should help you out.

    //MyObject class

    public class MyObject{
     public string Name{get;set;}
    }

//AnotherObject class

    public class AnotherObject{
    public string Name{get;set;}
    }

//Method call

    void Main()
    {
      var anotherObject = new AnotherObject() { Name = "Bob" };
      var object1 = new MyObject();
      var obj = object1.Add<MyObject, AnotherObject>(object1, anotherObject);
     //print obj.Name; anywhere.
    }

//Extension Method

    static class Ext{
    public static MyObject Add<K,T>(this MyObject myObj, MyObject myObjOne, T value) {
        // MyObject is dictionary<string, object> for simple example
        string val = typeof(T).GetProperty("Name").GetValue(value).ToString();
        myObjOne.Name = val;
        return myObjOne;
    }

    }
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22