Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 77119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:46:49+00:00 2026-05-10T20:46:49+00:00

OK so I’m looking a some code which looks roughly like this: void DoSomething(object

  • 0

OK so I’m looking a some code which looks roughly like this:

void DoSomething(object o) {     if (o is Sometype1) {      //cast o to Sometype and do something to it     }     else if (o is Sometype2) {     //cast o to Sometype2 and do something to it     }     ...     else if (o is SometypeN) {     //cast o to SometypeN and do something to it     } } 

Now one approach would be to make all the objects o that are used as parameters implement an interface like

interface ICanHaveSomethingDoneToMe {     //expose various properties that the DoSomething method wants to access } 

But the problem with this is that I don’t want all my objects to implement this interface – the logic for the do something method doesn’t really belong with them. What pattern should I be using to deal with this?

I suspect something like a series of implementations of

interface IPropertiesForDoingSomethingTo<T> {     //expose various properties that the DoSomething method wants to access } 

might be better. I’d have an implementation for each of the object types that I want to do it to, but then I have this new problem. I would at point need to have a method like

IPropertiesForDoingSomethingTo<T> GetPropsGeneric(T t); 

but is this going to need to have a massive switch on it? Should I define a class with loads of methods like

IPropertiesForDoingSomethingTo<Someobject1> GetProps(Someobject1 t); ... IPropertiesForDoingSomethingTo<Someobject1> GetProps(SomeobjectN t); 

This has the problem compared to the generic version that you wouldn’t be able to add new types at runtime. Ts there something cunning one can do with a DI container in GetPropsGeneric to resolve the container? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:46:50+00:00Added an answer on May 10, 2026 at 8:46 pm

    any time you see a switch statement (or a series of if-statements) that are checking the type of an object, this is a Big Red Flag for a missing base class or interface. In other words, the code should be relying on polymorphism, not testing the object type

    if you cannot change the base class or implement an interface, you’re probably left with a dictionary to simulate dynamic dispatching. In C# you could use an anonymous delegate for the method that included the cast

    as for the property access, if the properties don’t conform and accessing via reflection is not an option, you may need to extract the property values in the method/delegate above and pass them to a generic function instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer By default there is no way but it's not difficult… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer First of all, you are using the == operator to… May 11, 2026 at 8:22 pm
  • Editorial Team
    Editorial Team added an answer When you use a StateServer or SQL Server session state… May 11, 2026 at 8:22 pm

Related Questions

Ok so I thought it was fixed, but I'm getting totally inconsistent results. I
Ok so I've ordered Applying Domain-Driven Design and Patterns: Using .Net , but while
OK so I'm looking a some code which looks roughly like this: void DoSomething(object
Ok so I have an abstract base class called Product, a KitItem class that
Ok so I just did something boneheaded. I'm setting up a subversion server for

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.