首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我需要一个关于物业访问的复习课程

我需要一个关于物业访问的复习课程
EN

Stack Overflow用户
提问于 2010-05-27 00:46:39
回答 4查看 253关注 0票数 3

我需要在给定类中访问类属性的帮助。

例如,以以下类为例:

代码语言:javascript
复制
public partial class Account
    {
        private Profile _profile;
        private Email _email;
        private HostInfo _hostInfo;

        public Profile Profile
        {
            get { return _profile; }
            set { _profile = value; }
        }

        public Email Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public HostInfo HostInfo
        {
            get { return _hostInfo; }
            set { _hostInfo = value; }
        }

在"Account“类中有一堆类属性,比如Email或Profile。现在,当我想在运行时访问这些属性时,我会这样做(对于电子邮件):

代码语言:javascript
复制
    _accountRepository = ObjectFactory.GetInstance<IAccountRepository>();
    string username = Cryptography.Decrypt(_webContext.UserNameToVerify, "verify");
    Account account = _accountRepository.GetAccountByUserName(username);

    if(account != null)
    {
        account.Email.IsConfirmed = true;

但是,我得到"Object reference not set...“对于account.Email..。为什么会这样呢?如何访问Account,使account.Email、account.Profile等返回给定AccountId或UserName的正确数据。

代码语言:javascript
复制
    Here is a method that returns Account:

public Account GetAccountByUserName(string userName)
{
    Account account = null;

    using (MyDataContext dc = _conn.GetContext())
    {
        try
        {
            account = (from a in dc.Accounts
                       where a.UserName == userName
                       select a).FirstOrDefault();
        }
        catch
        {
            //oops
        }
    }

    return account;

}

上面的方法很有效,但是当我尝试的时候:

代码语言:javascript
复制
 account = (from a in dc.Accounts
               join em in dc.Emails on a.AccountId equals em.AccountId
               join p in dc.Profiles on em.AccountId equals p.AccountId
               where a.UserName == userName
               select a).FirstOrDefault();

我的电子邮件和配置文件仍收到对象引用异常

属性。这只是一个SQL问题,还是我需要执行其他操作才能完全访问Account类中的所有属性?

谢谢!

EN

Stack Overflow用户

发布于 2010-05-27 00:56:46

你得到这个是因为电子邮件是另一个尚未分配的类。你能做的就是在你的构造函数中默认将链接到其他类的属性作为新的项。例如,在ctor中:

代码语言:javascript
复制
public Account()
{
   // Set Defaults
   Email = new Email();
   Profile = new Profile();
   HostInfo = new HostInfo();
}

然后,您可以根据需要设置它们的值。

票数 3
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2915035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档