Hallo
Ich bin gerade dabei mal wieder etwas aus C++ in C# zu Binden und bin auf das Problem gestoßen des Bit-Fields in C#
Normal kann ich via masken und shiften mein Bits an die Stelle setzten wo ich will.
Nun bin ich kein Fan von hin und herschieben magischer werten die am Ende irendwo sitzen, sonden würde dem Ganzen gern einen Namen geben wie in C++
Mein erster gedanke war: ich bauche ein "struct".
Nun bin ich kein Fan von Properties in Structs.
So googelte ich etwas und fand (github.com/dotnet/csharplang/discussions/465) eine Feature request zu den ganzen Thema.
Enttäuschent musste ich festellen, das dieses nicht existiert
Also Kam ich zu diesen projekt codeproject.com/Articles/10955…ld-in-Csharp-using-struct
Was eig cool ist aber, ich kann nicht vom endnutzer verlangen das er Konvertierungsmethoden aufrufen soll.
Zum eig.!!
Ich hab das ganze Projekt etwas kleiner gemacht (im ersten sinne)(Sieh unten) und würde/bin dabei ein Code-Analysis mit Code-Fix zu schreiben.
Heißt: Ich durch suche mein QuellCode nach Structs welche Member mit dem Attributs "FieldOffset" und "BitFieldInfo" haben.
Wenn das fertig ist, starte ich ein Tracking der Structs.
Schau wo die Structs deklariet werden und ermittel den Scope.
Schau im Scope nach weiteren aufrufen und nach Inits.
Sollte ich ein Init finden würde ich per Code-Fix die Anweisung umschreiben in e.g.:
Was haltet ihr davon oder würdet ihr was anders machen?
Spoiler anzeigen
Ich bin gerade dabei mal wieder etwas aus C++ in C# zu Binden und bin auf das Problem gestoßen des Bit-Fields in C#
Normal kann ich via masken und shiften mein Bits an die Stelle setzten wo ich will.
Nun bin ich kein Fan von hin und herschieben magischer werten die am Ende irendwo sitzen, sonden würde dem Ganzen gern einen Namen geben wie in C++
Mein erster gedanke war: ich bauche ein "struct".
Nun bin ich kein Fan von Properties in Structs.
So googelte ich etwas und fand (github.com/dotnet/csharplang/discussions/465) eine Feature request zu den ganzen Thema.
Enttäuschent musste ich festellen, das dieses nicht existiert
Also Kam ich zu diesen projekt codeproject.com/Articles/10955…ld-in-Csharp-using-struct
Was eig cool ist aber, ich kann nicht vom endnutzer verlangen das er Konvertierungsmethoden aufrufen soll.
Zum eig.!!
Ich hab das ganze Projekt etwas kleiner gemacht (im ersten sinne)(Sieh unten) und würde/bin dabei ein Code-Analysis mit Code-Fix zu schreiben.
Heißt: Ich durch suche mein QuellCode nach Structs welche Member mit dem Attributs "FieldOffset" und "BitFieldInfo" haben.
Wenn das fertig ist, starte ich ein Tracking der Structs.
Schau wo die Structs deklariet werden und ermittel den Scope.
Schau im Scope nach weiteren aufrufen und nach Inits.
Sollte ich ein Init finden würde ich per Code-Fix die Anweisung umschreiben in e.g.:
Was haltet ihr davon oder würdet ihr was anders machen?
C#-Quellcode
- /// <summary>
- /// Specifies the length of each bit field
- /// </summary>
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
- public sealed class BitFieldInfoAttribute : Attribute
- {
- /// <summary>
- /// Initializes new instance of BitFieldInfoAttribute with the specified field offset and length
- /// </summary>
- /// <param name="offset">The offset of the bit field</param>
- /// <param name="length">The number of bits the bit field occupies</param>
- public BitFieldInfoAttribute(byte offset, byte length){}
- }
- [StructLayout(LayoutKind.Explicit, Size = sizeof(int), Pack = 1)]
- struct Example
- {
- [FieldOffset(0)]
- [BitFieldInfo(0, 1)]
- public int BitOne;
- [FieldOffset(0)]
- [BitFieldInfo(1, 2)]
- public int BitTwo;
- [FieldOffset(0)]
- [BitFieldInfo(3, 1)]
- public int BitThree;
- }