Assembly.CreateInstance 메서드

정의

이 어셈블리에서 형식을 찾은 다음 시스템 활성기를 사용하여 해당 형식의 인스턴스를 만듭니다.

오버로드

CreateInstance(String)

대/소문자 구분 검색 기능을 사용하여 이 어셈블리에서 지정된 형식을 찾은 다음 시스템 활성기를 사용하여 해당 형식의 인스턴스를 만듭니다.

CreateInstance(String, Boolean)

대/소문자 구분 검색 기능을 선택적으로 사용하여, 지정된 형식을 이 어셈블리에서 찾은 다음 시스템 활성기를 사용하여 해당 인스턴스를 만듭니다.

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

대/소문자 구분 검색 기능을 선택적으로 사용하고 지정된 문화권, 인수, 바인딩 및 활성화 특성을 사용하여, 지정된 형식을 이 어셈블리에서 찾은 다음 시스템 활성기를 사용하여 해당 인스턴스를 만듭니다.

CreateInstance(String)

Source:
Assembly.cs
Source:
Assembly.cs
Source:
Assembly.cs

대/소문자 구분 검색 기능을 사용하여 이 어셈블리에서 지정된 형식을 찾은 다음 시스템 활성기를 사용하여 해당 형식의 인스턴스를 만듭니다.

public:
 System::Object ^ CreateInstance(System::String ^ typeName);
public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName);
public object? CreateInstance (string typeName);
public object CreateInstance (string typeName);
member this.CreateInstance : string -> obj
abstract member CreateInstance : string -> obj
override this.CreateInstance : string -> obj
Public Function CreateInstance (typeName As String) As Object

매개 변수

typeName
String

찾을 형식의 FullName입니다.

반환

괄호가 없는 생성자로 만든 지정된 형식의 인스턴스입니다. 또는 typeName을 찾을 수 없는 경우 null입니다. 형식은 문화권 또는 활성화 특성을 지정하지 않고 BindingFlagsPublic 또는 Instance로 설정하여 기본 바인더를 사용하여 확인됩니다.

구현

예외

typeName이 빈 문자열("") 또는 null 문자로 시작하는 문자열입니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었습니다.

typeName이(가) null인 경우

일치하는 생성자를 찾을 수 없습니다.

typeName 에 종속 어셈블리가 필요하지만 이 어셈블리를 찾을 수 없습니다.

typeName에 종속 어셈블리가 필요하며 이 어셈블리를 찾았지만 로드할 수 없습니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었으며 typeName에 종속 어셈블리가 필요하지만 이 어셈블리가 미리 로드되지 않았습니다.

typeName 에는 종속 어셈블리가 필요하지만 파일은 현재 로드된 런타임에 유효한 어셈블리가 아닙니다.

예제

다음 예제에서는 클래스를 Person 정의하고 메서드를 CreateInstance(String) 호출하여 인스턴스화합니다.

using System;
using System.Reflection;
using Contoso.Libraries;

namespace Contoso.Libraries
{
   public class Person
   {
      private string _name;

      public Person()
      { }

      public Person(string name)
      {
         this._name = name;
      }

      public string Name
      { get { return this._name; }
        set { this._name = value; } }

      public override string ToString()
      {
         return this._name;
      }
   }
}

public class Example
{
   public static void Main()
   {
      Assembly assem = typeof(Person).Assembly;
      Person p = (Person) assem.CreateInstance("Contoso.Libraries.Person");
      if (! (p == null)) {
         p.Name = "John";
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p);
      }
      else {
         Console.WriteLine("Unable to instantiate a Person object.");
      }
   }
}
// The example displays the following output:
//        Instantiated a Person object whose value is 'John'
Imports System.Reflection
Imports Contoso.Libraries

Namespace Contoso.Libraries
   Public Class Person
      Private _name As String 
   
      Public Sub New()
      End Sub 
   
      Public Sub New(name As String)
         Me._name = name
      End Sub 
   
      Public Property Name As String 
         Get 
            Return Me._name
         End Get 
         Set 
            Me._name = value
         End Set 
      End Property 
   
      Public Overrides Function ToString() As String 
         Return Me._name
      End Function 
   End Class
End Namespace 

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Person).Assembly
      Dim p As Person = CType(assem.CreateInstance("Contoso.Libraries.Person"),
                              Person)
      If p IsNot Nothing Then
         p.Name = "John"
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p)
      Else
         Console.WriteLine("Unable to instantiate a Person object.")
      End If   
   End Sub
End Module
' The example displays the following output:
'       Instantiated a Person object whose value is 'John'

설명

런타임이 instance 찾을 typeNameAssembly 수 없는 경우 예외를 throw하는 대신 를 반환 null 합니다. 이 문제는 다음과 같은 경우에 발생할 수 있습니다.

  • 형식의 정규화된 이름을 지정하지 않았습니다.

  • 정규화된 형식 이름을 지정했지만 해당 대/소문자를 형식의 Type.FullName 속성과 일치하지 않습니다. 형식의 전체 이름과 대typeName/소문자를 구분하지 않는 비교의 경우 오버로드를 CreateInstance(String, Boolean) 호출하고 인수에 ignoreCase 대해 를 지정 true 합니다.

  • 형식이 현재 Assembly instance 없습니다.

적용 대상

CreateInstance(String, Boolean)

Source:
Assembly.cs
Source:
Assembly.cs
Source:
Assembly.cs

대/소문자 구분 검색 기능을 선택적으로 사용하여, 지정된 형식을 이 어셈블리에서 찾은 다음 시스템 활성기를 사용하여 해당 인스턴스를 만듭니다.

public:
 System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase);
public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase);
public object? CreateInstance (string typeName, bool ignoreCase);
public object CreateInstance (string typeName, bool ignoreCase);
member this.CreateInstance : string * bool -> obj
abstract member CreateInstance : string * bool -> obj
override this.CreateInstance : string * bool -> obj
Public Function CreateInstance (typeName As String, ignoreCase As Boolean) As Object

매개 변수

typeName
String

찾을 형식의 FullName입니다.

ignoreCase
Boolean

형식 이름의 대/소문자를 무시하면 true이고, 그러지 않으면 false입니다.

반환

괄호가 없는 생성자로 만든 지정된 형식의 인스턴스입니다. 또는 typeName을 찾을 수 없는 경우 null입니다. 형식은 문화권 또는 활성화 특성을 지정하지 않고 BindingFlagsPublic 또는 Instance로 설정하여 기본 바인더를 사용하여 확인됩니다.

구현

예외

typeName이 빈 문자열("") 또는 null 문자로 시작하는 문자열입니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었습니다.

일치하는 생성자를 찾을 수 없습니다.

typeName이(가) null인 경우

typeName 에 종속 어셈블리가 필요하지만 이 어셈블리를 찾을 수 없습니다.

typeName에 종속 어셈블리가 필요하며 이 어셈블리를 찾았지만 로드할 수 없습니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었으며 typeName에 종속 어셈블리가 필요하지만 이 어셈블리가 미리 로드되지 않았습니다.

typeName 에는 종속 어셈블리가 필요하지만 파일은 현재 로드된 런타임에 유효한 어셈블리가 아닙니다.

예제

다음 예제는 Person 클래스를 정의합니다. 그런 다음 메서드를 CreateInstance(String) 호출하여 인스턴스화하지만 인수의 typeName 대/소문자가 형식의 FullName 속성과 일치하지 않으므로 메서드는 를 반환합니다 null. 예제에서 동일한 문자열을 오버로드에 CreateInstance(String, Boolean) 전달하고 비교가 대/소문자를 Person 구분하지 않아야 한다고 지정하면 클래스가 발견되고 Person 개체가 성공적으로 인스턴스화됩니다.

using System;
using System.Reflection;
using Contoso.Libraries;

namespace Contoso.Libraries
{
   public class Person
   {
      private string _name;

      public Person()
      { }

      public Person(string name)
      {
         this._name = name;
      }

      public string Name
      { get { return this._name; }
        set { this._name = value; } }

      public override string ToString()
      {
         return this._name;
      }
   }
}

public class Example
{
   public static void Main()
   {
      String fullName = "contoso.libraries.person";
      Assembly assem = typeof(Person).Assembly;
      Person p = (Person) assem.CreateInstance(fullName);
      if (! (p == null)) {
         p.Name = "John";
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p);
      }
      else {
         Console.WriteLine("Unable to instantiate a Person object " +
                           "with Assembly.CreateInstance(String)");
         // Try case-insensitive type name comparison.
         p = (Person) assem.CreateInstance(fullName, true);
         if (! (p == null)) {
            p.Name = "John";
            Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                              p.GetType().Name, p);
         }
         else {
            Console.WriteLine("Unable to instantiate a {0} object.",
                              fullName);
         }
      }
   }
}
// The example displays the following output:
//    Unable to instantiate a Person object with Assembly.CreateInstance(String)
//    Instantiated a Person object whose value is 'John'
Imports System.Reflection
Imports Contoso.Libraries

Namespace Contoso.Libraries
   Public Class Person
      Private _name As String 
   
      Public Sub New()
      End Sub 
   
      Public Sub New(name As String)
         Me._name = name
      End Sub 
   
      Public Property Name As String 
         Get 
            Return Me._name
         End Get 
         Set 
            Me._name = value
         End Set 
      End Property 
   
      Public Overrides Function ToString() As String 
         Return Me._name
      End Function 
   End Class
End Namespace 

Module Example
   Public Sub Main()
      Dim fullName As String = "contoso.libraries.person"
      Dim assem As Assembly = GetType(Person).Assembly
      Dim p As Person = CType(assem.CreateInstance(fullName),
                              Person)
      If p IsNot Nothing Then
         p.Name = "John"
         Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                           p.GetType().Name, p)
      Else
         Console.WriteLine("Unable to instantiate a Person object" +
                           "with Assembly.CreateInstance(String)")
         ' Try case-insensitive type name comparison.
         p = CType(assem.CreateInstance(fullName, true), Person)
         If p IsNot Nothing Then 
            p.Name = "John"
            Console.WriteLine("Instantiated a {0} object whose value is '{1}'",
                              p.GetType().Name, p)
         Else 
            Console.WriteLine("Unable to instantiate a {0} object.", 
                              fullName)
         End If   
      End If   
   End Sub
End Module
' The example displays the following output:
'    Unable to instantiate a Person object with Assembly.CreateInstance(String)
'    Instantiated a Person object whose value is 'John'

설명

런타임이 instance 찾을 typeNameAssembly 수 없는 경우 예외를 throw하는 대신 를 반환 null 합니다. 이 문제는 다음과 같은 경우에 발생할 수 있습니다.

  • 형식의 정규화된 이름을 지정하지 않았습니다.

  • 형식이 현재 Assembly instance 없습니다.

적용 대상

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

Source:
Assembly.cs
Source:
Assembly.cs
Source:
Assembly.cs

대/소문자 구분 검색 기능을 선택적으로 사용하고 지정된 문화권, 인수, 바인딩 및 활성화 특성을 사용하여, 지정된 형식을 이 어셈블리에서 찾은 다음 시스템 활성기를 사용하여 해당 인스턴스를 만듭니다.

public:
 virtual System::Object ^ CreateInstance(System::String ^ typeName, bool ignoreCase, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ args, System::Globalization::CultureInfo ^ culture, cli::array <System::Object ^> ^ activationAttributes);
public virtual object? CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, object[]? args, System.Globalization.CultureInfo? culture, object[]? activationAttributes);
public virtual object CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
public object CreateInstance (string typeName, bool ignoreCase, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, object[] args, System.Globalization.CultureInfo culture, object[] activationAttributes);
abstract member CreateInstance : string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
override this.CreateInstance : string * bool * System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo * obj[] -> obj
Public Overridable Function CreateInstance (typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object
Public Function CreateInstance (typeName As String, ignoreCase As Boolean, bindingAttr As BindingFlags, binder As Binder, args As Object(), culture As CultureInfo, activationAttributes As Object()) As Object

매개 변수

typeName
String

찾을 형식의 FullName입니다.

ignoreCase
Boolean

형식 이름의 대/소문자를 무시하면 true이고, 그러지 않으면 false입니다.

bindingAttr
BindingFlags

검색 수행 방법에 영향을 주는 비트 마스크입니다. 이 값은 BindingFlags의 비트 플래그 조합입니다.

binder
Binder

리플렉션을 사용하여 바인딩, 인수 형식의 강제 변환, 멤버 호출 및 MemberInfo 개체 검색을 사용할 수 있도록 하는 개체입니다. bindernull이면 기본 바인더가 사용됩니다.

args
Object[]

생성자에 전달되는 인수를 포함하는 배열입니다. 이 인수 배열은 실행될 생성자의 매개 변수와 개수, 순서 및 형식이 일치해야 합니다. 매개 변수가 없는 생성자가 필요한 경우 args가 빈 배열이거나 null이어야 합니다.

culture
CultureInfo

형식의 강제 변환을 제어하는 데 사용되는 CultureInfo 의 인스턴스입니다. 이 매개 변수가 null이면 현재 스레드에 대한 CultureInfo가 사용됩니다. 예를 들어 1,000은 각 문화권마다 다르게 표현되므로 1,000을 나타내는 문자열을 Double 값으로 변환하기 위해 이 매개 변수가 필요합니다.

activationAttributes
Object[]

활성화할 수 있는 하나 이상의 특성으로 이루어진 배열입니다. 일반적으로, 원격 개체를 활성화하는 데 필요한 URL을 지정하는 단일 UrlAttribute 개체가 포함된 배열입니다. 이 매개 변수는 클라이언트 활성 개체와 관련되어 있습니다. 클라이언트 활성화는 이전 버전과의 호환성을 위해 유지되지만 새로운 개발에는 권장되지 않는 레거시 기술입니다. 분산된 애플리케이션은 Windows Communication Foundation을 사용해야 합니다.

반환

지정된 형식의 인스턴스 또는 null이 없으면 typeName입니다. 제공된 인수는 형식을 확인하고 인스턴스를 만드는 데 사용된 생성자를 바인딩하는 데 사용됩니다.

구현

예외

typeName이 빈 문자열("") 또는 null 문자로 시작하는 문자열입니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었습니다.

typeName이(가) null인 경우

일치하는 생성자를 찾을 수 없습니다.

비어 있지 않은 활성화 특성 배열이 MarshalByRefObject에서 상속되지 않는 형식에 전달됩니다.

typeName 에 종속 어셈블리가 필요하지만 이 어셈블리를 찾을 수 없습니다.

typeName에 종속 어셈블리가 필요하며 이 어셈블리를 찾았지만 로드할 수 없습니다.

또는

현재 어셈블리가 리플렉션 전용 컨텍스트에 로드되었으며 typeName에 종속 어셈블리가 필요하지만 이 어셈블리가 미리 로드되지 않았습니다.

typeName 에는 종속 어셈블리가 필요하지만 파일은 현재 로드된 런타임에 유효한 어셈블리가 아닙니다.

적용 대상