Reflection and static methods

  • Thread starter Thread starter Jon Turner
  • Start date Start date
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



}
}
 
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.
 
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
 
Back
Top