0

I need get property name as string from object class. It is possible in Delphi?

I need property transfer as argument of method and get property name as string. I don't want use property name as argument because the compiler does not catch error when name of property is changed in class.

type
  TMyClass = class
  private
    fField: some_type;
  public
    property Field:some_type read fField;
  end;

function GetPropertyName(arg: ??):string
begin
  Result := arg.PropertyName; // here I need get property name form transfer type
end;

var
  obj: TMyClass;
  name: string;
begin
  name := GetPropertyName(obj.Field);  
end;

To clarify, as discussed in comments, I'm looking for a direct equivalent to the C# nameof function.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
jagro
  • 1
  • 2
  • 1
    I'm sorry, I cannot understand this question at all. What value do you expect to be written into `name`? – David Heffernan Jan 31 '19 at 13:12
  • The Name of your property `Field` is Field. If you search for something else, please describe your question better – GuidoG Jan 31 '19 at 13:14
  • Sorry, I need get name of property as string. Method GetPropertyName(obj.Field) must return string 'Field', not value of fField – jagro Jan 31 '19 at 13:19
  • maybe [this](https://stackoverflow.com/questions/777125/how-to-get-the-property-type-name-for-custom-properties) might help – GuidoG Jan 31 '19 at 13:23
  • What you are trying to do is impossible. You will need to find a different way to solve the problem. – David Heffernan Jan 31 '19 at 13:33
  • If `obj.Field` reference is resolved by the compiler, you know that the name has not changed at compile time. – LU RD Jan 31 '19 at 13:33
  • I saw this thread, but isn't resolved my problem, because I don't want use property name but maybe use pointer to property or anything similar. – jagro Jan 31 '19 at 13:34
  • @LURD reference is ok but sometimes I need get name of property reference, example paste the name of property to sql query – jagro Jan 31 '19 at 13:49
  • 1
    Dude, what you are asking for is not possible. If you need the name, you need to pass the name. You could the use RTTI to read the property value. But as asked it is impossible. – David Heffernan Jan 31 '19 at 13:51
  • I'll just throw it out there that it's a common rookie mistake to try to reference control or object names and properties using the human-readable string tokens in the source code. This isn't to say that things like RTTI don't have valid use cases, but I would guess that this isn't one of them. We can't suggest a better alternative without understand what, exactly, you're trying to do, but the correct solution to your problem is most likely a different strategy altogether. – J... Jan 31 '19 at 14:12
  • @jagro, then declare a string constant for the property name for use in the sql query. At program start, test the name against all published properties of the object. – LU RD Jan 31 '19 at 14:23
  • I programing in C# and use EF, expression `context.Entity.Where(p => p.idEntity == 1).First();` is often used and compilator throw an error when property `idEntity` change name and I don't have to remember column name in DB. I wanted do the same in Delphi, in own ORM. Writing name of column as string isn't comfortable and is problematic. In Delphi I also have own class to bind property of class to component, but I must use property name as string. Property name as string generate exception during program is running when I change name of column in model, because compilator not find an error. – jagro Jan 31 '19 at 20:56
  • 1
    I suggest using the RTTI to iterate through all available fields, methods and properties. You can then see all names of the methods, properties and fields, and for fields and properties all meta info (datatype, visibility), and for properties their access possibilities (r/w/default/stored), and much more. Check this for a start: https://stackoverflow.com/questions/11350563/delphi-rtti-get-propertys-class – H.Hasenack Jan 31 '19 at 21:49
  • What you are attempting to do is impossible in C# too. – David Heffernan Jan 31 '19 at 22:38
  • I know RTTI and use this, but RTTI not allow use property as argument of method. – jagro Jan 31 '19 at 22:45
  • @DavidHeffernan what you talk, this code is C# `context.Entity.Where(p => p.idEntity == 1).First()`, `idEntity` is property of class. In C# I can get name as string from property. Example `public class A { public int Foo { get { return 1; } } } ... A obj = new A(); string name = nameof(obj.Foo);` – jagro Jan 31 '19 at 23:02
  • OK, well `nameof` isn't a function that you write in C#, it's provided by the compiler. I think we can safely answer the question now. – David Heffernan Feb 01 '19 at 08:08

1 Answers1

1

From the comments you make it clear that you are looking for a Delphi equivalent to the C# nameof function.

No such equivalent exists in Delphi, and the language does not have facilities for you to create it yourself. Instead you will need to name the method as a string literal in code.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490