Title: Source Unit name of any object that holds Type inforamtion
Question: Sometimes it would be required to programatically find out where a particular class is located in the source code. This method demonstrates the way it can be retrieved at run time with no overheads.
Answer:
Code to provide the Unit Name of objects with Type Information.
**************************************************
function GetUnitName(argObject: TObject): string;
var
ptrTypeData: PTypeData;
begin
if (argObject.ClassInfo nil) then
begin
ptrTypeData := GetTypeData(argObject.ClassInfo);
Result := ptrTypeData.UnitName;
end;
end;
**************************************************
Note: To make it work on objects inherited directly from TObject that class must be included with a {$M+} {$M-} directives.
Sri Ram