reflection, getting Type instance name
BlitzMax Forums/BlitzMax Programming/reflection, getting Type instance name
| ||
I have a type, I create an instance of the type with New. I want to get the name of the instance, but the TypeID.Name() method returns the type name, not the instance name.Type TMyType Field x:Float, y:Float End Type Local Dot:TMyType = New TMyType Local ID:TTypeId = TTypeId.ForObject(Dot) Print ID.Name() 'Prints "TMyType", but I want it to print "Dot" |
| ||
There is no reason for Objects to have "names". Imagine instead of "Dot:TMyType" you have list containing various TMyType-Instances. They would not have a name - would they? local dots:TList = CreateList dots.AddLast( new TMyType) So you'd better add a GUID/ID-system to your TMyType and then you could identify it by an manually or semi-automatically assigned name. Type TMyType field ID:int = -1 global lastID:int = 0 Method New() lastID :+ 1 ID = lastID End Method Method Init:TMyType(GUID:string) if GUID = "" then GUID = self.ID self.GUID = GUID return self End Method ... dots.AddLast( new TMyType.Init("dot") ) (above does not take care of clashes by manually assigning already used IDs as GUID) bye Ron |
| ||
What I am doing is trying to create an error logging routine for the methods in my type. I want to print out which object is generating the error. The sample below is basically what I'm doing now, passing a Name to the Create function, but I want to default to the object name when no name is passed. |
| ||
Thinking a bit more about it, I see what you are saying. The variable is only a placeholder for the object reference. Since the reference can be moved around (from variable to list, from list to another variable, etc...) there is no way to attach a name to it without explicitly giving it one (like my example above). Local Dot1:TPoint = TPoint.Create(10,10) 'Object created local Dot2:TPoint = Dot1 'Same Object, different variable holding the reference |
| ||
Yes ... exactly. Instead of "reflection" you also could default a name to be something hand-written (in your case the class name ... name:string="TMyType"). When extending objects you use custom "Method New()"-methods which override the default name by doing a (name = "TMyExtendedType"). This way you could avoid the depths of the reflection code (might save some cpu cycles in tight loops). bye Ron |