You cannot as the methods of System.Web.UI.Page are protected. You can only inherit to make use of these.
If the first project class (we'll call class1) just inherits System.Web.UI.Page then none of the standard methods will be available to the second project (class2) even if you add it as a reference.
You can create a bespoke class type for class1 so that you can expose methods you want to share. This can be done with inheritance and loading the class1 using reflection.
To do this, both class1 and class2 will need a shared point-of-reference so that they know what class1 looks like. You can create a simple Interface to do this:
interface IPage
{
void LoadMethod();
void InitMethod();
bool MyMethod();
}
Class1 can the implement this interface and use it for its own structure:
public partial class Class1 : System.Web.UI.Page , IPage
{
protected void Page_Load(object sender, EventArgs e)
{
LoadMethod();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitMethod();
}
public void LoadMethod()
{
}
public void InitMethod()
{
}
public bool MyMethod()
{
return true;
}
}
You can then load Class1 in Class2 using reflection and use the same methods in the same way (this ones more complex so I broke it down into several methods :
public partial class Class2 : System.Web.UI.Page
{
private IPage _page = GetPage("mydll", "myclass");
protected void Page_Load(object sender, EventArgs e)
{
_page.LoadMethod();
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_page.InitMethod();
}
protected virtual bool MyMethod()
{
return _page.MyMethod();
}
private static IPage GetPage(string assemblyName, string className)
{
Assembly parser = GetAssembly(assemblyName);
Type type = parser.GetType(className);
if (type.IsAssignableFrom(typeof(IPage)))
{
IPage instance = (IPage)GetInstance(type);
return instance;
}
throw new InvalidCastException("The Api does not implement the type " + typeof(IPage).Name);
}
private static Assembly GetAssembly(string trackerAssembly)
{
if (!string.IsNullOrEmpty(trackerAssembly))
{
string filename = trackerAssembly;
string extension = Path.GetExtension(filename);
if (!string.IsNullOrEmpty(extension) && !extension.Equals(".dll"))
filename += ".dll";
if (!File.Exists(filename))
{
string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!string.IsNullOrEmpty(location))
filename = Path.Combine(location, "Apis", filename);
}
if (!File.Exists(filename))
throw new FileNotFoundException("Could not find the file", filename);
return Assembly.LoadFrom(filename);
}
return Assembly.GetExecutingAssembly();
}
private static object GetInstance(Type page)
{
if (!page.IsAssignableFrom(typeof(IPage)))
throw new ArgumentException(string.Format("The type {0} does not implement the interface {1}.", page.Name, typeof(IPage).Name), "page");
Type[] types = Type.EmptyTypes;
var constructorInfo = page.GetConstructor(types);
if (constructorInfo != null)
return constructorInfo.Invoke(null);
throw new ArgumentException(string.Format("The type {0} does not have a ctor that matches the ctor paramters.", page.Name), "page");
}
}
Both project need to have a reference to the IPage interface. You can do this by creating a third project with the interface and reference this in both your other projects.
NB: They MUST use the same projects IPage. They should not have separate IPage interfaces that just look the same.
Hope that helps