How can get the owner of my class ??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m writing a class that need to know the type or name of the class which
created me.
It can be done if the owner will pass his this, but I would like to do it
without the owner intervention.
Assuming the owner is a regular class (not a Windows class), is it possible
???
 
Sharon,

You will have to pass the owner's name through the constructor, or have
a property/method that you can call which sets it. Objects generally don't
keep track of this particular characteristic.

Hope this helps.
 
Assuming you are not using a factory to create the instance, in your constructor put the following code:

StackFrame sf = StackFrame(1); // captures thte stack frame from the calling method
Type owningType = sf.GetMethod().ReflectedType;

Its not the most blisteringly fast code but will do what you want without involving the caller.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

I'm writing a class that need to know the type or name of the class which
created me.
It can be done if the owner will pass his this, but I would like to do it
without the owner intervention.
Assuming the owner is a regular class (not a Windows class), is it possible
???
 
Hi Richard,

It's not exactly good for me.

I created an exception class ‘expA’ derived from ApplicationException.
And there is an exception class ‘expB’ derived from ‘expA’.

Now class C application is throwing ‘expB’.
I want ‘expB’ to know how the class is the ctreated and throw him.

After trying you lines, I get the class ‘expA’ and it’s taking very very
long time.
 
using System;
using System.Diagnostics;

class App
{
static void Main(string[] args)
{
try
{
throw new MyDerivedException("Hello");
}
catch
{
}
}
}

class MyBaseException : ApplicationException
{
public MyBaseException(string msg)
:base(msg)
{
}
}

class MyDerivedException : MyBaseException
{
public MyDerivedException(string msg)
:base(msg)
{
StackFrame sf = new StackFrame(1);
Console.WriteLine(sf.GetMethod().ReflectedType.FullName);

}
}

Prints out "App" for me. Can you post a short and complete example that demonstrates what you are seeing. If its an exception, the fact that using the StackFrame is slow should be irrlevant as it should only get called rarely in exceptional situations. I ran this without the debugger and its not obviously very slow.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Hi Richard,

It's not exactly good for me.

I created an exception class &lsquo;expA' derived from ApplicationException.
And there is an exception class &lsquo;expB' derived from &lsquo;expA'.

Now class C application is throwing &lsquo;expB'.
I want &lsquo;expB' to know how the class is the ctreated and throw him.

After trying you lines, I get the class &lsquo;expA' and it's taking very very
long time.
 
I played with it and I found that if you change it to from 1 to 2 as follow,
it will work:

StackFrame sf = new StackFrame(2);
Type ownerType = sf.GetMethod().ReflectedType;
string ownerName = ownerType.ToString();

If you still think you me to post some code, just say so.
But your example is like mine.

Have a nice weekend.
 
Ah, I bet your code was in the base contructro not the deruved. The derived constructor calls the base constructor so there was an extra method in the call stack - hence you need 2 rather than 1 as the parameter too the stackframe ctor. The problem is the deeper your class heirarchy goes you'll need different values to pass to the strackframe ctor

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I played with it and I found that if you change it to from 1 to 2 as follow,
it will work:

StackFrame sf = new StackFrame(2);
Type ownerType = sf.GetMethod().ReflectedType;
string ownerName = ownerType.ToString();

If you still think you me to post some code, just say so.
But your example is like mine.

Have a nice weekend.
 
Back
Top