|
using System;
using System.Collections;
using System.Reflection;
using System.Xml;
namespace JobMorning
{
/// <summary>
/// ActionClassContainer deals with the collection of objects of type ActionClass during the application
/// life. It is also a wrapper for all ActionClass functions that can call the methods of a specific
/// ActionClass using its page name reference.
/// </summary>
public class ActionClassContainer
{
// contains all instantiated children of the base class ActionClass.
private Hashtable actionClassObjects;
// contains the type of all ActionClass objects.
private Hashtable actionClassTypes;
// reference to the current Session Object
private System.Web.SessionState.HttpSessionState sessionObject;
public ActionClassContainer()
{
actionClassObjects = new Hashtable();
actionClassTypes = new Hashtable();
}
/// <summary>
/// Instatiate a class inheriting the base class ActionClass using the class name.
/// Associates this new created class with an a page name.
/// </summary>
/// <param name="strActionClassName"></param>
/// <returns>returns true if the class inheriting from ActionClass could be created successfully.</returns>
public bool InstantiateActionClass(string strActionClassName, string strPage)
{
if (actionClassObjects[strPage]==null)
{
// object anActionClass;
// anActionClass = Activator.CreateInstance(strActionClassName, "JobMorning.ActionClass");
// Load the generated assembly into the ApplicationDomain
Object anActionClass;
Type t = null;
try
{
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(strActionClassName);
if (t!=null)
{
anActionClass = Activator.CreateInstance(t);
actionClassObjects.Add(strPage, anActionClass);
actionClassTypes.Add(strPage, strActionClassName);
} // if (t!=null)
}
return true;
}
catch (System.Reflection.TargetInvocationException e)
{
sessionObject["ERROR"]="The ActionClass "+strActionClassName+" could not be instantiated.";
sessionObject["ERROR"]+="The constructor of the object class threw an exception. ";
return false;
}
catch (System.ArgumentNullException)
{
sessionObject["ERROR"]="The type of "+strActionClassName+" could not be found in the ";
sessionObject["ERROR"]+="running assembly. Make sure the type exists.";
return false;
}
return false;
/*
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
// Construct an instance of the type and invoke the member method
Object anActionClass = t.InvokeMember("doBeforeLoad", bflags |
BindingFlags.CreateInstance, null, null, null);
*/
}
else
{
// ActionClass already instantiated.
return false;
}
}
/// <summary>
/// Deletes an ActionClass associated to a page.
/// </summary>
/// <param name="strPage">The page name.</param>
/// <returns></returns>
public bool DisposeActionClass(string strPage)
{
bool rtnBool = true;
rtnBool = actionClassObjects.Contains(strPage);
actionClassObjects.Remove(strPage);
return rtnBool;
}
/// <summary>
/// Procedure called before the page mentionned in the XML file is loaded and displayed.
/// </summary>
public void DoBeforeDisplay(string strPage, string strXmlMCContext)
{
object anActionClass = actionClassObjects[strPage];
string strType = (string)actionClassTypes[strPage];
if (anActionClass!=null)
{
// Load the generated assembly into the ApplicationDomain
Type t;
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(strType);
if (t!=null)
{
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
string[] allParams = new string[1];
allParams[0]=strXmlMCContext;
// Call the method
t.InvokeMember("DoBeforeDisplay", bflags | BindingFlags.InvokeMethod,
null, anActionClass,allParams);
break;
} // if (t!=null)
}
}
}
/// <summary>
/// doPageValidation validates the page input.
/// If the result is false, the page is displayed again with a PopUp message containing
/// strErrorMessage. If no ActionClass is associated to the page, returns the value true.
/// </summary>
public bool ValidatePageBeforeSavePoints(string strPage,Hashtable pageInputs, out string strErrorMessage)
{
object anActionClass = actionClassObjects[strPage];
string strType = (string)actionClassTypes[strPage];
strErrorMessage = "";
if (anActionClass!=null)
{
// Load the generated assembly into the ApplicationDomain
Type t;
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(strType);
if (t!=null)
{
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
object[] allParams = new object[2];
allParams[0]=pageInputs;
allParams[1]=strErrorMessage;
// Call the method
object boolRslt = t.InvokeMember("ValidatePageBeforeSavePoints", bflags | BindingFlags.InvokeMethod,
null, anActionClass,allParams);
strErrorMessage = (string)allParams[1];
return ((bool)boolRslt);
} // if (t!=null)
} // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
return false;
} // if (anActionClass!=null)
else
{
return true;
}
}
/// <summary>
/// ValidatePageAfterSavePoint validates the page input after a savePoint has been executed.
/// The savePoint must be contained in the current path between the current page and its successor.
/// </summary>
public bool ValidatePageAfterSavePoint(string strPage, string strSavePointName, string strXmlMCContext, out string strErrorMessage)
{
strErrorMessage = "";
object anActionClass = actionClassObjects[strPage];
string strType = (string)actionClassTypes[strPage];
if (anActionClass!=null)
{
Type t;
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(strType);
if (t!=null)
{
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
object[] allParams = new object[3];
allParams[0]=strSavePointName;
allParams[1]=strXmlMCContext;
allParams[2]= strErrorMessage;
// Call the method
object boolRslt = t.InvokeMember("ValidatePageAfterSavePoint", bflags | BindingFlags.InvokeMethod,
null, anActionClass,allParams);
strErrorMessage = (string)allParams[2];
return ((bool)boolRslt);
} // if (t!=null)
} // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
return false;
} // if (anActionClass!=null)
else
{
return true;
}
}
/// <summary>
/// Procedure called after the page has been posted and that all values have been saved
/// into the context. This procedure is executed after ValidatePageBeforeSavePoints.
/// </summary>
public void DoAfterContextSave(string strPage, string strXmlMCContext)
{
object anActionClass = actionClassObjects[strPage];
string strType = (string)actionClassTypes[strPage];
if (anActionClass!=null)
{
// Load the generated assembly into the ApplicationDomain
Type t;
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
t = asm.GetType(strType);
if (t!=null)
{
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
string[] allParams = new string[1];
allParams[0]=strXmlMCContext;
// Call the method
t.InvokeMember("DoAfterContextSave", bflags | BindingFlags.InvokeMethod,
null, anActionClass,allParams);
break;
} // if (t!=null)
} // foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.