In an old project we are using a third party assembly with a class that has a property with some hardcoded information:
public string ConnectionString
{
get
{
string[] fullDbName = new string[5];
fullDbName[0] = "Data Source=";
fullDbName[1] = this.dbServer;
fullDbName[2] = ";Initial Catalog=";
fullDbName[3] = this.FullDbName;
fullDbName[4] = ";Integrated Security=SSPI;Pooling=false";
return string.Concat(fullDbName);
}
}
I need to be able to construct the connection string my self. So I have tried to make a derived class that hides the original property, but it does not seem to work:
public class SqlServerRestorerExstension : SQLServerRestorer
{
public SqlServerRestorerExstension(string dbServer, string dbName, string dbFilePath, string dbDataFileName, string dbLogFileName, bool detachOnFixtureTearDown, string connectionstring) : base(dbServer, dbName, dbFilePath, dbDataFileName, dbLogFileName, detachOnFixtureTearDown)
{
ConnectionString = connectionstring;
}
public string ConnectionString { get; private set; }
}
Is it possible do achive this in any way when I don’t have acces to the third party code?
As others have pointed out you can use the
newkeyword to hide the base member property. Note however that this doesn’t magically turn theConnectionStringproperty into a polymorphic function, i.e. if you have something like this:and you do this:
Then you will still see an “a” printed to the console. In fact the
newkeyword just stops the compiler from issuing a warning regarding the hiding of the member of the base class. It doesn’t change the behavior of the code at runtime.You can try to use a Decorator pattern and wrap the
SQLServerRestorer, but if that doesn’t work either, you are out of luck I am afraid.