繁体   English   中英

如何在不更改C#中的返回类型的情况下向类添加属性

[英]how to add properties to class without changing return type in c#

由于公司政策的原因,我有一个可以阅读但不能写作的课程。 我的项目中有以下结构。

public class Name                    // can not touch this class OR modify it
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  public string GetNames()
  {
        return FirstName + " " + LastName;
  }
}

public class Details
{
  // some methods and properties.

    public Name LoadName()          // This method return type i can not change as this method is used in ObjectDataSource for GridView
    {
        var names = new Name();
        if (txtInpput.Text == "Jermy")
        {
              names.FirstName = "Jermy";
              names.LastName = "Thompson";
        }
        else
        {
              names.FirstName = "Neville";
              names.LastName = "Vyland";
        }

        return
        names;
    }
}

不,我要添加额外的财产class Name叫做"Email Address"和使用class Details LoadName()方法返回其中包括类型**Email Address**为好。 由于我必须使用该方法,因此我无法将返回类型Name更改为其他Name

我可以扩展class Nameoverrides GetNames()方法以包括新创建的属性,但这无济于事,因为我无法在class Details LoadName()方法中更改返回类型。

我不确定这是否有可能,但只是想知道是否有解决方案。

I am suing
.NET 3
VS 2010

如果您无法更改方法的签名,则调用者将需要进行一些强制转换。 这不是最佳选择,但是您仍然可以这样做:

public class NameWithEmail : Name {
    public string EMail {get;set;}
}
...
public Name LoadName() {
    ...
    return new NameWithEmail(); // OK because NameWithEmail extends Name
}

现在,呼叫者将需要知道新的类型,进行强制转换并通过它访问电子邮件:

NameWithEmail name = Details.LoadName() as NameWithEmail;
if (name != null) {
    Console.WriteLine("{0} {1} : {2}", name.FirstName, name.LastName, name.EMail);
}

最棘手的部分是将新属性绑定到数据网格。 这个答案解释了如何做到这一点。

尝试这个:

    public class Name                    // can not touch this class OR modify it
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string GetNames()
        {
            return FirstName + " " + LastName;
        }
    }
    public class Name1:Name {
        public string EmailAddress { get; set; }
        public override string GetNames()
        {
            return FirstName + " " + LastName+" "+EmailAddress;
        }

    }
    public class Details
    {
        // some methods and properties.

        public Name LoadName()          // This method return type i can not change as this method is used in ObjectDataSource for GridView
        {
            TextBox txtInpput = new TextBox();
            var names = new Name();
            if (txtInpput.Text == "Jermy")
            {
                names.FirstName = "Jermy";
                names.LastName = "Thompson";
            }
            else
            {
                names.FirstName = "Neville";
                names.LastName = "Vyland";
            }

            return
            names;
        }
    }
    public  class Details1:Details {
        public override Name LoadName()          
        {
            TextBox txtInpput = new TextBox();
            var names = new Name();
            if (txtInpput.Text == "Jermy")
            {
                names.FirstName = "Jermy";
                names.LastName = "Thompson";
            }
            else
            {
                names.FirstName = "Neville";
                names.LastName = "Vyland";
            }

            return
            names;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM