C # The ASPX page dynamically loads the ascx user control and calls it through reflection

Source: Internet
Author: User
ArticleDirectory
    • Comment

// ControlCode
Public partial class webusercontrol: system. Web. UI. usercontrol
{
   Public void testmethod (string Strid)
   {
       This. textbox1.text + = "webusercontrol:" + Strid;
       // Other related operations
   }
}

// Control code
Public partial class webusercontrol2: system. Web. UI. usercontrol
{
   Public void testmethod (string Strid)
   {
       This. textbox1.text + = "webusercontrol2:" + Strid;
       // Perform other operations based on input parameters
   }
}

// Page code
Public partial class default1: system. Web. UI. Page
{
Bool isshow = true; // Yes
String strwebusercontrols = "webusercontrol, webusercontrol2 ";
Protected void page_load (Object sender, eventargs E)
{
If (isshow)
{
String [] strucs = strwebusercontrols. Split (",". tochararray () [0]);
For (INT I = 0; I <strucs. length; I ++)
{
String strucname = strucs [I]. tostring ();
Control A = page. loadcontrol (strucname + ". ascx ");
A. ID = strucname;
This. panel1.controls. Add ();
}
}
}

// Page button operation
Protected void button#click (Object sender, eventargs E)
{
If (isshow)
{
String [] strucs = strwebusercontrols. Split (",". tochararray () [0]);
For (INT I = 0; I <strucs. length; I ++)
{
String strucname = strucs [I]. tostring ();
Type TC = This. panel1.findcontrol (strucname). GetType ();
Control UC = This. panel1.findcontrol (strucname );
/// Object o = system. activator. createinstance (UC. GetType ());
System. reflection. methodinfo M = tc. getmethod ("testmethod ");
Object [] objparas = new object [1];
Objparas [0] = "1 ";
M. Invoke (UC, objparas );
//// M. Invoke (A, null );
}
}
}
}

Posted on freeliver54 read (774) Comments (3)Edit the category of the favorite CIDR Block: VS technical skills

Comment # 1st Floor [Poster] Freeliver54

C # a small example of the reflection class [system. Reflection] [Reproduced]
// Create a class library project and add a getsum method.
Using system;

Namespace classlibrary1
{
Public class class1
{
Public class1 ()
{
}
Public int getsum (int x, int y)
{
Return X + Y;
}
}
}

// Create another project and reference the classlibrary1.dll generated above in the project

System. reflection. Assembly A = system. reflection. Assembly. loadfrom ("classlibrary1.dll ");

System. type T = A. GetType ("classlibrary1.class1 ");

// Dynamically generate an instance of the classlibrary1.class class
Object theobj = system. activator. createinstance (t );

// Parameter information. getsum requires two int parameters.
System. Type [] paramtypes = new system. Type [2];
Paramtypes [0] = system. type. GetType ("system. int32 ");
Paramtypes [1] = system. type. GetType ("system. int32 ");

// Obtain Method
System. reflection. methodinfo MI = T. getmethod ("getsum", paramtypes );

// Parameter value
Object [] parameters = new object [2];
Parameters [0] = 3;
Parameters [1] = 4;

// Call
Object returnvalue = mi. Invoke (theobj, parameters );

Console. writeline ("classlibrary1.class1. getsum (3, 4) returns: {0}", returnvalue. tostring ()); Reply Reference View   

# Floor 2 [Poster] Freeliver54

C # class reflection instance
Http://www.cnblogs.com/hl8792/archive/2008/08/05/1261051.html
Create a class class1 in a namespace and a class S1;
(Here, these classes are only used for called calls, which is of little significance. Reflection usage in Form class)

Class1 and form are in the same namespace
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace fanshetest1
{
Class class1
{
Private string AB = "1 ";
Public class1 (string aa)
{
A = AA;
}
Public int AA (int x, int y)
{
Return X + Y + x + y;
}
Public String BB ()
{
Return "BB ";
}
Public static string CC ()
{
Return "cc ";
}
Public String AB
{
Get
{
Return AB;
}
Set
{
AB = value;
}
}
Public String;
}
}

The class1 and form forms are in different namespaces.
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace fanshetest1
{
Class class1
{
Private string AB = "1 ";
Public class1 (string aa)
{
A = AA;
}
Public int AA (int x, int y)
{
Return X + Y + x + y;
}
Public String BB ()
{
Return "BB ";
}
Public static string CC ()
{
Return "cc ";
}
Public String AB
{
Get
{
Return AB;
}
Set
{
AB = value;
}
}
Public String;
}
}
The following describes how to use reflection to operate the above classes;
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. reflection;
Namespace fanshetest
{
Public partial class form1: Form
{
System. reflection. Assembly;
System. reflection. assembly B;
Public form1 ()
{
Initializecomponent ();
A = system. reflection. Assembly. loadfrom ("class1.dll ");
}

Private void button#click (Object sender, eventargs E)
{
Gouzaohanshu ();

}< br> // if no parameter is set, a type is returned.
private void one ()
{< br> // create another project, REFERENCE The classlibrary1.dll generated above in the project
system. type T =. getType ("class1.class1");
// dynamically generate an instance of the classlibrary1.class class
Object theobj = system. activator. createinstance (t);
// parameter information. getsum requires two string parameters.
system. type [] paramtypes = new system. type [2];
paramtypes [0] = system. type. getType ("system. string ");
paramtypes [1] = system. type. getType ("system. string ");
// system. reflection. methodinfo MI = T. getmethod ("AA", paramtypes);
system. reflection. methodinfo MI = T. getmethod ("BB");
// parameter value
object [] parameters = new object [2];
parameters [0] = 3;
parameters [1] = 4;

Object returnvalue = mi. Invoke (theobj, null );
This. textbox1.text = returnvalue. tostring ();
}
// If no return value is returned, directly execute the function.
Private void none_void ()
{
// Create another project and reference the classlibrary1.dll generated above in the project
System. type T = A. GetType ("class1.class2 ");
// Dynamically generate an instance of the classlibrary1.class class
Object theobj = system. activator. createinstance (t );
// Parameter information. getsum requires two string parameters.
System. Type [] paramtypes = new system. Type [2];
// Call the method here. If there is a parameter, you only need to add "," and the type [] parameter of the parameter.
System. reflection. methodinfo MI = T. getmethod ("mes ");
Object returnvalue = mi. Invoke (theobj, null );

}
// No return value. Outgoing parameters receive the return value;
Private void two ()
{
Type T = A. GetType ("class1.class1 ");
Object theobj = activator. createinstance (t );
Type [] types = new type [2];
Types [0] = type. GetType ("system. int32 ");
Types [1] = type. GetType ("system. int32 ");
Object [] OBJ = new object [2];
OBJ [0] = 2;
OBJ [1] = 3;
Methodinfo MI = T. getmethod ("AA", types );
Object returnvalue = mi. Invoke (theobj, OBJ );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain the reflected output value of the same namespace
Private void local ()
{
Type T = type. GetType ("fanshetest1.class1 ");
// Create an instance
Object theobj = activator. createinstance (t );
Type [] types = new type [2];
Types [0] = type. GetType ("system. int32 ");
Types [1] = type. GetType ("system. int32 ");
Object [] OBJ = new object [2];
OBJ [0] = 2;
OBJ [1] = 3;
Methodinfo MI = T. getmethod ("AA", types );
Object returnvalue = mi. Invoke (theobj, OBJ );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain an attribute
Private void attribute ()
{
A = assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
Object theobj = activator. createinstance (t );
T. getproperty ("AB"). setvalue (theobj, "A", null );
This. textbox1.text = T. getproperty ("AB"). getvalue (theobj, null). tostring ();
}
// Obtain static functions;
Private void static _()
{
A = system. reflection. Assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
// Create an instance
Object theobj = activator. createinstance (t );
// Create an instance of a Method
Methodinfo MI = T. getmethod ("cc ");
Object returnvalue = mi. Invoke (theobj, null );
This. textbox1.text = returnvalue. tostring ();
}
// Obtain the variable;
Private void variable ()
{
A = assembly. loadfrom ("class1.dll ");
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
Object theobj = activator. createinstance (t );
Methodinfo MI = T. getmethod ("");
T. getfield ("A"). setvalue (theobj, "");
This. textbox1.text = T. getfield ("A"). getvalue (theobj). tostring ();
}
// Obtain the private variable reflection value;
Private void private _()
{

}
// Constructor
Private void gouzaohanshu ()
{
Type T = A. GetType ("class1.class1 ");
T = type. GetType ("fanshetest1.class1 ");
// Create the constructor type
Type [] structure_type = new type [1];
Structure_type [0] = type. GetType ("system. String ");
// Define the parameter passing type of the constructor
Object [] structure_obj = new object [1];
Structure_obj [0] = "AA ";
// Create an instance
Object theobj = activator. createinstance (T, structure_obj );
// Methodinfo structure_mi = T. getconstructor (structure_type );
// Methodinfo structure_mi = T. getmethod ("class1", structure_type );
// Structure_mi.invoke (theobj, structure_obj );

Methodinfo MI = T. getmethod ("");
// T. getfield ("A"). setvalue (theobj, "");
This. textbox1.text = T. getfield ("A"). getvalue (theobj). tostring ();
}

}
} Reply Reference View   

# Third Floor [Poster] Freeliver54

Type has scope,
To obtain the type instance of system. Web,
You must first obtain the assembly of system. Web,
The findtype function is used to describe all the assemblies in the current appdomain.
And find the type of the input string. The function list is as follows:

Private type findtype (string typename)
{
Foreach (Assembly assem in appdomain. currentdomain. getassemblies ())
{
Type T = assem. GetType (typename );
If (T! = NULL)
Return T;
}
Return NULL;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.