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

  • SEARCH
  • Home
  • 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 164329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:43:29+00:00 2026-05-11T11:43:29+00:00

Regardless of whether or not this is a good idea, is it possible to

  • 0

Regardless of whether or not this is a good idea, is it possible to implement an interface where the executing function is aware of the calling object’s type?

class A {    private C;     public int doC(int input)    {       return C.DoSomething(input);    } }  class B {    private C;     public int doC(int input)    {       return C.DoSomething(input);    } }  class C {    public int DoSomething(int input)    {       if(GetType(CallingObject) == A)       {          return input + 1;       }       else if(GetType(CallingObject) == B)       {          return input + 2;       }        else       {          return input + 3;       }    } } 

It seems to me like this is a bad coding practice (because the parameters don’t change, but the output would) but aside from that is it possible?

I’m in a situation were I want a few specific types to be able to call a certain function, but I can’t exclude access to the function. I thought about having a ‘type’ parameter

DoSomething(int input, Type callingobject) 

But there’s no guarantee that the calling object would use GetType(this), as opposed to GetType(B) to spoof a B regardless of their own type.

Would this be as simple (relatively simple) as examining the callstack?

  • 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-11T11:43:30+00:00Added an answer on May 11, 2026 at 11:43 am

    First, yes, it’s a terrible idea to do this and breaks all kinds of solid design principles. You should definitely consider an alternative approach if that’s open, like simply using polymorphism—this seems like it can be refactored to a pretty clear case of single dispatch.

    Secondly, yes, it’s possible. Use System.Diagnostics.StackTrace to walk the stack; then get the appropriate StackFrame one level up. Then determine which method was the caller by using GetMethod() on that StackFrame. Note that building a stack trace is a potentially expensive operation, and it’s possible for callers of your method to obscure where things are really coming from.


    Edit: This comment from the OP makes it pretty clear that this could probably be a generic or polymorphic method. @devinb, you might want to consider making a new question that provides more detail about what you’re trying to do, and we can see if it lends itself well to a good solution.

    The short version is that I would end up have 30 or 40 identical functions that were simply off by one or two lines. – devinb (12 secs ago)

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

Sidebar

Related Questions

I currently have this query running on my website, public function searchCandidates($type=null, $gender=null, $age=null)
Regardless of whether my machines's root web config (the one in Windows/Microsoft.NET/...) contains system.web/pages/namespaces/add
Does a custom control (ascx) get rendered regardless of whether visibility property is true
Apparently the following is valid in c# 4.0 regardless of the type of the
I'm trying to make a small program which can tell whether or not the
Does anyone know why this will not compile? I've tried both VS 2008 and
I'm trying to edit this js code so that the autocomplete function kicks in
I want to write URL that work regardless whether the base URL is either
I've got an app that has all content regardless of language displaying content in
I'm trying to create an unique array regardless of its original order and using

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.