0

由于公司政策,我有一门课,我可以读但不能写。我的项目中有以下结构。

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其他类型。

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

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

I am suing
.NET 3
VS 2010
4

2 回答 2

3

If you cannot change the signature of the method, your callers would need to do some casting. This is not optimal, but you can still do it:

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

Now the caller would need to know the new type, do a cast, and access the email through it:

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

The trickiest part is binding the new property to a data grid. This answer explains how this can be done.

于 2013-09-13T10:18:49.037 回答
1

Try this:

    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;
        }
    }
于 2013-09-13T10:19:17.090 回答