• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Determining calling type in a base class

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    #11
    If you want to know if any given type implements an interface you can:

    (C#)

    PHP Code:
    if (typeof(IWhateverable).IsAssignableFrom(myType)) 
    where myType is the type you want to check.

    Comment


      #12
      Originally posted by 3ill
      Not sure what it is in VB.Net.. but in C# you should be able to accomplish this by using Reflection
      As Mr/Ms 3ill says, you can find just about everything you need via reflection. Sometimes perhaps in a round-about way. and there may be a performance penalty (but then that's what caching/pooling is for), but one way or another you can achieve what you want.

      Certainly so far as 'private' is private goes it just doesn't exist in .NET...
      Last edited by Joe Black; 18 December 2006, 19:41.

      Comment


        #13
        I used this to return the calling assembly name for a bizTalk Orchestration. [n] = the number of levels up you want to go. The namespace your after is System.Diagnostics and System.Reflection. If you can't convert this to VB.NET, then you need a slap in the head!!!!

        StackTrace stackTrace = new StackTrace();
        StackFrame stackFrame = stackTrace.GetFrame([n]);
        MethodBase methodBase = stackFrame.GetMethod();
        return methodBase.DeclaringType.FullName;
        Last edited by Weltchy; 19 December 2006, 09:10.

        Comment


          #14
          Originally posted by DimPrawn
          Don't mean to rain on your parade, but what screwed up design would need a base class to know anything about which derived class called it?

          How can a base class ever be expected to know what future derived classes there might be and how should it know how to behave differently in each case?

          Sounds like a bad case of arse about face design.

          I've used it for application logging and management, so I know what calling method and assembly fell over, which then gets fed out to managed email groups, depending on the calling orchestration/application.

          Comment


            #15
            Originally posted by Weltchy
            I used this to return the calling assembly name for a bizTalk Orchestration. [n] = the number of levels up you want to go. The namespace your after is System.Diagnostics and System.Reflection. If you can't convert this to VB.NET, then you need a slap in the head!!!!

            StackTrace stackTrace = new StackTrace();
            StackFrame stackFrame = stackTrace.GetFrame([n]);
            MethodBase methodBase = stackFrame.GetMethod();
            return methodBase.DeclaringType.FullName;


            Wading through whats where in reflection just gets so boring.

            When invoked from a static declaringtype seems to be nothing when it's a derived class

            But I'm probably just looking in the wrong place

            Still, I have for the time being implemented a fairly grotesque hack in the calling code which lets me achieve what I want.

            Cheers,

            Comment


              #16
              Tell me about it. Reflection is one of those things that is really useful to know, but something you'll only use once or twice a year!!!

              So I take it that you've got a static method in the concrete class that inherits from the base, and for some reason the stack trace isn't returning any frames?

              Or have you overridden the base class's method?

              Comment


                #17
                Originally posted by Weltchy
                Tell me about it. Reflection is one of those things that is really useful to know, but something you'll only use once or twice a year!!!
                Butting into the conversation, but if that's the case how do you implement an AbstractFactory in the .NET world? Surely you make use of one of those much more than once a year, it's one of the most fundamental OO patterns after the Singleton.
                Listen to my last album on Spotify

                Comment


                  #18
                  I'll rephrase my original comment to, one of those things you only do something interesting in once or twice a year!!!!

                  Comment


                    #19
                    Originally posted by Weltchy
                    I'll rephrase my original comment to, one of those things you only do something interesting in once or twice a year!!!!
                    Listen to my last album on Spotify

                    Comment


                      #20
                      Originally posted by Weltchy
                      Tell me about it. Reflection is one of those things that is really useful to know, but something you'll only use once or twice a year!!!

                      So I take it that you've got a static method in the concrete class that inherits from the base, and for some reason the stack trace isn't returning any frames?

                      Or have you overridden the base class's method?
                      If I recall correctly (and I might not)

                      - All the method were static
                      - A derived class was being invoked as a result of a call from a static method method on the derived class
                      - All I really wanted to find out was the class that was calling.

                      But I must admit to having rather lost the will to persue it any further.

                      Comment

                      Working...
                      X