How to treat an instance variable as an instance of another type in C#
I have a simple inheritance heirarchy with MyType2
inheriting from MyType1
.
I have an instance of MyType1
开发者_高级运维, arg
, passed in as an argument to a method. If arg
is an instance of MyType2
, then I'd like to perform some logic, transforming the instance. My code looks something like the code below.
Having to create a new local variable b
feels inelegant - is there a way of achieving the same behavior without the additional local variable?
public MyType1 MyMethod(MyType1 arg)
{
if(arg is MyType2)
{
MyType2 b = arg as MyType2;
//use b (which modifies "arg" as "b" is a reference to it)...
}
return arg;
}
Note that the "is" and "as" is duplicating the test; either use is
and then (once you know) just cast - or use as
in the first place and test for null
.
Re your issue; if you only want to do one thing - then cast:
if(arg is MyType2)
{
((MyType2)arg).SomeSpecialMethod();
}
Otherwise - perhaps a virtual
method (on the base-type), or just refactor the logic out into another method, so you have:
if(arg is MyType2)
{
StuffThatTakesType2((MyType2)arg);
}
But persoanlly, I'd just use the extra variable:
MyType2 whatever = arg as MyType2;
if(whatever != null) {
whatever.Foo = 123;
whatever.Bar();
}
No; you need to create a separate variable.
Also, your code performs more casts than necessary; it would be a little bit faster like this:
MyType2 b = arg as MyType2;
if(b != null)
{
//Use b
}
I would say option 1 is to put all the modifying code (your comment line) as a function of MyType2, and then do (arg as MyType2).Foo()
on it.
Option 2 is to implement MyMethod on both MyType1 and as an override in MyType2, and just call it without worrying. The MyType1 implementation may do nothing, which isn't the best design.
edit: technically not a violation of LSP since that only applies in the reverse of this situation (MyType2 shouldn't break behavior of MyType1).
精彩评论