-7

I am passing a List to a method and I was wondering, the easiest way to return actual name of this list. I am trying to achieve something like this.

static List<string> GatherDataPerProduct(List<Pandora.Data.DomainObject> lstdata)
{
    if(lstData.value == "subjects")
    {
      //do whatever
    }
Habib
  • 219,104
  • 29
  • 407
  • 436
Arianule
  • 8,811
  • 45
  • 116
  • 174
  • 14
    what you mean 'name of this list'? – Kasyx May 16 '13 at 09:00
  • 1
    variable "names" as such have little to no meaning – Wim Ombelets May 16 '13 at 09:01
  • 1
    Why is this question downvoted so much? It's true that what he's trying to do is probably not possible, but that doesn't make it an invalid question. – Kenneth May 16 '13 at 09:09
  • 1
    @Kenneth, not my downvote, but i have read the question multiple times and still have no idea what is being asked. – Habib May 16 '13 at 09:12
  • 1
    It's true that it could be a little clearer, but I think that if he clarifies his question a bit, it could be a valid question. I may understand the down votes because it's a bit unclear. I do not however understand the close votes since it's definitely salvagable. – Kenneth May 16 '13 at 09:15
  • I believe that this question should not be closed (or if so, maybe only because it's a duplicate). @Arianule, you should specify more clearly that you are trying to get the variable name of the passed in parameter to the method call. – Alex Filipovici May 16 '13 at 09:27

3 Answers3

2

You will need to wrap your list in another object, that contains the extra data. e.g.

public class DataList
{
    public List<Pandora.Data.DomainObject> Data {get; set;}
    public string Name {get; set;}
}

...

static List<string> GatherDataPerProduct(DataList lstdata)
{
    if(lstData.Name == "subjects")
    ....
}
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
2

If what you mean is the name of the variable that was used when the list was passed to the method like this:

static List<string> GatherDataPerProduct(List<Pandora.Data.DomainObject> lstdata)
    {
        if(lstData.value == "subjects")
        {
          //do whatever
         }

List<DomainObject> subjects;
GatherDataPerProduct(subjects);

then what you're trying to do is not possible. The reason is that upon compilation, the compiler will remove all variable names. That means you cannot get the names of variables. If what you really want is name the object itself, then you should go with Dave's answer and wrap the list inside another object and define a property on it.

Kenneth
  • 28,294
  • 6
  • 61
  • 84
1

Try this:

using Extensions;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var subjects = default(List<Pandora.Data.DomainObject>);
            var result = Helper<List<Pandora.Data.DomainObject>>.ExampleFunction(() => subjects);
        }
    }

}
namespace Extensions
{
    static class Helper<T>
    {
        public static List<string> ExampleFunction(Expression<Func<T>> f)
        {
            if ((f.Body as MemberExpression).Member.Name == "subjects")
            {
                return new List<String>();
            }
            return null;
        }
    }
}

The original solution is here.

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78