gpt4 book ai didi

c# - 在函数中键入变量

转载 作者:太空宇宙 更新时间:2023-11-03 18:22:36 24 4
gpt4 key购买 nike

美好的一天!这个问题看起来很简单,但我想不出怎么做。我会将 Type 参数传递给函数并检查变量是否具有该类型。

void foo(Type type = ) // how to pass here default value for example System.Object?
{
if (elem.GetType() is type)
{

}
}
}

编辑我有下一节课

class Element {}
class ChildElementClass : Element {}
class SecondChildEleementClass : Element{}

并且有一个 Elements[] 数组,它存储了所有三个类的元素

Element[] elements;

因此,我将检索 ChildElementClass 的所有元素。我用其他方式做到了,但这很有趣。

最佳答案

使用这个:

void foo(Type type = null)
{
if (type == null)
type = typeof(object);
}

参见:here

关于c# - 在函数中键入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47074469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com