How to access a function of a namespace from outside of application in c#? -
i have namespace shown below.
namespace testnamespace { public class testclass : system.web.ui.page { public static string testdata; public void test() { string s = "fgdfgdg"; } protected string invoke(string methodname) { //string methodname = "test"; string classname = methodname.split('-')[0]; string funcname=methodname.split('-')[1]; type type = type.gettype(classname); activator.createinstance(type); methodinfo method = type.getmethod(funcname); string result = (string)method.invoke(activator.createinstance(type), null); return result; } public static string testfunc(string temp) { hdndata = temp; string strval= invoke(s); return strval; } } }
i referencing dll in application given below.
using testnamespace; protected void button1_click(object sender, eventargs e) { string test='testfunction'; string s=testfunc(test); }
when declare function public im getting error
"an object reference required non-static field, method, or property "
but when declare public static can access function other functions,variables needs declared static. i dont want functions of class static function testfunc. how can this?
to call static function should specify class name first. otherwise compiler not know, method call. in example replace:
string s=testfunc(test);
with
string s = testclass.testfunc(test);
you should provide valid parameters, matching method's signature, when calling method.invoke(...)
, otherwise runtime exception.
and while dont know, why 1 create such design (and dont explain idea behind test class either), looks "bad" design me.
Comments
Post a Comment