在c#

时间:2017-08-21 23:06:00

标签: c#

我得到“对象引用未设置为对象的实例”。以下是我的情景: 我有以下课程

public class Attachments
     {
            public string AName { get; set; }
            public int AID { get; set; }
           }

在另一个类中,我声明了一个如下所示的属性

     public class RetrieveEDetails
        {
                public List<Attachments> attachment { get; set; }
           public string FromEmail { get; set; }        
            public string ReplyTo { get; set; }         
            public string SendTo { get; set; }  
     public bool HasAttachments { get; set; }     
         }

在另一个类中,我正在设置这样的值,但我收到错误消息"Object reference not set to an instance of an object."

    public class testAttachment
    {
    RetrieveEDetails dtls = new RetrieveEDetails ();
    dtls.FromEmail ="t@g.com';
    if(dtls.HasAttachments )
    {//actually this is in loop
     Attachments att= new MailAttachments();
               att.AID =  1,
               att.AName = "test.txt";
               dtls.attachment.Add(att);//error here 
    }
    }

最后我想显示类似这样的结果:

    EmailDeatils:[{{FromEmail ="t@g.com',
                 ...................
                 ...................}
                 Attachment:{aname =1,aid='test.txt'}},........]

请建议我怎么办呢。

1 个答案:

答案 0 :(得分:0)

public List<Attachments> attachment { get; set; } = new List<Attachments>();