samedi 15 janvier 2022

Reflection: Create instance of a class that inherit from another class located in sperate assembly

I created an empty Web Form Application and Created a WebForm and a class named MyClass:

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string GetText()
        {
            return "Message of Web Form 1";
        }
    }
}

and:

namespace WebApplication1
{
    public class MyClass
    {
        public string GetText()
        {
           return "Message from MyClass";
        }
    }
}

and I publish it using "Merge all assemblies in one assembly" option:

enter image description here

and then I want to get type and then create instance of WebForm1 and MyClass. So I wrote this code:

Assembly asm = Assembly.LoadFrom(@"C:\Pub\bin\WebApplication1.dll");

Type tMyClass = asm.GetType("WebApplication1.MyClass");

Type t = asm.GetType("WebApplication1.WebForm1"); <----Error

after the code has been ran tMyClass has correct type:

enter image description here]

but when it want to get type of WebForm1 it generate an error:

enter image description here

System.TypeLoadException: 'Could not load type 'System.Web.UI.Page' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.'

How can I get type of WebForm1 and create instance of it?





Aucun commentaire:

Enregistrer un commentaire