Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查列表框中的项目类型_C#_.net - Fatal编程技术网

C# 如何检查列表框中的项目类型

C# 如何检查列表框中的项目类型,c#,.net,C#,.net,需要找出列表框中元素的类型,无论类型是按钮、单选按钮还是简单字符串 下面的片段中有一些东西: foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource) { if _item is of type of button //DO this else i

需要找出列表框中元素的类型,无论类型是按钮、单选按钮还是简单字符串

下面的片段中有一些东西:

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
                    {
                        if _item is of type of button 
                            //DO this
                        else if _item is  typeof RadioButton
                            //Do that
                    }
只要做:

if( item is Button )
{
  // Do something
}
else if( item is RadioButton )
{
  // Do something
}
只要做:

if( item is Button )
{
  // Do something
}
else if( item is RadioButton )
{
  // Do something
}

只需使用
就是

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource) 
{ 
    if _item is Button  
        //DO this 
    else if _item is RadioButton 
        //Do that 
} 

只需使用
就是

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource) 
{ 
    if _item is Button  
        //DO this 
    else if _item is RadioButton 
        //Do that 
} 

如果您只想检查类型,请像其他问题建议的那样使用is关键字

如果您真的想使用该项作为该类型,那么通常最好使用作为关键字。这会进行检查,但会在施法后为您提供实际项目以供使用,并将防止您在使用is然后as时收到fxcop警告

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
{
    Button b = _item as Button;
    if (b != null) { // DO this }

    RadioButton rb = _item as RadioButton;
    if (rb != null) { // DO that }
}
例如,如果您想知道类型,而不管类型是什么(而不是局限于某些控件),那么可以使用GetType()方法

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
{
    Type t = _item.GetType();
}

如果您只想检查类型,请像其他问题建议的那样使用is关键字

如果您真的想使用该项作为该类型,那么通常最好使用作为关键字。这会进行检查,但会在施法后为您提供实际项目以供使用,并将防止您在使用is然后as时收到fxcop警告

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
{
    Button b = _item as Button;
    if (b != null) { // DO this }

    RadioButton rb = _item as RadioButton;
    if (rb != null) { // DO that }
}
例如,如果您想知道类型,而不管类型是什么(而不是局限于某些控件),那么可以使用GetType()方法

foreach (ListBoxItem _item in listPhotoAlbum.ItemsSource)
{
    Type t = _item.GetType();
}
这个怎么样:

foreach (var _item in listPhotoAlbum.Items)
{
    var radioButton = _item as RadioButton;

    if (radioButton != null)
    {
        //Do with radioButton
        continue;
    }

    var button = _item as Button;

    if (button != null)
    {
        //Do with button
        continue;
    }
}
这个怎么样:

foreach (var _item in listPhotoAlbum.Items)
{
    var radioButton = _item as RadioButton;

    if (radioButton != null)
    {
        //Do with radioButton
        continue;
    }

    var button = _item as Button;

    if (button != null)
    {
        //Do with button
        continue;
    }
}