4

In .net how do I fetch object's name in the declaring type. For example...

public static void Main()
{
Information dataInformation =  new Information();
}

public class Inforamtion
{

//Constructor
public Inforamtion()
{

//Can I fetch name of object i.e. "dataInformation" declared in Main function
//I want to set the object's Name property = dataInformation here, because it is the name used in declaring that object.
}

public string Name = {get; set;}
}
3
  • As an aside, I would say that trying to force some mapping between a variable name and any runtime value is a maintainability disaster. Commented Oct 31, 2009 at 4:56
  • 1
    Remember that what you are looking at is a text file and your variable name will simply be replaced by some label once it is compiled anyway. A variable name will ultimately end up as a pointer once the Jitter gets a hold of it. The text value of your variable name is not preserved outside your source file, and the debugging symbols. Commented Oct 31, 2009 at 5:01
  • Why in the world would you want to do it? Can you please explain this. Commented Oct 31, 2009 at 6:37

3 Answers 3

4

As far as the CLR goes, there's not really a way to determine an object's name. That sort of information is stored (to some extent) in the debugging information and the assembly, but it's not used at runtime. Regardless, the object you're referring to is just a bunch of bytes in memory. It could have multiple references to it with multiple names, so even if you could get the names of all the variables referencing the object, it would be impossible to programmatically determine which one you're looking to use.

Long story short: you can't do that.

Sign up to request clarification or add additional context in comments.

3 Comments

This is why every programmer should do some assembly language, or at the very least C
Python is also a scripting language, and is built entirely differently from the .NET Framework.
@Software Python allows this no more than the CLR does. In the case of Python, variables are created in "scope" objects, in the case of the CLR the instance members can be looked through with reflection (if they were locals this story would be different). However, this doesn't address the fundamental problem: a variable is not an object, merely a way to store/reference an object. Thus, using Python and/or the CLR would entail a manual search of an object through a collection of variables.
3

That is the variable name, not the object name. It also poses the question: what is the name here:

Information foo, bar;
foo = bar = new Information(); 

You can't do this for constructors etc; in limited scenarios it is possible to get a variable name via Expression, if you really want:

    public static void Main()
    {
        Information dataInformation =  new Information();
        Write(() => dataInformation);
    }
    static void Write<T>(Expression<Func<T>> expression)
    {
        MemberExpression me = expression.Body as MemberExpression;
        if (me == null) throw new NotSupportedException();
        Console.WriteLine(me.Member.Name);
    }

Note that this relies on the capture implementation, etc - and is generally cheeky.

1 Comment

Thanks a lot Marc, thought present one is not the answer, it has lead me to design another approach... Thanks a lot.
1

I don't think this is possible.

But at the first place, why do you need something like this??

With my experience i have realized that if you need something weird from a compiler or a language which is not offered, then (most often) it means that there is something wrong with the approach or the logic.

Please reconsider why are you trying to achieve this.

1 Comment

In short, this feature is required because we are designing a framework in which we plan to simplify lots of things. Anyway thanks for your comments...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.