名前を指定したアセンブリと、指定したパラメータに最も一致するコンストラクタを使用して、名前を指定した型のインスタンスを作成します。
Overloads Public Shared Function CreateInstance( _
ByVal assemblyName As String, _ ByVal typeName As String, _ ByVal ignoreCase As Boolean, _ ByVal bindingAttr As BindingFlags, _ ByVal binder As Binder, _ ByVal args() As Object, _ ByVal culture As CultureInfo, _ ByVal activationAttributes() As Object, _ ByVal securityInfo As Evidence _) As ObjectHandle
[C#]
public static ObjectHandle CreateInstance(stringassemblyName,stringtypeName,boolignoreCase,BindingFlagsbindingAttr,Binderbinder,object[] args,CultureInfoculture,object[] activationAttributes,EvidencesecurityInfo);
[C++]
public: static ObjectHandle* CreateInstance(String* assemblyName,String* typeName,boolignoreCase,BindingFlagsbindingAttr,Binder* binder,Object* args __gc[],CultureInfo* culture,Object* activationAttributes __gc[],Evidence* securityInfo);
[JScript]
public static function CreateInstance(
assemblyName : String,typeName : String,ignoreCase : Boolean,bindingAttr : BindingFlags,binder : Binder,args : Object[],culture : CultureInfo,activationAttributes : Object[],securityInfo : Evidence) : ObjectHandle;
パラメータ
- assemblyName
typeName という名前の型をシークする場所となるアセンブリの名前。 assemblyName が null 参照 (Visual Basic では Nothing) の場合は、実行中のアセンブリが検索されます。 - typeName
推奨される型の名前。 - ignoreCase
typeName の検索で大文字と小文字を区別するかどうかを指定するブール値。 ignoreCase が true の場合、検索では大文字と小文字は区別されません。 - bindingAttr
typeName コンストラクタの検索に影響を与える 0 個以上のビット フラグの組み合わせ。 bindingAttr が 0 の場合は、大文字と小文字を区別してパブリック コンストラクタを検索します。 - binder
bindingAttr および args を使用して、 typeName コンストラクタをシークおよび識別するオブジェクト。binder が null 参照 (Visual Basic では Nothing) の場合は、既定のバインダが使用されます。 - args
呼び出すコンストラクタのパラメータと、数、順序、および型が一致する引数の配列。 args が空の配列または null 参照 (Visual Basic では Nothing) である場合は、パラメータをとらないコンストラクタ (既定のコンストラクタ) が呼び出されます。 - culture
args から typeName コンストラクタに対して宣言された仮型への強制変換を制御するカルチャ固有の情報。culture が null 参照 (Visual Basic では Nothing) の場合は、現在のスレッドの CultureInfo が使用されます。 - activationAttributes
アクティべーションに参加できる 1 つ以上の属性の配列。 - securityInfo
セキュリティ ポリシーがコードに与えるアクセス許可を決定するために使用する情報。
戻り値
新しく作成されたインスタンスにアクセスするために、ラップを解除する必要があるハンドル。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | typeName が null 参照 (Visual Basic では Nothing) です。 |
MissingMethodException | 一致するコンストラクタが見つかりませんでした。 |
TypeLoadException | assemblyName で、 typename が見つかりませんでした。 |
FileNotFoundException | assemblyName が見つかりませんでした。 |
MethodAccessException | 呼び出し元に、このコンストラクタを呼び出すためのアクセス許可がありません。 |
MemberAccessException | 抽象クラスのインスタンスを作成できません。または、このメンバが遅延バインディングの機構を使用して呼び出されました。 |
TargetInvocationException | コンストラクタがリフレクションによって呼び出されたため、例外がスローされました。 |
InvalidComObjectException | COM 型が GetTypeFromProgID または GetTypeFromCLSID を使用して取得されていません。 |
NotSupportedException | TypedReference 型、ArgIterator 型、および RuntimeArgumentHandle 型の作成はサポートされていません。
または activationAttributes が空の配列ではありません。また、作成される型が MarshalByRefObject から派生していません。 |
解説
ObjectHandle.Unwrap を使用して戻り値のラップを解除します。
使用例
[Visual Basic, C#, JScript] CreateInstance メソッドのオーバーロードの例を次に示します。
[Visual Basic, C#, JScript] このコード例を実行するには、アセンブリの完全限定名を指定する必要があります。アセンブリの完全限定名を取得する方法については、「 アセンブリ名 」を参照してください。
Dim hdlSample As ObjectHandle
Dim myExtenderInterface As IMyExtenderInterface
Dim argOne As String = "Value of argOne"
Dim argTwo As Integer = 7
Dim args As Object() = {argOne, argTwo}
' Uses the UrlAttribute to create a remote object.
Dim activationAttributes() = {New UrlAttribute("https://localhost:9000/MySampleService")}
' Activates an object for this client.
' You must supply a valid fully qualified assembly name here.
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "samplenamespace.sampleclass", True, BindingFlags.Instance Or BindingFlags.Public, Nothing, args, Nothing, activationAttributes, Nothing)
myExtenderInterface = CType(hdlSample.Unwrap(), IMyExtenderInterface)
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"))
[C#]
ObjectHandle hdlSample;
IMyExtenderInterface myExtenderInterface;
string argOne = "Value of argOne";
int argTwo = 7;
object[] args = {argOne, argTwo};
// Uses the UrlAttribute to create a remote object.
object[] activationAttributes = {new UrlAttribute("https://localhost:9000/MySampleService")};
// Activates an object for this client.
// You must supply a valid fully qualified assembly name here.
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken",
"samplenamespace.sampleclass",
true,
BindingFlags.Instance|BindingFlags.Public,
null,
args,
null,
activationAttributes,
null);
myExtenderInterface = (IMyExtenderInterface)hdlSample.Unwrap();
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));
[JScript]
var hdlSample : ObjectHandle;
var myExtenderInterface : IMyExtenderInterface;
var argOne : String = "Value of argOne";
var argTwo : int = 7;
var args : Object[] = [argOne, argTwo];
// Uses the UrlAttribute to create a remote object.
var activationAttributes : Object[] = [new UrlAttribute("https://localhost:9000/MySampleService")];
// Activates an object for this client.
// You must supply a valid fully qualified assembly name here.
hdlSample = Activator.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken",
"samplenamespace.sampleclass",
true,
BindingFlags.Instance|BindingFlags.Public,
null,
args,
null,
activationAttributes,
null);
myExtenderInterface = IMyExtenderInterface(hdlSample.Unwrap());
Console.WriteLine(myExtenderInterface.SampleMethod("Bill"));
[C++] C++ のサンプルはありません。Visual Basic、C#、および JScript のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- SecurityPermission (デリゲートのインスタンスの作成時にアンマネージ コードを呼び出すために必要なアクセス許可)SecurityPermissionFlag/UnmanagedCode (関連する列挙体)
- ReflectionPermission (すべての型のメンバに対して操作を呼び出すために必要なアクセス許可)ReflectionPermissionFlag/MemberAccess (関連する列挙体)
参照
Activator クラス | Activator メンバ | System 名前空間 | Activator.CreateInstance オーバーロードの一覧