Invoke a public method with no parameters using reflection?

The first parameter of MethodInfo. Invoke is the instance on which you want to invoke the method. Pass customControl as the first parameter instead of null and it should work.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a method in my user control: public string ControleIdContainer() { string abc = "Hello"; return abc; } Now I want to call this method on my Page using reflection. I have tried this but it is not working: DirectoryInfo dirInfo = new DirectoryInfo(Server. MapPath("~/UserControls")); FileInfo controlsInfo = dirInfo.

GetFiles("*. Ascx"); foreach (var item in controlsInfo) { var customControl = LoadControl(string. Format("~/UserControls/{0}", item)); var controlType = customControl.GetType(); var controlClientScript = controlType.

GetMethod("ControleIdContainer"). Invoke(null,null); } c# reflection usercontrols link|improve this question edited Dec 19 '11 at 18:36bobbymcr10.6k1231 asked Dec 19 '11 at 18:30Haseeb Khan615 46% accept rate.

Maybe you should use an interface like IHaveControleIdContainer - then you can just do an as cast, test for null, and call the method. – default. Kramer Dec 19 '11 at 18:42 Echoing @default.

Kramer ... yes, always avoid reflection wherever you can. It should be a last resort. – bobbymcr Dec 19 '11 at 19:06.

Its working but as you can see I am calling it in a loop so next time it gives me error "Object refrence not set to an instance of an object" – Haseeb Khan Dec 19 '11 at 18:35 @HaseebKhan: That could be for a variety of reasons. Is customControl null? Is GetMethod returning a null MethodInfo?

Try adding some checks for null in that code to narrow down where the problem is. – bobbymcr Dec 19 '11 at 18:38 thanks dear it helps a lot thanks very much – Haseeb Khan Dec 19 '11 at 18:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions