views:

223

answers:

3

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;}
}
+4  A: 

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.

Adam Maras
This is why every programmer should do some assembly language, or at the very least C
Josh
Python allows to do this...
Software Enthusiastic
Python is also a scripting language, and is built *entirely* differently from the .NET Framework.
Adam Maras
@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*.
pst
A: 

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.

Amby
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...
Software Enthusiastic
+2  A: 

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.

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