Uređivanje

Dijeli putem


Activator Class

Definition

Contains methods to create types of objects locally or remotely, or obtain references to existing remote objects. This class cannot be inherited.

public ref class Activator abstract sealed
public ref class Activator sealed : System::Runtime::InteropServices::_Activator
public static class Activator
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class Activator : System.Runtime.InteropServices._Activator
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class Activator : System.Runtime.InteropServices._Activator
type Activator = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type Activator = class
    interface _Activator
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Activator = class
    interface _Activator
Public Class Activator
Public NotInheritable Class Activator
Implements _Activator
Inheritance
Activator
Attributes
Implements

Examples

The following example shows how to use the Activator class to dynamically construct objects at run time.

using namespace System;
using namespace System::Reflection;
using namespace System::Text;

public ref class SomeType
{
public:
    void DoSomething(int x)
    {
        Console::WriteLine("100 / {0} = {1}", x, 100 / x);
    }
};

void main()
{
    // Create an instance of the StringBuilder type using 
    // Activator.CreateInstance.
    Object^ o = Activator::CreateInstance(StringBuilder::typeid);

    // Append a string into the StringBuilder object and display the 
    // StringBuilder.
    StringBuilder^ sb = (StringBuilder^) o;
    sb->Append("Hello, there.");
    Console::WriteLine(sb);

    // Create an instance of the SomeType class that is defined in this 
    // assembly.
    System::Runtime::Remoting::ObjectHandle^ oh = 
        Activator::CreateInstanceFrom(Assembly::GetEntryAssembly()->CodeBase, 
                                      SomeType::typeid->FullName);

    // Call an instance method defined by the SomeType type using this object.
    SomeType^ st = (SomeType^) oh->Unwrap();

    st->DoSomething(5);
};

/* This code produces the following output:
 
Hello, there.
100 / 5 = 20
 */
using System;
using System.Reflection;
using System.Text;

public class SomeType
{
    public void DoSomething(int x)
    {
        Console.WriteLine("100 / {0} = {1}", x, 100 / x);
    }
}

public class Example
{
    static void Main()
    {
        // Create an instance of the StringBuilder type using
        // Activator.CreateInstance.
        Object o = Activator.CreateInstance(typeof(StringBuilder));

        // Append a string into the StringBuilder object and display the
        // StringBuilder.
        StringBuilder sb = (StringBuilder) o;
        sb.Append("Hello, there.");
        Console.WriteLine(sb);

        // Create an instance of the SomeType class that is defined in this
        // assembly.
        System.Runtime.Remoting.ObjectHandle oh =
            Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase,
                                         typeof(SomeType).FullName);

        // Call an instance method defined by the SomeType type using this object.
        SomeType st = (SomeType) oh.Unwrap();

        st.DoSomething(5);
    }
}

/* This code produces the following output:

Hello, there.
100 / 5 = 20
 */
open System
open System.Reflection
open System.Text

type SomeType() =
    member _.DoSomething(x) = printfn $"100 / {x} = {100 / x}"

// Create an instance of the StringBuilder type using Activator.CreateInstance.
let o = Activator.CreateInstance typeof<StringBuilder>

// Append a string into the StringBuilder object and display the StringBuilder.
let sb = o :?> StringBuilder
sb.Append "Hello, there." |> ignore
printfn $"{sb}"

// Create an instance of the SomeType class that is defined in this assembly.
let oh = 
    Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().Location, typeof<SomeType>.FullName)

// Call an instance method defined by the SomeType type using this object.
let st = oh.Unwrap() :?> SomeType

st.DoSomething 5

(* This code produces the following output:

Hello, there.
100 / 5 = 20
 *)
Imports System.Reflection
Imports System.Text

Module Module1
    Sub Main()
        ' Create an instance of the StringBuilder type using 
        ' Activator.CreateInstance.
        Dim o As Object = Activator.CreateInstance(GetType(StringBuilder))

        ' Append a string into the StringBuilder object and display the 
        ' StringBuilder.
        Dim sb As StringBuilder = CType(o, StringBuilder)
        sb.Append("Hello, there.")
        Console.WriteLine(sb)

        ' Create an instance of the SomeType class that is defined in this assembly.
        Dim oh As System.Runtime.Remoting.ObjectHandle = _
            Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, _
                                         GetType(SomeType).FullName)

        ' Call an instance method defined by the SomeType type using this object.
        Dim st As SomeType = CType(oh.Unwrap(), SomeType)

        st.DoSomething(5)
    End Sub

    Class SomeType
        Public Sub DoSomething(ByVal x As Int32)
            Console.WriteLine("100 / {0} = {1}", x, 100 \ x)
        End Sub
    End Class
End Module

' This code produces the following output:
' 
' Hello, there.
' 100 / 5 = 20

Remarks

The CreateInstance method creates an instance of a type defined in an assembly by invoking the constructor that best matches the specified arguments. If no arguments are specified, the constructor that takes no parameters, that is, the parameterless constructor, is invoked.

You must have sufficient permission to search for and call a constructor; otherwise, an exception is thrown. By default, only public constructors are considered during the search for a constructor. If no constructor or parameterless constructor can be found, an exception is thrown.

A binder parameter specifies an object that searches an assembly for a suitable constructor. You can specify your own binder and search criteria. If no binder is specified, a default binder is used. For more information, see the System.Reflection.Binder and System.Reflection.BindingFlags classes.

An evidence parameter affects the security policy and permissions for the constructor. For more information, see the System.Security.Policy.Evidence class.

An instance of a type can be created at a local or remote site. If the type is created remotely, an activation attribute parameter specifies the URI of the remote site. The call to create the instance might pass through intermediary sites before it reaches the remote site. Other activation attributes can modify the environment, or context, in which the call operates at the remote and intermediary sites.

If the instance is created locally, a reference to that object is returned. If the instance is created remotely, a reference to a proxy is returned. The remote object is manipulated through the proxy as if it were a local object.

The GetObject method creates a proxy to a currently running remote object, server-activated well-known object, or XML Web service. You can specify the connection medium, that is, the channel. For more information, see the System.Runtime.Remoting.Channels.ChannelServices class.

Assemblies contain type definitions. The CreateInstance method creates an instance of a type from a currently running assembly. The CreateInstanceFrom method creates an instance from a file that contains an assembly. The CreateComInstanceFrom method creates an instance of a COM object from a file that contains an assembly.

Methods

CreateComInstanceFrom(String, String, Byte[], AssemblyHashAlgorithm)

Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

CreateComInstanceFrom(String, String)

Creates an instance of the COM object whose name is specified, using the named assembly file and the parameterless constructor.

CreateInstance(ActivationContext, String[])

Creates an instance of the type that is designated by the specified ActivationContext object and activated with the specified custom activation data.

CreateInstance(ActivationContext)

Creates an instance of the type designated by the specified ActivationContext object.

CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
Obsolete.

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(AppDomain, String, String)

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly and parameterless constructor.

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
Obsolete.

Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the type whose name is specified, using the named assembly and the constructor that best matches the specified parameters.

CreateInstance(String, String, Object[])

Creates an instance of the type whose name is specified, using the named assembly and parameterless constructor.

CreateInstance(String, String)

Creates an instance of the type whose name is specified, using the named assembly and parameterless constructor.

CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance(Type, BindingFlags, Binder, Object[], CultureInfo)

Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance(Type, Boolean)

Creates an instance of the specified type using that type's parameterless constructor.

CreateInstance(Type, Object[], Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance(Type, Object[])

Creates an instance of the specified type using the constructor that best matches the specified parameters.

CreateInstance(Type)

Creates an instance of the specified type using that type's parameterless constructor.

CreateInstance<T>()

Creates an instance of the type designated by the specified generic type parameter, using the parameterless constructor.

CreateInstanceFrom(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
Obsolete.

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(AppDomain, String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(AppDomain, String, String)

Creates an instance of the type whose name is specified in the specified remote domain, using the named assembly file and parameterless constructor.

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[], Evidence)
Obsolete.

Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(String, String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

Creates an instance of the type whose name is specified, using the named assembly file and the constructor that best matches the specified parameters.

CreateInstanceFrom(String, String, Object[])

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

CreateInstanceFrom(String, String)

Creates an instance of the type whose name is specified, using the named assembly file and parameterless constructor.

GetObject(Type, String, Object)

Creates a proxy for the well-known object indicated by the specified type, URL, and channel data.

GetObject(Type, String)

Creates a proxy for the well-known object indicated by the specified type and URL.

Explicit Interface Implementations

_Activator.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

_Activator.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

_Activator.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

_Activator.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

Applies to