001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053
|
Public Class Sample
Public Sub New()
Console.WriteLine("インスタンスが生成されました。")
End Sub
Public Sub New(ByVal arg As String)
Console.WriteLine("インスタンスが生成されました。 引数は{0}です。", arg)
End Sub
Public Sub New(ByVal arg1 As Integer, ByVal arg2 As String)
Console.WriteLine("インスタンスが生成されました。 引数は{0}, {1}です。", arg1, arg2)
End Sub
Public Sub TestMethod()
Console.WriteLine("メソッドが呼び出されました。")
End Sub
End Class
Public Class Test
Public Shared Sub Main()
Dim type As Type = GetType(Sample)
Dim instance As Object
' 引数をとらないコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance(type, System.Reflection.BindingFlags.CreateInstance, Nothing, Nothing, Nothing)
' Stringを引数にとるコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance(type, System.Reflection.BindingFlags.CreateInstance, Nothing, New Object() {"テスト"}, Nothing)
' Int32とStringを引数にとるコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance(type, System.Reflection.BindingFlags.CreateInstance, Nothing, New Object() {123, "テスト"}, Nothing)
' 生成されたインスタンスのメソッドを呼び出す
Dim sample As Sample = DirectCast(instance, Sample)
sample.TestMethod()
End Sub
End Class
|
001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047
|
public class Sample
{
public Sample()
{
Console.WriteLine( "インスタンスが生成されました。" );
}
public Sample( string arg )
{
Console.WriteLine( "インスタンスが生成されました。 引数は{0}です。", arg );
}
public Sample( int arg1, string arg2 )
{
Console.WriteLine( "インスタンスが生成されました。 引数は{0}, {1}です。", arg1, arg2 );
}
public void TestMethod()
{
Console.WriteLine( "メソッドが呼び出されました。" );
}
}
public class Test
{
public static void Main()
{
Type type = typeof( Sample );
object instance;
// 引数をとらないコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance( type, System.Reflection.BindingFlags.CreateInstance, null, null, null );
// Stringを引数にとるコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance( type, System.Reflection.BindingFlags.CreateInstance, null, new object[] { "テスト" }, null );
// Int32とStringを引数にとるコンストラクタを使用してインスタンスを作成する
instance = System.Activator.CreateInstance( type, System.Reflection.BindingFlags.CreateInstance, null, new object[] { 123, "テスト" }, null );
// 生成されたインスタンスのメソッドを呼び出す
Sample sample = instance as Sample;
sample.TestMethod();
}
}
|