How can you get the current class name without using StackTrace?

Z

Zytan

Since the call stack can be changed in release, as functions are
inlined, how can you be certain to get the current class name of any
method that cares to know? Even IF it is an inlined method?

Seems impossible to do.

Zytan
 
Z

Zytan

How about this.GetType()?

Ok, that returns the namespace, as well, but that should be good
enough.

Thanks,

Zytan
 
Z

Zytan

How about this.GetType()?

Oh, wait, I had already tried that, and it didn't work because I need
it to work on static classes. There is no "this" for a static class.

Any more ideas?

Zytan
 
P

Paul McNamara

Here's an example of something I use in a static method (should work in a static class too).

public static Type[] GetKnownTypes()
{
Type thisType = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
return thisType.Assembly.GetTypes().ToList<Type>().Where(t => t.IsSubclassOf(thisType)).ToArray();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top