I have a problem scenario like this:-
1) Listbox with values like Car, Scooter and Bike. A button to click.
<div>
<asp:ListBox ID="lst" runat="server">
<asp:ListItem Text="Bike" Value="Bike"></asp:ListItem>
<asp:ListItem Text="Car" Value="Car"></asp:ListItem>
<asp:ListItem Text="Scooter" Value="Scooter"></asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Invoke" />
</div>
2) Now i have three different class like below:-
class Car
{
static string getData()
{
return "I like cars";
}
}
class Bike
{
static string getData()
{
return "I like Bike";
}
}
class Scooter
{
static string getData()
{
return "I dont like scooter";
}
}
3) Now on the button click event handler “Button1_Click”, i want to call the getData() method based on the selected value from the listbox using the REFLECTION only.
Please help me out.
Type.GetTypegets your Type. You might want to prefix this with your namespaceGetMethodgets the getData methodInvokewill execute the method. Since the method is static, invoking the method with null will work. If it wasn’t static, null had to be replace with your instance of that object. Also, the second null serves as the parameters, which are none in this case