-1

私は本当にここで立ち往生しており、助けに感謝します. サービスのリクエスト データ コントラクトとレスポンス データ コントラクトを作成しました。リクエスト DTO には、Cardnum、Id、および Noteline1----noteline18 が含まれます。応答 DTO には、noteline1 ~ noteline18 が含まれます。

リクエストデータメンバnoteLine1(データ長78文字)に文字長100の文字列を渡します。ここで、78 文字のみが noteline1 データ メンバーに入力され、残りがリクエスト DTO の他の空の noteline データ メンバーに収まるようにします。私は次のコードを使用しましたが、うまくいきました:

string requestNoteReason = request.noteLine1;
if (response != null)
{
    foreach (PropertyInfo reqPropertyInfo in requestPropertyInfo)
    {
        if (reqPropertyInfo.Name.Contains("noteLine"))
        {
            if (reqPropertyInfo.Name.ToLower() == ("noteline" + i))
            {
                if (requestNoteReason.Length < 78)
                {
                    reqPropertyInfo.SetValue(request, requestNoteReason, null);
                    break;
                }
                else
                {
                    reqPropertyInfo.SetValue(request, requestNoteReason.Substring(0, 78), null);
                    requestNoteReason = requestNoteReason.Substring(78, requestNoteReason.Length - 78);
                    i++;
                    continue;
                }
            }
        }
    }
    goto Finish;
}

ここで、78 文字を超える文字列を含む noteline1 を分割して、次の空の noteline に入力する必要があります。文字列の長さが 200 文字を超える場合は、文字列を分割し、次の連続する空のメモ行に入力する必要があります。たとえば、文字列に 3 つの空白のノートラインのスペースが必要な場合、次に連続する使用可能な空のノートライン (つまり、ノートライン 2、ノートライン 3、ノートライン 4) の残りの文字列のみを埋める必要があり、すでに使用されている文字列でノートラインを埋める必要はありません。前に埋めました。

助けてください

4

1 に答える 1

1
[TestFixture]
public class Test
{
    [Test]
    public void TestLongLength()
    {
        var s = new string('0', 78) + new string('1', 78) + new string('2', 42);
        var testClass = new TestClass();
        FillNoteProperties(s, testClass);

        Assert.AreEqual(new string('0', 78), testClass.NoteLine1);
        Assert.AreEqual(new string('1', 78), testClass.NoteLine2);
        Assert.AreEqual(new string('2', 42), testClass.NoteLine3);
    }

    public static void FillNoteProperties(string note, TestClass testClass)
    {
        var properties = testClass.GetType().GetProperties();
        var noteProperties = (from noteProperty in properties
                              where noteProperty.Name.StartsWith("NoteLine", StringComparison.OrdinalIgnoreCase)
                              orderby noteProperty.Name.Length, noteProperty.Name
                              select noteProperty).ToList();                
        var i = 0;
        while (note.Length > 78)
        {
            noteProperties[i].SetValue(testClass, note.Substring(0, 78), null);
            note = note.Substring(78);
            i++;
        }

        noteProperties[i].SetValue(testClass, note, null);
    }
}
于 2012-07-26T06:34:28.033 に答える