0

I have a class which i can read but not write because of the company policy. I have following structures in my project.

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;
    }
}

No I want to add extra property in class Name called "Email Address" and use class Details LoadName() method to return the type which includes the **Email Address** as well. as i am bound to use that method i can not change the return type Name to something else.

I can extend the class Name and overrides the GetNames() method to include the newly created property but that wont help as i can not change the return type in class Details LoadName() method.

I am not sure this is even possible BUT just wondering if there are any solutions.

I am suing
.NET 3
VS 2010
4

2 に答える 2

3

メソッドのシグネチャを変更できない場合、呼び出し側で何らかのキャストを行う必要があります。これは最適ではありませんが、それでも実行できます。

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);
}

最も難しい部分は、新しいプロパティをデータ グリッドにバインドすることです。この回答は、これを行う方法を説明しています。

于 2013-09-13T10:18:49.037 に答える
1

これを試して:

    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 に答える