Get the instance name of this

后端 未结 4 1733
攒了一身酷
攒了一身酷 2021-01-22 08:50

Is this possible!!?!?!?!?

I\'m trying to make a set of classes that model a number of different types of things. Properties of these things change over time, and I want

相关标签:
4条回答
  • 2021-01-22 09:25

    It's not possible to do that kind of reflection. Variable names could be erased by the compiler depending on debugging options and scope. The name of a variable is an alias of its "address" so can not be reversed (at least in Java and C# AFAIK).

    An object can be pointed from nay variables, so the variable name is irrelevant and the object don't know this kind of stuff:

    Cat tiger = new Cat();
    Cat tiger2 = tiger;
    

    Variable tiger2 points to the Cat "tiger", so 2 variable points same object with different names.

    Use class properties passed in the constructor.

    0 讨论(0)
  • 2021-01-22 09:34

    The normal way of doing this is to store the 'name' as a variable in the instance, for ex:

    // Initialize this like: var cat = new Cat("Tiger");
    class Cat {
        public String Name { get; private set; }
        public Cat(String name) 
        { 
            Name = name; 
        }
    }
    

    And then if you need to get tigers in more than one place, make it into a method that explicitly returns Tigers:

    // This can go into a class of cat-creating static methods.  Or maybe in the Cat class itself.
    public static Cat GiveMeATiger() {
        return new Cat("Tiger");
    }
    
    0 讨论(0)
  • 2021-01-22 09:48

    Given the question as is, It is would be very hard, if not impossible, to do so.

    With debugging symbols, it is possible to dig up through stack frames and enumerate the local variables and match the address with the instance address. However, the approach won't work because the constructor will be invoked before the reference got set, so the tiger will be null at the time and matching will fail all the time.

    Yet if I can alter the question, the solution might be that you need to call MAGICSTUFF.GetInstanceName(this); in getter method of CatName instead, so my approach should be working fine.

    Now I'm trying to writing some code to do so, though I would post it here if someone might read this and come up with the real implementation before me.

    0 讨论(0)
  • 2021-01-22 09:52

    I know, I know, old post.

    but I found a solution...

    public static List<Packet> Packets = new List<Packet>();
    public class Packet
    {
        public Packet()
        {
            Packets.Add(this);
        }
        public string packetName;
    }
    
    public Packet myPacket = new Packet();
    myPacket.packetName = "myPacket";
    
    public void SomeMethod()
    {
        int x = 0;
        foreach (Packet p in Packets)
        {
            if (p.packetName = "myPacket")
            {
                Packet myPacket = Packets[x];
                break;
            }
            x++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题