C # activator. createinstance () method

Source: Internet
Author: User

From: http://blog.csdn.net/jaydawson/article/details/5539438

 

C # use the following method to dynamically create a class instance in a class factory:

1. activator. createinstance (type)

2. activator. createinstance (type, object [])

 

The differences between the two methods are as follows: creating a constructor without parameters and creating a constructor with parameters.

// Activator. createinstance (type)

Object result = NULL;
Type typeofcontrol = NULL;

Typeofcontrol = type. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol );

 

// Activator. createinstance (type, object [])

Object result = NULL;
Type typeofcontrol = NULL;

Typeofcontrol = type. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol, objparam );

 

However, during dynamic creation, instances of classes in the DLL of an external application may be dynamically used. In this case, decompilation is required and the Assembly class under the reflection naming control is used.

// First load the DLL using the Assembly class, and then obtain the class based on the full path of the class

Object result = NULL;
Type typeofcontrol = NULL;
Assembly tempassembly;

Tempassembly = assembly. loadfrom (vdllname );
Typeofcontrol = tempassembly. GetType (vfullclassname );
Result = activator. createinstance (typeofcontrol, objparam );

C # activator. createinstance () method

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.