A couple of options:
If you're using C# 4, you could use dynamic typing, and split the method into a number of overloads:
public Uri GetUri(dynamic obj)
{
return GetUriImpl(obj);
}
private Uri GetUriImpl(Entry entry)
{
...
}
private Uri GetUriImpl(EntryComment comment)
{
...
}
In this case you'd probably want some sort of "backstop" method in case it's not a known type.
You could create a Dictionary>
:
private static Dictionary> UriProviders =
new Dictionary> {
{ typeof(Entry), value => ... },
{ typeof(EntryComment), value => ... },
{ typeof(Blog), value => ... },
};
and then:
public Uri GetUri(object obj)
{
Func
Note that this won't cover the case where you receive a subtype of Entry
etc.
Neither of these are particularly nice, mind you... I suspect a higher level redesign may be preferable.