c# - How to pass an argument when invoking a method (reflection)? -
i need call method, passing int. using following code can fetch method not passing argument. how fix it?
dynamic obj; obj = activator.createinstance(type.gettype(string.format("{0}.{1}", namespacename, classname)));  var method = this.obj.gettype().getmethod(this.methodname, new type[] { typeof(int) }); bool isvalidated = method.invoke(this.obj, new object[1]);  public void mymethod(int id) { }      
the new object[1] part how you're specifying arguments - you're passing in array single null reference. want:
int id = ...; // whatever want value object[] args = new object[] { id }; method.invoke(obj, args);   (see methodbase.invoke documentation more details.)
note method.invoke returns object, not bool, current code wouldn't compile. cast return value bool, in example wouldn't @ execution time mymethod returns void.
Comments
Post a Comment