65.9K
CodeProject is changing. Read more.
Home

ASP.NET Load WebUserControl Programmatically and Invoke Method using Reflection

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 15, 2010

CPOL
viewsIcon

6050

Why invoke methods on a user web control that is required to be loaded dynamically

Perhaps you should explain a bit regarding the context of this tip.
e.g. Why would you want to invoke methods on a user web control that is required to be loaded dynamically?

There are three things:

  1. It sounds to me as a code smell: apparently, the control contains logic that is used by other methods outside of the control. Therefore, a better solution would be to encapsulate that logic in a reusable component outside of the webcontrol, so the webcontrol would not need to be loaded dynamically in the first place.
  2. Most of the example code demonstrates the use of reflection, which is not specific to web controls and therefore, kind of miss the point of this tip/trick.
  3. In my opinion also, the use of reflection is a code smell. Dynamic execution of methods (without compile-time type checking) should be avoided whenever possible as it may introduce runtime errors and reduces maintainability: code becomes less readable and understandable.

History

  • 12th March, 2010: Initial version

====================================
just because you can, doesn't mean you should.