Create a delegate at run time
-
Hi, I want to let the user to insert a string that will create a function. I know how to create a class or method at runtime. I have a delegate and I want the function that the user been created will be stored in the delegate event that i use. How can I do it? Specification The user can select the sorted function that he want's(Sort of some system class). He gets a list with this functions for example: 1. Area 2. Length 3. Custom If the user choose Area/length then the list of item that I have (very complex system) will be sorted by Area/Length but if the user choose 'Custom' then I want to show him a new Textbox and then he can write 'Area*Length +COS(Length)'. This is my delegate:
delegate double GetGrade(SystemD SD);
GetGrade MyGrade;1. Area:
MyGrade = new GetGrade(AreaGrade);
public double AreaGrade(SystemD SD)
{
return SD.Area;
}2. Length:
MyGrade = new GetGrade(LengthGrade);
public double LengthGrade(SystemD SD)
{
return SD.Length;
}3. Custom:
MyGrade = new GetGrade(CustomGrade);
public double CustomGrade(SystemD SD)
{
string str= CustomTextBox.Text;
double Grade= CompileAndGetDoubleNumber(str,SD); //This is the function that i want to implement.
// The function that i want gets the string that contain the code for calculation, and SD that represent the system(class instance). It's return the wanted value after the calculation, for example from the string '2*SD.Length+SD.Area' i will get a double that represent the value (2*Length +Area) of SD.
return Grade;
}I want to get the string from the user and create at runtime a function that can be overload to this event but I need this function before to compilation because then I can not choose the function for the event because the function does not exists. What can I do to resolve this problem?
-
Hi, I want to let the user to insert a string that will create a function. I know how to create a class or method at runtime. I have a delegate and I want the function that the user been created will be stored in the delegate event that i use. How can I do it? Specification The user can select the sorted function that he want's(Sort of some system class). He gets a list with this functions for example: 1. Area 2. Length 3. Custom If the user choose Area/length then the list of item that I have (very complex system) will be sorted by Area/Length but if the user choose 'Custom' then I want to show him a new Textbox and then he can write 'Area*Length +COS(Length)'. This is my delegate:
delegate double GetGrade(SystemD SD);
GetGrade MyGrade;1. Area:
MyGrade = new GetGrade(AreaGrade);
public double AreaGrade(SystemD SD)
{
return SD.Area;
}2. Length:
MyGrade = new GetGrade(LengthGrade);
public double LengthGrade(SystemD SD)
{
return SD.Length;
}3. Custom:
MyGrade = new GetGrade(CustomGrade);
public double CustomGrade(SystemD SD)
{
string str= CustomTextBox.Text;
double Grade= CompileAndGetDoubleNumber(str,SD); //This is the function that i want to implement.
// The function that i want gets the string that contain the code for calculation, and SD that represent the system(class instance). It's return the wanted value after the calculation, for example from the string '2*SD.Length+SD.Area' i will get a double that represent the value (2*Length +Area) of SD.
return Grade;
}I want to get the string from the user and create at runtime a function that can be overload to this event but I need this function before to compilation because then I can not choose the function for the event because the function does not exists. What can I do to resolve this problem?