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?
In C#, a method in a derived class can have the same name as a method in the base class. You can specify how the methods interact by using the new and override keywords. The override modifier extends the base class virtual method, and the new modifier hides an accessible base class method.
Property overriding means redefining or modifying the property of the base class in its derived class. Thus, it can be only achieved in inheritance. The property that needs to be overridden should be open in nature, in the base class. We cannot override a method that is final in nature.
override (C# reference)An override method provides a new implementation of the method inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. An override method must have the same signature as the overridden base method.
Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This is known as function overriding in C++. The function in derived class overrides the function in base class.
As others have pointed out you can use the new
keyword to hide the base member property. Note however that this doesn't magically turn the ConnectionString
property into a polymorphic function, i.e. if you have something like this:
public class A
{
public string CString { get { return "a"; } }
}
public class B : A
{
public new string CString { get { return "b"; }}
}
and you do this:
A a = new B();
Console.WriteLine(a.CString);
Then you will still see an "a" printed to the console. In fact the new
keyword 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.
You will need to indicate that you want to 'replace' this property, using new
:
public new string ConnectionString
{
get { return "My custom connection string"; }
}
Obviously you can extend that to implement your own set
, even if just to utilise auto-implemented accessors. Documentation on 'versioning' with new
can be found here, but specifically:
Using the new keyword tells the compiler that your definition hides the definition contained in the base class. This is the default behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With