Tech Off Thread

11 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Get Instantiating Class Name

Back to Forum: Tech Off
  • User profile image
    csharp1171

    Is it possible to get the name of the class that created an object?  For example, I have ClassA that creates ClassB.  ClassA then calls ClassB.Save().  In ClassB.Save() can I determine that ClassA is the class that called the Save() method?

  • User profile image
    odujosh

    I would avoid this because it causes large amount of coupling. Your better off passing metadata to make descisions you need to make based off that information as a parameter.

    Shorter answer: Not that I can think of if I was to ignore all the red flags your questions raises.

  • User profile image
    wkempf

    Generically, no (at least not with out some fragility).  For specific use cases, you could manage it through reflection and StackFrame objects.  But I agree that it's a better design to make something like this an explicit passage of meta data then to rely on reflection.

  • User profile image
    csharp1171

    Thanks to each of you for your input.  I was hoping there would be an easy way to do this.  Thanks.

  • User profile image
    Randolpho

    What are you trying to do, exactly? Maybe we can help architect a better solution.

  • User profile image
    csharp1171

    I've been tasked with adding audit logging to a set of objects that are currently used in a web app.  Eventaully the objects will be used by other applications.  This includes the datetime, userid, recordId, type of edit (insert, update, delete), and the source (in this case the web page).  They don't want to know what was changed just that objectA.Save() was called from WebPageA.aspx by UserA.  Currently the user id making the insert, update, delete, is not passed into the stored procedure and they don't want to add any triggers to the database.  The objects are in a separate project from the web application so my intent was to modify just the objects without needing to touch the web pages.  My first thought is to either add additional parameters to the save/delete methods for each object that includes the user id and source or creating a CreateAuditLog method that is then placed in each web page where needed.  Any other thoughts?

  • User profile image
    Randolpho

    Ok, so you have some middle-tier objects that are called by ASP.NET web pages, which in turn call stored procedures on a database, correct? And you want to add logging to the middle-tier objects such that they include in the log the user id and the page from which they were used, correct?

    What types of objects are they? Can they get access to the current request context in any way? You could use that to get the user id and URL

  • User profile image
    csharp1171

    Yes it is middle-tier objects.  For now I can get the current http context but in the future these objects will be used by other types of applications such as web service, windows service, etc. 

  • User profile image
    Ion Todirel

    csharp1171 wrote:
    Is it possible to get the name of the class that created an object?  For example, I have ClassA that creates ClassB.  ClassA then calls ClassB.Save().  In ClassB.Save() can I determine that ClassA is the class that called the Save() method?

        static class A
        {
            public static void CallSaveOnB() { B.Save(); }
        }    

        static class B
        {
            public static void Save()
            {
                StackFrame[] frames = new StackTrace().GetFrames();
                if (frames.Length < 2 || frames[1].GetMethod().DeclaringType != typeof(A))            
                    throw new InvalidOperationException();            
            }
        }

  • User profile image
    Secret​Software

    why cant you send the object as a parameter?

    Save (object sender)...


    then when you call
    ClassB.Save (this);

    so you pass the sender as a parameter?

  • User profile image
    Yggdrasil

    Ion Todirel wrote:

    StackFrame[] frames = new StackTrace().GetFrames();


    You should not rely on the StackFrames collection when running production code. When you're compiling your app in the Release configuration the compiler and the JIT can optimize your code, which includes inlining methods.
    The method calls you see in your code are not the ones you get during runtime, and this will break any code that relies on jumping x stackframes backwards.

    More info on MSDN:
    "StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization."

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.