1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
private UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
{
ArrayList constParamTypes = new ArrayList();
foreach (object constParam in constructorParameters)
{
constParamTypes.Add(constParam.GetType()) ;
}
object[] o = constParamTypes.ToArray();
Type[] t = new Type[o.Length];
for (int i = 0; i < o.Length; i++)
{
t[i] = (Type)o[i];
}
UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;
// Find the relevant constructor
ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(t) ;
//And then call the relevant constructor
if (constructor == null)
{
throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString()) ;
}
else
{
constructor.Invoke(ctl,constructorParameters) ;
}
// Finally return the fully initialized UC
return ctl;
} |