How to get calling object type in static method of Base Class

M

Mirek Endys

Hello all,

another problem im solving right now. I badly need to get typeof object that
called static method in base classe. I did it by parameter in method Load,
but i thing there should be way, how to get this information in called
method.
Thanks.

public abstract class BaseClass
{
public static BaseClass Load(SqlConnection sConnection)
{
// ****************************************
// here is code for loading data from SqlDB
// ****************************************

// look at ???callingObjectType???
BaseClass newItem =
(BaseUDCObject)System.Activator.CreateInstance(???callingObjectType???, new
string[] { Convert.ToString(dataReader["ID"]),
Convert.ToString(dataReader["Description"]) });

return newItem;

}
}

public class MyClass : BaseClass
{
private string _ID = null;
private string _Description = null;

public MyClass(string id, string description)
{
this._ID = id;
this._Description = description;
}
public static MyClass Load(SqlConnection sqlConn)
{
return (MyClass)BaseClass.Load(sqlConn)
}
}
 
W

Wiebe Tijsma

Static methods are not meant to address inheritance-like problems, maybe
you can use a singleton with instance methods instead?
 
M

Mirek Endys

Is the true, that i cannot get type of calling class?... i this case
typeof(MyObject) .

Wiebe Tijsma said:
Static methods are not meant to address inheritance-like problems, maybe
you can use a singleton with instance methods instead?


Mirek said:
Hello all,

another problem im solving right now. I badly need to get typeof object
that called static method in base classe. I did it by parameter in method
Load, but i thing there should be way, how to get this information in
called method.
Thanks.

public abstract class BaseClass
{
public static BaseClass Load(SqlConnection sConnection)
{
// ****************************************
// here is code for loading data from SqlDB
// ****************************************

// look at ???callingObjectType???
BaseClass newItem =
(BaseUDCObject)System.Activator.CreateInstance(???callingObjectType???,
new string[] { Convert.ToString(dataReader["ID"]),
Convert.ToString(dataReader["Description"]) });

return newItem;

}
}

public class MyClass : BaseClass
{
private string _ID = null;
private string _Description = null;

public MyClass(string id, string description)
{
this._ID = id;
this._Description = description;
}
public static MyClass Load(SqlConnection sqlConn)
{
return (MyClass)BaseClass.Load(sqlConn)
}
}
 
J

Jon Skeet [C# MVP]

Mirek Endys said:
another problem im solving right now. I badly need to get typeof object that
called static method in base classe. I did it by parameter in method Load,
but i thing there should be way, how to get this information in called
method.

No, there is no other way. Pass it in as a parameter.
 
G

Guest

Hi,
you can use the StackTrace class in the System.Diagnostics namespace for
this. You'd get the type of the calling object like this:

StackTrace trace = new StackTrace();
trace.GetFrame(1).GetMethod().ReflectedType

The trace object consists of individual frames, each one being one entry on
the stack. Frame 0 is your method, Frame 1 is you calling method, etc...
These calls are expensive (i.e. slow) though, so use with care :)

I think it is bad design though to take different execution paths in a
method depending upon the object calling it.
 
J

Jon Skeet [C# MVP]

pali said:
you can use the StackTrace class in the System.Diagnostics namespace for
this. You'd get the type of the calling object like this:

StackTrace trace = new StackTrace();
trace.GetFrame(1).GetMethod().ReflectedType

The trace object consists of individual frames, each one being one entry on
the stack. Frame 0 is your method, Frame 1 is you calling method, etc...
These calls are expensive (i.e. slow) though, so use with care :)

They're also not guaranteed to be accurate - inlining may remove
frames, for example.
I think it is bad design though to take different execution paths in a
method depending upon the object calling it.

Yup.
 

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