Click here to Skip to main content
15,896,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project having only one class. And having another project with one web form with a button. I want to make the class of first project as the code behind of second project. How can I do that? I don't want to add my DLL as a reference in that project.
Posted

Follow the steps,


  1. The created C#(cs) file should be partial and inherited from the System.Web.UI.Page class as below
    C#
    public partial class Testing : System.Web.UI.Page
    {
    }
  2. Then Front end(.aspx) file, should refer your class file by CodeBehind attribute of <page> tag
  3. Also Inherits attribute should refers your class by means of you namespaces, as below
    ASP.NET
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Testing.aspx.cs" Inherits="WebApplication1.Testing" %>
 
Share this answer
 
Comments
Andy Lanng 7-Apr-15 6:27am    
How could that possibly work if the code behind is in a different assembly that is not referenced? Read the question again ^_^
F-ES Sitecore 7-Apr-15 6:39am    
Partial classes need to be in the same assembly, you can't have one half of a partial class in one assembly and another in a different assembly.
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:

C#
interface IPage
{
    void LoadMethod();
    void InitMethod();
    bool MyMethod();
}


Class1 can the implement this interface and use it for its own structure:

C#
   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()
    {
        //shared stuff
    }

    public void InitMethod()
    {
        //shared stuff
    }

    public bool MyMethod()
    {
        //shared stuff
        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 :

C#
   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); //Raise error? or log at least
            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
 
Share this answer
 
v3
Comments
Andy Lanng 7-Apr-15 6:30am    
The code tags (pre) are wrong and I can't find them in the source >_<
Baiju christadima 7-Apr-15 8:45am    
Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900