これは Delphi Prism 用です。
たとえば、バイナリ ファイルに保存したい次の列挙型 SET があるとします。
Fruit = (Apple, Banana, Mango, Cherry, Grapes, BlueBerry);
Fruits = set of Fruit;
FruitBasket:Fruits;
with Fruit do
FruitBasket := [Apple, Mango];
BinaryWriter thefile := new BinaryWriter(File.Create("test.dat"));
thefile.write(FruitBasket); //<<< raises Error - There is no overloaded method "Write" with these parameters
thefile.Close;
BinaryWriter を使用して enum SET Type(Fruit) をバイナリ ファイルに読み書きするにはどうすればよいですか? 別の Stackoverflow の質問で自分の質問に対する答えが見つかったと思っていましたが、見つかりませんでした。
ほとんどの場合、その要素をループする必要があると思いますが、もっと簡単な方法があるかどうかを知る必要がありました。
更新:最初の回答の後、私は試してみてすぐに結論に達しましたが、これは私の側の大きな間違いでした. プログラム内の他の問題やエラーを整理すると、CK による最初の回答と唯一の回答で提案されているように、行った変更に対してコンパイラがエラーを発生させました。私は書くことしかできませんが、それを読み返すことはできません。コンパイラは、「型が一致しません。System.SByte を Groups.TFeature のセットに割り当てることができません」と言い続けます。
上のコードは一例です。以下が実際のコードです。
列挙型は次のとおりです。
TFeature = (tfUnit,tfSignal,tfAlarm,tfControl,tfMaker,tfViewer,tfTrend,
tfComm,tfSystem,tfScan,tfShutdown,tfPID,tfMagiKal);
SET OF タイプは次のとおりです。
TFeatures = set of TFeature;
クラスは次のとおりです。
TGroup = class
name:string;
rwFeatures:TFeatures;
roFeatures:TFeatures;
levels:TLevels;
private
public
constructor;
Method ReadAGrp(bgreader:BinaryReader);
Method ReadOld(bgreader:BinaryReader);
Method WriteAGrp(bgwriter:BinaryWriter);
end;
TGroupList = class(ArrayList)
private
public
Method ReadGroups(fname:string);
Method WriteGroups(fname:string);
Method AddGroup(group1:TGroup);
Method DeleteGroup(group1:TGroup);
Method FindGroup(gname:string):TGroup;
end;
Binarywriter を使用してバイナリ ファイルに SET OF 型を読み書きしようとしている方法は次のとおりです。
procedure TGroup.ReadAGrp(bgreader:BinaryReader);
begin
name:=bgreader.ReadString;
rwFeatures := TFeature(bgreader.ReadSByte);
roFeatures := TFeature(bgreader.ReadSByte);
levels := TLevels(bgreader.readsbyte);
end;
procedure TGroup.ReadOld(bgreader:BinaryReader);
begin
name:=bgreader.ReadString;
rwfeatures := TFeature(bgreader.ReadSByte);
roFeatures := TFeature(bgreader.ReadSByte);
levels :=TLevels(bgreader.readsbyte);
end;
procedure TGroup.WriteAGrp(bgwriter:BinaryWriter);
begin
bgwriter.Write(name);
bgwriter.Write(rwFeatures.toarray);
bgwriter.Write(roFeatures.ToArray);
bgWriter.Write(levels.toarray);
end;
サンプルコードまたは実際のコードで答えていただければ幸いです。
お気づきでない場合は、この質問に対して報奨金を開始しました。私は本当に実用的な答えが必要です。ありがとう、
ありがとう、