尝试强制转换为子类型,如果没有';t工作强制转换到不同的子类型c#
我正在尝试创建一个函数,该函数可以接受实际上是Child1、Child2或Child3类型的类Parent的参数。我想根据孩子的类型做一些不同的事情,除了下面我要展示的以外,我想不出其他方法来做。这对我来说是错误的,所以任何关于更好的方法的建议都将不胜感激尝试强制转换为子类型,如果没有';t工作强制转换到不同的子类型c#,c#,inheritance,C#,Inheritance,我正在尝试创建一个函数,该函数可以接受实际上是Child1、Child2或Child3类型的类Parent的参数。我想根据孩子的类型做一些不同的事情,除了下面我要展示的以外,我想不出其他方法来做。这对我来说是错误的,所以任何关于更好的方法的建议都将不胜感激 public static bool DoStuff(Parent parent) { try { Child1 child = parent as Child1; DoStuffChild1(
public static bool DoStuff(Parent parent)
{
try
{
Child1 child = parent as Child1;
DoStuffChild1(child);
}
catch (Exception)
{
try
{
Child2 child = parent as Child2;
DoStuffChild2(child);
}
catch(Exception)
{
try
{
Child3 child = parent as Child3;
DoStuffChild3(child);
}
catch (Exception)
{
HandleError();
}
}
}
}
您只需使用
is
关键字即可:
public static bool DoStuff(Parent parent)
{
try
{
if (parent is Child1)
{
Child1 child = parent;
DoStuffChild1(child);
}
else if (parent is Child2)
{
Child2 child = parent;
DoStuffChild2(child);
}
else if (parent is Child3)
{
Child3 child = parent;
DoStuffChild3(child);
}
}
catch (Exception)
{
HandleError();
}
}
is如果实例位于继承树中,则返回true
此外,以下是关于类型检查的一个非常好的答案:
您只需使用
is
关键字即可:
public static bool DoStuff(Parent parent)
{
try
{
if (parent is Child1)
{
Child1 child = parent;
DoStuffChild1(child);
}
else if (parent is Child2)
{
Child2 child = parent;
DoStuffChild2(child);
}
else if (parent is Child3)
{
Child3 child = parent;
DoStuffChild3(child);
}
}
catch (Exception)
{
HandleError();
}
}
is如果实例位于继承树中,则返回true
此外,以下是关于类型检查的一个非常好的答案:
如果C#7不是一个选项,此方法将适用于旧版本的C#
不要使用
try
/catch
作为控制流。永远
如果需要根据对象的类别执行不同的操作,可以使用typeof
和.GetType()
。在你的例子中
public static bool DoStuff(Parent parent)
{
if (parent.GetType() == typeof(Child1))
{
Child1 child = parent as Child1;
DoStuffChild1(child);
}
else if (parent.GetType() == typeof(Child2))
{
Child2 child = parent as Child2;
DoStuffChild2(child);
}
else if (parent.GetType() == typeof(Child3))
{
Child3 child = parent as Child3;
DoStuffChild3(child);
}
else
{
HandleError();
}
}
这是一个简单的处理方法
在FaizanRabbani的答案中,在
上使用此方法的原因是该方法是一种严格的类型检查,忽略了超类-例如:
if (parent is Parent)
将计算true
,但
if (parent.GetType() == typeof(Parent))
例如,如果它是Child1
、Child2
或Child3
,则将评估false
。如果C#7不是一个选项,此方法将适用于旧版本的C
不要使用try
/catch
作为控制流。永远
如果需要根据对象的类别执行不同的操作,可以使用typeof
和.GetType()
。在你的例子中
public static bool DoStuff(Parent parent)
{
if (parent.GetType() == typeof(Child1))
{
Child1 child = parent as Child1;
DoStuffChild1(child);
}
else if (parent.GetType() == typeof(Child2))
{
Child2 child = parent as Child2;
DoStuffChild2(child);
}
else if (parent.GetType() == typeof(Child3))
{
Child3 child = parent as Child3;
DoStuffChild3(child);
}
else
{
HandleError();
}
}
这是一个简单的处理方法
在FaizanRabbani的答案中,在上使用此方法的原因是该方法是一种严格的类型检查,忽略了超类-例如:
if (parent is Parent)
将计算true
,但
if (parent.GetType() == typeof(Parent))
例如,如果它是Child1
、Child2
或Child3
,则将评估false
。如果您使用的是C#7.0或更高版本,您可以使用以下代码实现所需的功能:
if (parent is Child1 child1)
{
DoStuffChild1(child1);
}
else if (parent is Child2 child2)
{
DoStuffChild2(child2);
}
希望这对您有所帮助。如果您使用的是C#7.0或更高版本,您可以通过以下代码实现所需功能:
if (parent is Child1 child1)
{
DoStuffChild1(child1);
}
else if (parent is Child2 child2)
{
DoStuffChild2(child2);
}
希望这有帮助。C#7是一种选择吗?模式匹配功能当然是比这更好的解决方案。即使排除了这一点,为什么还需要异常处理?如果强制转换失败,那么child应该是null
。您完全正确,这是错误的。我不仅会在一次代码审查中拒绝这一点,还会带你回去枪毙你;)查找typeof
和.GetType()
-如果你愿意,我会写一个涵盖这些选项的答案。是的,就像@Evantimboli说的,这不会像你期望的那样工作,因为强制转换失败只会导致赋值为null,而不是异常。我知道这是可行的,因为Visual Studio需要在此处尝试捕获“as”强制转换,以防无法将其强制转换到子类。然而,这是如此令人厌恶,我从来没有提交任何形式的审查@邪恶的杰米我几乎想把自己带回去,因为我想到了这件事。谢谢你的回答。C#7是一个选项吗?模式匹配功能当然是比这更好的解决方案。即使排除了这一点,为什么还需要异常处理?如果强制转换失败,那么child应该是null
。您完全正确,这是错误的。我不仅会在一次代码审查中拒绝这一点,还会带你回去枪毙你;)查找typeof
和.GetType()
-如果你愿意,我会写一个涵盖这些选项的答案。是的,就像@Evantimboli说的,这不会像你期望的那样工作,因为强制转换失败只会导致赋值为null,而不是异常。我知道这是可行的,因为Visual Studio需要在此处尝试捕获“as”强制转换,以防无法将其强制转换到子类。然而,这是如此令人厌恶,我从来没有提交任何形式的审查@邪恶的杰米我几乎想把自己带回去,因为我想到了这件事。谢谢你的回答。注意:@rufus-l对这个问题的评论是我完全感谢你的原因。我有可用的C#7。下层选民愿意解释这个答案的错误吗…?注意:@rufus-l对这个问题的评论是我接受try/catch out completely谢谢你的原因。我有C#7可用。下面的投票人是否愿意解释这个答案的错误…?您甚至可以在后面添加一个变量名来自动创建变量<代码>如果(父母是Child1){DoStuffChild1(Child1);}
@BlakeThingstad我相信这在C#7是真的?或者如果你不在C#7,你可以做:如果(父母是Child1){DoStuffChild1(Child1)父母)}
@EvilGeniusJamie是的,据我所知,这是模式匹配的一部分。在《谢谢》中有一些讨论。我处理的唯一错误是它不是任何可能的类型。我应该澄清一点。这就是我想要的。你甚至可以在之后添加一个变量名来自动创建变量<代码>如果(父母是Child1){DoStuffChild1(Child1);}
@BlakeThingstad我相信这在C#7是真的?或者如果你不在C#7,你可以做:如果(父母是Child1){DoStuffChild1(Child1)父母)}
@EvilGeniusJamie是的,据我所知,这是模式匹配的一部分。在《谢谢》中有一些讨论。我处理的唯一错误是它不是任何可能的类型。我应该澄清一点。T