Maybe a bit of a philosophical, but should I generally, in C#, expect a property with an interface type to retain its assigned class?
For example a class property like this:
public IBehavior Behavior { get; set; }
that gets assigned a implmented class
instance.Behavior = new ImplementedBehavior();
should i generally expect this cast to succeed?
Assert.IsNotNull(instance.Behavior as ImplementedBehavior);
Or is it lost, and i should have kept a reference to the instantiated ImplementedBehavior
instead?
var ib = new ImplementedBehavior();
instance.Behavior = ib;
Assert.IsNotNull(ib as ImplementedBehavior);
Clarification:
Looking to use an external class, that when assigned a class similar to what is done above, changes the content of instance.Behavior
internally after i set it, to return an instance of SomeOtherBehavior
Can I assume this is bad behavior and not in the spirit of C#?
GitHub issue related to the question: https://github.com/aws/aws-cdk/issues/19013
CodePudding user response:
Can I expect a property to retain its assigned class?
No.
It may not even be accepted in the first place, e.g.
public IBehavior Behavior {
get {...}
set { if (!value.HasCapabilityX) return; ... };
}
It may simply be changed by some other code that has access to the same object (instance
):
Thread A:
instance.Behavior = new ImplementationA();
Thread B:
instance.Behavior = new ImplementationB();
There are object oriented design patterns that promote the idea of changing the object, e.g. the decorator pattern and proxy pattern, e.g.
public IBehavior Behavior {
get {...}
set { _behavior = new PermissionsDecorator(new LoggingDecorator(value)); };
}
Should I generally expect this cast to succeed?
No.
Or is it lost [...] ?
Maybe.
and I should have kept a reference to the instantiated ImplementedBehavior instead?
If you need something that ImplementedBehavior
can do which IBehavior
can't do, then yes, keep a reference.
Can I assume this is bad behavior and not in the spirit of C#?
This is not bad behavior. It is good practice to only expect the interface to fulfill the contract that was specified by the interface.
CodePudding user response:
The property can be assigned any value that implements IBehavior
:
public class BehaviorA : IBehavior { }
public class BehaviorB : IBehavior { }
...
instance.Behavior = new BehaviorA();
instance.Behavior = new BehaviorB();
So in general, you CANNOT expect a property's value to be of a particular type that derives from the property's declared type.
CodePudding user response:
this
Assert.IsNotNull(instance.Behavior as ImplementedBehavior);
will work so long as you dont change instance.Behavior
, it wont happen spontaneously. But if you also have
class Imp2 : IBehavior{
}
and then do
instance.Behavior = new Imp2();
then that assert will fail.
CodePudding user response:
Let me give you a simple example using
public interface IType
{
string Text { get; set; }
int Number { get; set; }
}
public class MyClass
{
public IType MyProperty { get; set; }
}
public class MyFirstType : IType
{
public string Text { get; set; }
public int Number { get; set; }
public char Letter { get; set; }
}
public class MySecondType : IType
{
public string Text { get; set; }
public int Number { get; set; }
public decimal Price { get; set; }
}
And here is the usage with comments that explain what's going on:
var instance = new MyClass(); // creates a new object of type "MyClass" in memory
var myFirstType = new MyFirstType(); // creates a new object of type "MyFirstType" in memory
var mySecondType = new MySecondType(); // creates a new object of type "MySecondType" in memory
// setting property
instance.MyProperty = myFirstType; // MyProperty is now simply referencing "myFirstType" using the address to its position in memory
instance.MyProperty = mySecondType; // The memory address to "myFirstType" is now replaced by the memory address to "mySecondType"
// printing values of MyProperty
Console.WriteLine(instance.MyProperty.Text); // will work.
Console.WriteLine(instance.MyProperty.Number); // will work.
Console.WriteLine(instance.MyProperty.Letter); // compiler error. IType does not specify this "Letter" property.
Console.WriteLine(instance.MyProperty.Price); // compiler error. IType does not specify this "Price" property. However, it is in memory on the object, but the compiler will not infer the type of the property unless the property is explicitly casted.
// casting
var convertedPropertyA = (MyFirstType)instance.MyProperty; // throws an exception since the property is currently set to an object of type "MySecondType".
var convertedPropertyB = (MySecondType)instance.MyProperty; // works because the property is set to an object of type "MySecondType".
// modifying the values of the property
instance.MyProperty.Text = "new text"; // this works
instance.MyProperty.Number = 123; // this works
((MySecondType)instance.MyProperty).Price = 14.99; // this works
Console.WriteLine(instance.MyProperty.Text); // prints "new text"
Console.WriteLine(instance.MyProperty.Number); // prints "123"
Console.WriteLine(((MySecondType)instance.MyProperty).Price); // prints "14.99"