Reflection and static methods


J

Jon Turner

I am trying to read the attributes thru reflection in a method that is
static to a class. my function
GetList in the base class need to retrieve the attrutes from the derived
class. Is there a way
to accomplish this ?


Many Thanks in Advance.




Example

[TestAttribute("TableName")]
class MyClass: BaseClass
{
}


class BaseClass
{
static BaseClass GetList()
{
// here is where I want to read the attributes of MyClass, but
because
// this is static, I can't use the this.GetType() to retrieve
[Type] so
// I can retrieve the attbutes:

Type myType = this.GetType(); //Won't work



}
}
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Jon,

You could always create a constant for the name. However, it would
require you to maintain it.

I would create an instance of the StackFrame class, and then call
GetMethod on the instance for the current stack frame. Once you have the
MethodBase class, you can call the ReflectedType property to get the type
that the method belongs to (if you use DeclaringType, it will give you the
type that a method is declared on, not the type that the method is on).

Hope this helps.
 
Ad

Advertisements

A

Alexander Shirshov

Jon,

AFAIK, you can't do that. You'll need to redesign your base class so that
the functionality of GetList resides in an instance method.

HTH,
Alexander
 

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