TOO MANY if (obj is thisObj) statements
<p>I currently have method which is trying to find out what the obj is it recieved. It knows is on a certain interface, for example IService but I have code which looks at it and tries to tell me is it is for example Service1 or Service2.
I currently a lot of if(obj is thisObj) style statements, what would be the best solution to make this code pretty?</p>

<p>here is a sample of what exactly I have:</p>

<pre><code>    public void DoSomething(IService service)
    {
        if (service is Service1)
        {
            //DO something
        }

        if (service is Service2)
        {
            //DO something else
        }           
    }
</code></pre>

<p>now having two isnt too much of a bad thing,  but I am looking at having probably 20+ of these which just becomes awful to use.</p>

<p>Any ideas?</p>

<hr>

<p>ok further details I think are needed and here they are:</p>

<p>prior to this method I have another method which is recieving a xml doc, which it them deserializes into the interface IService, so we have something like this:</p>

<pre><code>    private static void Method(InnerXml)

    {

            var messageObj = (IServiceTask)XmlSerialization.Deserialize(typeof(IServiceTask), InnerXml);

            var service = GetService(messageObj);
            service.PerformTask(xmlDoc);

    }

    private static IService GetService(IServiceTask messageObj)
    {
        var service = new IService ();

        if (messageObj is Task1)
        {
            service = (SomeService)messageObj;
        }

        if (messageObj is Task2)
        {
            service = (SomeOtherService)messageObj;
        }
        return service ;
    }
</code></pre>

<p>Hopefully that makes it a bit clearer.</p>
