C # Reflection--------Creating an object instance

Source: Internet
Author: User

C # Reflection--------Creating an object instance

There are three ways to create object instances in C #
(1) through the System.Reflection.Assmbly method obtains the instance, mainly according to the class FullName and the constructor function parameter creation instance, including the private and the public, very powerful

public Object CreateInstance (string typeName);//Use case-sensitive method to find the specified type name, FullName,
public Object CreateInstance (string typeName, bool ignoreCase);//Use the specified type name, FullName, whether case sensitivity is determined by the parameter

Using a variety of parameters, Binder is null, does not know what, args is the parameter of the constructor, culture and activation can be null, do not know what
Public virtual Object CreateInstance (string typeName, bool IgnoreCase, BindingFlags bindingattr, Binder binder, object[] A RGS, CultureInfo culture, object[] activationattributes);

Examples of creating instances:
var obj = Assembly.LoadFrom (@ "d:\ application software \vs2017\c#\windowsformsapp16\classlibrary1\bin\debug\classlibrary1.dll"). CreateInstance (parametric)

(2) using System.activator static method CreateInstance to obtain the corresponding example

 Public Static ObjectCreateInstance (type type);//Create an object instance based on a public parameterless constructor
public static object CreateInstance (type type, bool nonpublic), or true if common or non-public default constructors can match, or false if only public default constructors can match.
public static object CreateInstance (type type, object[] args, object[] activationattributes);//args as a constructor argument
public static object CreateInstance (type type, params object[] args);
public static object CreateInstance (type type, BindingFlags bindingattr, Binder binder, object[] args, CultureInfo culture );
public static object CreateInstance (type type, BindingFlags bindingattr, Binder binder, object[] args, CultureInfo culture , object[] activationattributes);


public static ObjectHandle CreateInstance (String assemblyname, String typeName);
public static ObjectHandle CreateInstance (String assemblyname, String TypeName, object[] activationattributes);



C # Reflection--------Creating an object instance

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.