Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a method using reflection

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi, i'm trying to build my own pager control. I have added a property called DataSourceID which allows me to access the ObjectDataSource on the page. However in my control i need to call the "SelectCountMethod" from the type "TypeName" specified by the object data source to return the number of rows. I understand the way to do this is to use reflection but i have no idea where to start.

Really appreciate it if someone could help. Thanks
 
Code:
//sample class
public class MyClass
{
    public int GetSomeInt(){ return 4; }
}

//to call a method of an unknown object
object mysteryObject = new MyClass();
string methodName = "GetSomeInt";
MethodInfo info = typeof(MyClass).GetMethod(methodName);
int returnValue = (int)info.Invoke(mysteryObject, null);

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Slight correction to make it more dynamic:

Code:
object mysteryObject = new MyClass();
string methodName = "GetSomeInt";
MethodInfo info = typeof([b]mysteryObject.GetType()[/b]).GetMethod(methodName);
int returnValue = (int)info.Invoke(mysteryObject, null);



MCP, MCTS - .NET Framework 2.0 Web Applications
 
Slight other correction to get the code to compile. :)

Code:
MethodInfo info = mysteryObject.GetType().GetMethod(methodName);

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top