AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

from C# to delphi

Ein Thema von sdean · begonnen am 22. Dez 2022 · letzter Beitrag vom 30. Jan 2023
Antwort Antwort
generic

Registriert seit: 24. Mär 2004
Ort: bei Hannover
2.415 Beiträge
 
Delphi XE5 Professional
 
#1

AW: from C# to delphi

  Alt 28. Dez 2022, 21:42
Hello can someone help me porting this C# code into Delphi
Why to convert that, when you can use that code directly.
You have many options to do this.
1) Create a webservice and call it from Delphi.
2) Create a COM-Server and use it from Delphi. With Delphi's import tlb tool you don't need to write any definitions and with dotnet RegAsm.exe you can generate a TLB file. If you don't want to register the COM Server you can also use SideBySide Configuration.
Coding BOTT - Video Tutorials rund um das Programmieren - https://www.youtube.com/@codingbott

Geändert von generic (28. Dez 2022 um 21:59 Uhr)
  Mit Zitat antworten Zitat
QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.889 Beiträge
 
Delphi 12 Athens
 
#2

AW: from C# to delphi

  Alt 29. Dez 2022, 13:26
@generic: I suspect he wants a native implementation to achieve a speed increase.
@sdean: I guess you want to convert a bunch of functions. Think about automating the process! At least a bit. Like dictionary style.

The other way could have been to use a decompiler of the .net assembly to decompile it into Delphi.net code or Oxygene Code. You could apply some finishing touches to this delphi like code and be done.
I'm just not sure if you have a version of Delphi that supports Delphi.net...I think Delphi.net went the way of Kylix? I also don't know if Oxygen is still alive???

How big is the C# library/assembly/Project you are currently converting?

If you just want to use a C# assembly in a Delphi project you might just want to use https://www.crystalnet-tech.com/runt...library4delphi
Andreas
Monads? Wtf are Monads?

Geändert von QuickAndDirty (29. Dez 2022 um 13:46 Uhr)
  Mit Zitat antworten Zitat
QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.889 Beiträge
 
Delphi 12 Athens
 
#3

AW: from C# to delphi

  Alt 29. Dez 2022, 14:40
A quick and dirty approach...my default type of approach...
Lines with comments, you have to check yourself.
I assume that u use the Rudy's TBiginteger project. But I'm not familiar with it.yet I know that he implemented all the operators.
Never tested or compiled the code.
I kept all the tranlations in the same line. so the code blocks aren't nicely indented and line breaks are missing, but they are in the same line like the original code that they represent.
I think you need Delphi 11 for Inline declarations to work.
Delphi-Quellcode:
      Function TMyClassHelper.IsProbablePrime(n:TBigInteger):boolean;
      Begin
         var n_minus_one:TBiginteger := n -1;
         if (n_minus_one.Sign <= 0) then Begin Result := false; Exit; end; // look for TBiginteger sign yourself

         var s:Integer := 0;
         var d:TBiginteger := n_minus_one;
         While d.IsEven Do Begin d.shr(1); inc(s); end;// look for TBiginteger IsEven yourself

         var bitLen:Integer := n.GetBitLength;// use code completion to find n.GetBitLength
         var randomBytes : TByteDynArray; SetLength(randomBytes, (bitlen div 8) +1);
         var lastByteMask:Byte := Byte( ((1 shl Integer(bitLen mod 8)) -1) );
         var a:TBigInteger;
         if (MillerRabinIterations < 15) then
            Begin Result := 15; Exit; End;
         for var i:Integer := 0 TO MillerRabinIterations-1 do
         Begin
            repeat
               Encryption.RNG.GetBytes(randomBytes); //??
               randomBytes[^1] &= lastByteMask; //??
               a := TBigInteger.Create(randomBytes);
            Until ( (a < 3) or (a >= n_minus_one) );
            a := a-1;

            var x:TBigInteger; x.ModPow(a, d, n);// Ithink TBigInteger.Pow(d,n) exists but for mod you might have to use the "mod" operator
            if (x = 1) or (x = n_minus_one) then continue;

            var r:Integer;
            for r := s -1 downto 1 do // looks weird "s-1 downto 1"
            Begin
               var x:TBigInteger; x.ModPow(x, 2, n);// Ithink TBigInteger.Pow(d,n) exists but for mod you might have to use the "mod" operator
               if x = 1 then Begin result := false; exit; end;
               if x = n_minus_one then break;
            end;
            if r = 0 then Begin Result := false; exit; end;
         end;
         result := true;
      end;
Andreas
Monads? Wtf are Monads?

Geändert von QuickAndDirty (29. Dez 2022 um 15:02 Uhr)
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#4

AW: from C# to delphi

  Alt 1. Jan 2023, 22:41
So many thanks for all your great help end valuable time .
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;
         }
      }
  Mit Zitat antworten Zitat
peterbelow

Registriert seit: 12. Jan 2019
Ort: Hessen
675 Beiträge
 
Delphi 11 Alexandria
 
#5

AW: from C# to delphi

  Alt 2. Jan 2023, 17:17
So many thanks for all your great help end valuable time .
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;
         }
      }
That does not directly translate to Delphi since simple types are not objects in Delphi, so you cannot write a method that can basically take any type of value this way. Delphi has run-time type information, though, it has support for typeless (anonymous) parameters (from which you cannot obtain the actual type of the passed values, unfortunately), it has support for Variant types ( derived from the OLE VARIANT type), and it has TValue. Depending on how this PrintValue method is used in the C# code you are trying to translate you could probably write an analog using one of these.

For example something simple like this works for many simple types:
Delphi-Quellcode:
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;
You can add support for other data types that can be packaged into a Variant with a
Delphi-Quellcode:
  case VarType(V) of
     varArray: (...);
  else
    WriteLn(V);
  end;
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.

Delphi-Quellcode:
procedure PrintValue2(const Value; ValueType: PTypeInfo);
begin
  case ValueType^.Kind of
    // ...cast Value to the correct type for WriteLn.
  end;
end;
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.

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:
procedure PrintValue3(const V: TValue);
begin
  WriteLn(V.ToString);
end;
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...
Peter Below

Geändert von peterbelow ( 2. Jan 2023 um 17:54 Uhr)
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#6

AW: from C# to delphi

  Alt 6. Jan 2023, 10:18
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;
      }
  Mit Zitat antworten Zitat
QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.889 Beiträge
 
Delphi 12 Athens
 
#7

AW: from C# to delphi

  Alt 27. Jan 2023, 10:06
What does ChatGPT say to it?
Andreas
Monads? Wtf are Monads?
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:02 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz