![]() |
AW: from C# to delphi
So many thanks for all your great help end valuable time .
:wink: i still need your help to port this code :
Code:
internal static void PrintValue(object value, Type valueType)
{ if (value == null) { Console.WriteLine(valueType); return; } var type = value.GetType(); switch (Type.GetTypeCode(type)) { case TypeCode.Int32: Console.WriteLine((int)value); break; case TypeCode.Int64: Console.WriteLine((long)value); break; case TypeCode.UInt32: Console.WriteLine((uint)value); break; case TypeCode.UInt64: Console.WriteLine((ulong)value); break; case TypeCode.Double: Console.WriteLine((double)value); break; case TypeCode.String: Console.WriteLine((string)value); break; case TypeCode.Boolean: Console.WriteLine((bool)value ? true : false); break; case TypeCode.DateTime: Console.WriteLine((DateTime)value); break; case TypeCode.Object: if (type.IsArray) if (value is byte[] bytes) Console.WriteLine(bytes); else if (value is Int128 int128) Console.WriteLine(int128); else if (value is Int256 int256) Console.WriteLine(int256); else if (value is IObject MyObject) Console.WriteLine(MyObject); else if (type.IsEnum) Console.WriteLine((uint)value); else break; default: break; } } |
AW: from C# to delphi
Zitat:
For example something simple like this works for many simple types:
Delphi-Quellcode:
You can add support for other data types that can be packaged into a Variant with a
procedure PrintValue(const V: Variant);
begin WriteLn(V); end; procedure TProcessor.TestPrintSimpleValues; const A: array of integer = [0,1,2,3,4]; begin Write('Byte: '); PrintValue(Byte($80)); Write('ShortInt: '); PrintValue(ShortInt($80)); Write('Word: '); PrintValue(Word($8045)); Write('SmallInt: '); PrintValue(SmallInt($8045)); Write('Integer: '); PrintValue(Integer(-124972)); Write('Cardinal: '); PrintValue(Cardinal(124972)); Write('Single: '); PrintValue(Single(128.45)); Write('Double: '); PrintValue(Single(128.45)); Write('String: '); PrintValue('This is just text.'); Write('Array: '); PrintValue(A); // The last one fails since a variant containing an array connot be converted to a string directly by VarToString. end;
Delphi-Quellcode:
inside PrintValue, but this is limited by what you can package into a Variant in a way that allows the type of the content to be identified. The big advantage is that Delphi does the packaging at the point of call for you, inferring the type from the value or variable you pass.
case VarType(V) of
varArray: (...); else WriteLn(V); end;
Delphi-Quellcode:
That would be the general approach using typeless parameters. In this case you need to explicitely pass the correct type information (obtained via the TypeInfo function) with the value to print and are settled with the task of converting to text for display yourself.
procedure PrintValue2(const Value; ValueType: PTypeInfo);
begin case ValueType^.Kind of // ...cast Value to the correct type for WriteLn. end; end; And finally there is TValue (System.RTTI unit), which has a number of implicit operator methods to package common data types into a TValue. And it has a ToString method that covers most of the conversion needs. But you may have to code some special cases yourself, like in the PrintValue2 procedure above.
Delphi-Quellcode:
You can use this like the procedure with the Variant parameter, at least for types that can be directly converted to a TValue and for which ToString works. For anything else more work is required again to get a string from the packaged data, especially for objects or records. It can be done but it's a lot of work...
procedure PrintValue3(const V: TValue);
begin WriteLn(V.ToString); end; |
AW: from C# to delphi
so many thanks for all your great help ,
What about this please :
Code:
internal static IObject ReadObject(uint constructorNb = 0)
{ var myDef = type.GetCustomAttribute<MYDefAttribute>(); var obj = Activator.CreateInstance(type, true); IEnumerable<FieldInfo> fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public); if (myDef.inheritBefore) fields = fields.GroupBy(f => f.DeclaringType).Reverse().SelectMany(g => g); ulong myflags = 0; boolFlagAttribute boolFlag; foreach (var field in fields) { if (((boolFlag = field.GetCustomAttribute<boolFlagAttribute>()) != null) && (myflags & (1UL << boolFlag.Bit)) == 0) continue; object value = field.FieldType; field.SetValue(obj, value); if (field.FieldType.IsEnum) if (field.Name == "myflags") myflags = (uint)value; else if (field.Name == "myflags2") myflags |= (ulong)(uint)value << 32; } return (IObject)obj; } |
AW: from C# to delphi
What does ChatGPT say to it?
|
AW: from C# to delphi
Zitat:
|
AW: from C# to delphi
I see. So ChatGpt is kinda like a multipurpose Mechanical Turk...
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 01:47 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz