Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

c# - Is there a way to use a property with same name but is of different type in derived class?

I have legacy code using the BaseClass and the code is expecting customerid to be of type int. Then I have a requirement to create a new class, DerivedClass, which behaves very much like BaseClass but now customerid needs to be a string. The legacy code can't be modified so that testing is not needed.

How do I get the effect I want using inheritance (or any other way)?

Linqpad test code below illustrates what I need to do. Obviously it won't compile because customerid in DerivedClass needs to be int. I need a string property called customerid as well. It needs to be that name because other code will use this class instead of BaseClass and expects the same named property to be of type string.

public  class  BaseClass
{
    public virtual int customerid {get; set;}

    public void printname()
    {
       customerid = 1;
       customerid.Dump();
    }
}

public class DerivedClass : BaseClass
{
    public override string customerid {get; set;}

    public void PrintCustomerID()
    {
        customerid = "jshwedeX"; 
        customerid.Dump();
    }
}



void Main()
{
    DerivedClass dc = new DerivedClass();
    dc.printname();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the new modifier like this:

public class BaseClass
{
    public virtual int customerid {get; set;}

    public void printname()
    {
       customerid = 1;
       customerid.Dump();
    }
}

public class DerivedClass : BaseClass
{
    public new string customerid {get; set;}

    public void PrintCustomerID()
    {
        customerid = "jshwedeX"; 
        customerid.Dump();
    }
}

This will give you the desired results, but it will also hide the property on the base class. If you referenced an instance of DerivedClass as a BaseClass variable, you would only be able to reference the property on the base class; not the derived class.

In other words:

BaseClass instance = new DerivedClass();
string customerId = instance.customerid; // <- this won't compile

An alternative would be to use explicit interface implementations:

public interface IBase
{
    int customerid { get; set; }
}

public interface IDerived
{
    string customerid { get; set; }
}

public class Derived : IBase, IDerived
{
    int IBase.customerid { get; set; }
    string IDerived.customerid { get; set; }
}

When your instance of Derived is stored in a variable of type IBase, customerid will resolve to the int version, and when it is stored in a variable of type IDerived, it will resolve to the string version:

var derived = new Derived();
IBase ibase = derived;
IDerived iderived = derived;
int id1 = ibase.customerid; // <- compiles just fine
string id2 = iderived.customerid; // <- compiles just fine

You could also use casting:

var instance = new Derived();
int id1 = ((IBase)instance).customerid;
string id2 = ((IDerived)instance).customerid;

Keep in mind that explicit interface implementations cause the implemented members to not be visible unless the variable is of the interface type:

var instance = new Derived();
var customerid = instance.customerid; // <- this won't compile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...