1

I need to know how to get the owning instance of a TableAdapter if I just have the Table Adapter.

So, for example, there is this class:

public class example 
{
   public User _user;
   public TableAdapter _adapter;

   someMethods();
   .
   .
   .
}

I work with the aspect-oriented framework PostSharp. For logging purposes I have a logging class with a method that gets executed when the get_Adapter method is called. So, in my logging class I get the TableAdapter as an argument. What I need in the end is the User object.

Unfortunately, I cannot change anything in the design of the class I need to get, so all I have is this TableAdapter. My logging class looks like this (simplified):

public class logger
{
   public override void OnExit(MethodExecutionArgs args)
   {
      TableAdapter = (TableAdapter)args.Instance;

      //here I need the example object in order to get the current user object
   }
}

Is there any way to do this? Reflection, maybe? Anything?

Thanks for help in advance.

Oh, by the way, I work with C# and WinForms.

wchargin
  • 15,589
  • 12
  • 71
  • 110
TheNewGuy
  • 23
  • 3
  • So you also cannot change the `TableAdapter`? – Marnix May 07 '12 at 12:21
  • Unfortunately not. I tried to simplify my question as much as possible. The real scenario is: In the system I work, the example class which holds the user is the base class of several dozen classes - all these classes use one or more table adapters. So I would have to change over 100 table adapters. – TheNewGuy May 07 '12 at 12:34
  • 1
    @TheNewGuy I do not believe what your asking is possible. I would suggest as long as you have to go and change all these table adapters, re-factor the code into a proper layered architecture so you can avoid these issues in the future. – asawyer May 07 '12 at 12:37
  • 1
    To clarify, you aren't looking for the parent class (vice base class) as your title eludes; you're looking for the owning instance, something that holds a reference to the object. – roken May 07 '12 at 12:45
  • Where are you applying to the aspect, to the TableAdapter or to the user classes? is get_Adapter an accessor method? Is logger the aspect or is it a regular class? – Dustin Davis May 07 '12 at 12:45
  • To All: thanks for your answers so far. @DustinDavis: I apply to the tableAdapter. get_Adapter is an accesor method and the logger is the aspect – TheNewGuy May 07 '12 at 12:49
  • How many of these `example` instances do you have active when this method is called? Wouldn't you be able to check all of them and see which one of them has an `_adapter` that ReferenceEquals the TableAdapter you have here? – Mr Lister May 07 '12 at 12:50
  • Are you able to modify your TableAdapter? I believe you can specify your own base class for the TableAdapter, where you could store a reference the the 'example' that holds the TableAdapter. In a similar vein, the TableAdapter will be a partial class, so you could extend it that way to hold your reference back to 'example.' – roken May 07 '12 at 12:51
  • @MrLister: Well, that is the problem: I dont know how to access my example instances. roken, I´m not supposed to make changes. The decision to use aspect oriented programming for logging was made cause we added logging when the system already was pretty big. So, all I have is this stupid Table Adapter ;) – TheNewGuy May 07 '12 at 12:58

1 Answers1

0

As far as postsharp goes, you won't be able to get to it without some type of intermediary such as additional aspect or injecting into the tableadapter instance, a reference to it's host class.

You can try to walk the stack and try to get it that way.

Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
  • Walk the stack sounds acceptable to me, I´m already doing this anyway to get some other information. I´m already at the point where I get the type information of my example class as well, but I don´t know how to get the instance. As far as I know from other threads (I investigated the way via StackFrame already) this is not possible – TheNewGuy May 07 '12 at 13:03
  • @TheNewGuy It might not be, I was throwing it out there. You might be better off with an intermediary. It's more work and makes things complicated but IMO better than walking the stack. However, you may just want to rethink your approach to not have so much complexity. Maybe create an adapter class for TableAdapter and require it to be instantiated with the instance of it's host. Cleaner and simpler. – Dustin Davis May 07 '12 at 14:03