0

パブリック変数を含まないオブジェクトがありますが、データを保持するだけです(他の会社のAPI、変更できるものではありません)。彼らは私にバイトマップを提供し、どの値がどのくらいの長さで、どこにあるかを教えてくれます。

私が途方に暮れているのは、これらのフィールドにアクセスする方法です。大学ではmemcpyなどを使ってたくさんのことをしましたが、CLIではそれが正しい方法だとは思えません...

このオブジェクトから情報のバイトを抽出するための最良の方法は何ですか?

以下は、私が達成したいことの簡単な疑似コードです。これを構文的に行うための最良の方法がわかりません。私はCLIと.Netにあまり精通しておらず、これを実行するための良い方法が必要だと考えています...

int GetSingleDataPoint(int LowestByte,int NumOfBytes, Object^ DataRecord)
Copy the NumOfBytes starting at DataRecord[LowestBytes] to a temporary integer
return temporary integer

この説明で重要な場合、データはリトルエンディアンでパックされ、1〜4バイトの長さの符号付き変数と符号なし変数が含まれます。

4

2 に答える 2

0

If the objects being provided are CLI objects then generally it is not appropriate to directly access the bytes. I believe it is possible using Marshalling, however it would be easier to just use reflection to access the private/protected members. For instance:

Object^ getFieldByName(Object^ obj, String^ name, Type^ t)
{
    return t->GetField(name, BindingFlags::Public | BindingFlags::NonPublic | BindingFlags::Instance)->GetValue(obj);
}

If it's a native object, just do the following (basically what you suggested in the question):

int GetSingleDataPoint(int LowestByte,int NumOfBytes, void* object)
{
    int result;
    size_t numBytes;

    numBytes = NumOfBytes;
    if(numBytes > sizeof(int))
        numBytes = sizeof(int);

    memcpy(&result, object+LowestByte, numBytes);

    return result;
}
于 2012-10-12T22:11:34.760 に答える
0

最良のアプローチは、.NET プロジェクト内で C++ ネイティブ コードを使用し、素敵なmemcpyまたは型キャスト (さらに高速) を使用することだと思います。

int n1 = * (unsigned char*) (DataRecord + LowestBytes);
int n2 = * (int16_t*) (DataRecord + LowestBytes);
int n4 = * (int32_t*) (DataRecord + LowestBytes);

.NETでは使用する必要がMarshalあり、クラスはネイティブC++クラスであるため、地獄のようなものです!!

于 2012-10-12T22:14:17.937 に答える