TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Daniel Kaschel
NA
5
712
Calling a method by name with parameters (or build like sql)
Jan 21 2016 12:35 PM
.Net Version 4.5.1
Very Brief Background
I have a console application where the user enters a function and parameters. Rather than adding supported functions to a dictionary of some kind, I'd like to just have it call the method by name, using the method signature to validate the parameters. I can gather all the information I need in theory, but can't figure out how to CALL the expression.
I think I know why--it's because I can't figure out how to coerce my parameters into a form the Expression.Call line will accept. I tried using the ParameterExpression class, but it doesn't seem to contain the values, only the types and names.
What I Want to Do
/// <summary>
/// Call a method in this class instance by name and parameters
/// </summary>
/// <param name="methodName"></param>
/// <param name="args"></param>
/// <returns></returns>
public
void
Execute(
string
methodName,
object
[] args)
{
//get method we will be calling
MethodInfo method = GetMethodByName(methodName);
//validate & retrieve parameters based on the method signature
object
[] parameters;
if
(!GetAndValidateParams(
method.GetParameters().Select(pd => pd.ParameterType).ToArray(),
args,
out
parameters))
return
;
//exit on failed validation of parameters
//call method using signature -- this is where I'm getting an error
Expression.Call(method, parameters);
}
I would also be happy with the ability to "assemble" a method call, like in this pseudocode:
MethodCall mc =
new
MethodCall(methodInfo);
mc.ClassInstance =
this
;
mc.Parameters.AddRange(parameters);
mc.Execute();
Thank you in advance for any help or guidance you can provide!
Daniel
Just in Case: The Validation Method
//validates that types are as expected. Only accepts string, int and decimal
private
bool
GetAndValidateParams(Type[] expected,
object
[] args,
out
object
[] convertedArgs)
{
Console.Write(
"Validating parameters..."
);
//check for any unsupported types
convertedArgs =
null
;
Type[] supportedTypes =
new
Type[] {
typeof
(
decimal
),
typeof
(
string
),
typeof
(
int
) };
if
(expected.Any(t => !supportedTypes.Any(st => t == st)))
return
false
;
if
(expected ==
null
)
return
false
;
if
(expected.Count() == 0 && (args ==
null
|| args.Count() == 0))
{
Console.Write(
"Success.\r\n"
);
return
true
;
}
convertedArgs =
new
object
[args.Count()];
for
(
int
x = 0; x < expected.Count(); x++)
{
//check to make sure there are args left
if
(x > (args.Count() - 1))
return
false
;
//if we have an arg and an expectation, check them
try
{ convertedArgs[x] = Convert.ChangeType(args[x], expected[x]); }
catch
{
return
false
;}
}
//If we got here, everything was validated successfully
Console.Write(
"Success.\r\n"
);
return
true
;
}
Reply
Answers (
1
)
I Want to Display dateformat(dd/MM/yyyy) in masked textbox
Sorry,Please Give Me,datetime(MM/dd/yyyy) will Display into