So I've been searching around for about a week trying to figure out how to do this. While gathering bits and pieces of stuff I didn't know I found a relatively simple solution.
I think the original poster was looking for something like this, because if you have to use the name of the class to find out the name of the class then what's the point..
For those saying "It's not possible" and "Why would you want to.." my particular reason is for a class library where I have a class that the app developer can call the instances whatever they want, and it's meant to have multiple instances with different names. So I needed a way to be able to identify those instances so I can use the right one for the circumstance.
public static List<Packet> Packets = new List<Packet>();
public class Packet
{
public Packet(string Name)
{
Packets.Add(this);
name = Name;
}
internal string _name;
public string name
{
get { return _name; }
set { _name = value; }
}
}
It does require that they pass in the name of the instance as I've not yet figured out how to acquire the name they're using from inside the constructor. That is likely the thing that isn't possible.
public Packet MyPacket = new Packet("MyPacket");
This creates the instance, stores a reference to it in Packets and saves it's name in the newly created instance.
To get the name associated with the Packet and connect it to a variable..
Packet NewName = Packets[Packets.FindIndex(x => x.name == "MyPacket");
Whether you use the same variable name or a new one doesn't really matter, it's just linking the instance you want to it.
Console.WriteLine(NewName.name); // Prints MyPacket
For instances with the same name you would have to come up with some other way to tell them apart, which would require another list and some logic to determine which one you want.
myStudent, or the name of the student e.g.John?NSDictionaryOfVariableBindingsin Xamarin but I can´t because there´s no way to get the instance name...