我正在使用PayPal網絡SDK(可在nuget上使用)的一些代碼。我從用C#編寫的示例中解除了它。然後我使用了一個自動翻譯器(來自Telerik)將其轉換爲VB.net。譯者是不完美的,這裏是一些代碼,它不能處理:在VB.net中使用對象初始值設定項的鍵
items = new List<PayoutItem>
{
new PayoutItem
{
recipient_type = PayoutRecipientType.EMAIL,
Amount = New Currency
{
value = "0.99",
Currency = "USD"
},
receiver = "[email protected]",
note = "Thank you.",
sender_item_id = "item_1"
},
}
};
在看這個代碼片段,我認爲這是一個已命名的類的對象初始化,但是當我做了類搜索名爲「payoutitem」,我並不覺得,當我點擊右鍵並去定義,它告訴我,這是重建出來「元數據」,並給了我,開頭代碼:
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
namespace PayPal.Api
{
public class PayoutItem : PayPalSerializableObject
{
public PayoutItem();
所以我繼續使用翻譯到VB,並且也告訴我,'payoutItem'只作爲元數據存在。此外,它給了我一個錯誤,當我嘗試編譯的結果,那就是:
Dim payout = New Payout() With { _
key .sender_batch_header = New PayoutSenderBatchHeader() With { _
Key .sender_batch_id = "batch_" + System.Guid.NewGuid().ToString().Substring(0, 8), _
Key .email_subject = "You have a payment" _
}, _
Key .items = New List(Of PayoutItem)() From { _
New PayoutItem() With { _
Key .recipient_type = PayoutRecipientType.EMAIL, _
Key .amount = New Currency() With { _
Key .value = "0.99", _
Key .currency = "USD" _
}, _
Key .receiver = "[email protected]", _
Key .note = "Thank you.", _
Key .sender_item_id = "item_1" _
}, _
New PayoutItem() With { _
Key .recipient_type = PayoutRecipientType.EMAIL, _
Key .amount = New Currency() With { _
Key .value = "0.90", _
Key .currency = "USD" _
}, _
Key .receiver = "[email protected]", _
Key .note = "Thank you.", _
Key .sender_item_id = "item_2" _
}, _
New PayoutItem() With { _
Key .recipient_type = PayoutRecipientType.EMAIL, _
Key .amount = New Currency() With { _
Key .value = "2.00", _
Key .currency = "USD" _
}, _
Key .receiver = "[email protected]", _
Key .note = "Thank you.", _
Key .sender_item_id = "item_3" _
} _
} _
}
顯然具有第一「鍵」在這裏會導致問題,當我刪除它,我得到其他編譯問題。
任何人都可以向我解釋爲什麼一個類僅作爲元數據存在,即使它有一個名稱(PayoutItem),其次,爲什麼VB版本不能編譯? 謝謝。 Gordon
刪除「密鑰」。當您創建匿名類型時,對象初始值設定項只需要「Key」。 –
此外,我會建議創建構造函數,以便您可以直接傳遞屬性,顯着清理。 –
第一個塊中的代碼很可能是JSON。第二塊中的代碼進一步表明它最有可能是JSON。看看Newtonsoft.JSON來處理這些事情。 – Tim