我從來沒有使用過結構。我已經創建了一個簡單的示例,說明了我在下面要做的事情。我選擇struts的原因是因爲對象永遠不需要存在於類的上下文之外。謝謝。如何將結構實例賦值給包含該結構的類?
類
public class EmailAddress
{
public string Email { get; set; }
public string Name { get; set; }
}
public class EmailMessage
{
public EmailAddress To { get; set; }
public EmailAddress From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public struct Attachment
{
public string Name { get; set; }
public string Bas64 { get; set; }
}
嘗試方法
protected void MyMethod()
{
var myEmailMessage = new EmailMessage
{
To = { Email = "ToEmailAddress" },
From = { Email = "FromEmailAddress" }
};
var myAttachment = new EmailMessage.Attachment
{
Name = "AttachmentName",
Bas64 = "Base64String"
};
myEmailMessage.Attachment = myAttachment;
}
你得到什麼錯誤? – 2014-09-05 09:22:26
您的電子郵件沒有「附件」 – Sayse 2014-09-05 09:24:05
考慮閱讀[在類和結構之間選擇](http://msdn.microsoft.com/en-us/library/ms229017(v = vs.110).aspx )。我沒有理由在這裏使用'struct'。 – 2014-09-05 09:53:13