C# What is the best way to determine the type of an inherited interface class?

Posted by Martijn on Stack Overflow See other posts from Stack Overflow or by Martijn
Published on 2010-12-24T17:50:14Z Indexed on 2010/12/24 17:54 UTC
Read the original article Hit count: 167

Filed under:
|

In my application I work with criterias. I have one base Criteria interface and and other interfaces who inherits from this base interface:

               ICriteria
                  |
                  |
        ----------------------
         |                  |
    ITextCriteria        IChoices

What I'd like to know is, what is the best way to know what Type the class is?

In my code I have a dropdown box and based on that I have to determine the type:

// Get selected criteria
var selectedCriteria = cmbType.SelectedItem as ICriteria;

if (selectedCriteria is IChoices)
{
    //selectedCriteria = cmbType.SelectedItem as IChoices; Doesn't work
    IChoices criteria = selectedCriteria as IChoices;//cmbType.SelectedItem as IChoices;

    SaveMultipleChoiceValues(criteria);

    //_category.AddCriteria(criteria);
}
else
{
    //ICriteria criteria = selectedCriteria; //cmbType.SelectedItem as ICriteria;

    if (selectedCriteria.GetCriteriaType() == CriteriaTypes.None)
    {
        return;
    }

    //_category.AddCriteria(criteria);
}

_category.AddCriteria(selectedCriteria);

selectedCriteria.LabelText = txtLabeltext.Text;

this.Close();

My question is, is this the best way? Or is there a better way to achieve this?

The chance is big that there are coming more interfaces based on ICriteria.

© Stack Overflow or respective owner

Related posts about c#

Related posts about interface