「C#」typeofでオブジェクトの型を取得する
書式
typeof(オブジェクトの型)
typeof() を使用してオブジェクトの型を取得します。演算子 typeof ( GetType Visual Basic) は、 を表す オブジェクトを Type 取得するために使用されます String 。
このオブジェクトから、 メソッドを使用して、開始位置と長さを受け取るオーバーロードを表す Type GetMethod MethodInfo String.Substring を取得します。
使用例
using System;
using System.Text;
namespace StringBuilderDemo
{
class Program
{
static void Main(string[] args)
{
Type resA = typeof(int);
Console.WriteLine(resA);
Type resB = typeof(StringBuilder);
Console.WriteLine(resB);
Type resC = typeof(byte[]);
Console.WriteLine(resC);
}
}
}
using System;
using System.Text;
namespace StringBuilderDemo
{
class Program
{
static void Main(string[] args)
{
Type resA = typeof(int);
Console.WriteLine(resA);
Type resB = typeof(StringBuilder);
Console.WriteLine(resB);
Type resC = typeof(byte[]);
Console.WriteLine(resC);
}
}
}
using System; using System.Text; namespace StringBuilderDemo { class Program { static void Main(string[] args) { Type resA = typeof(int); Console.WriteLine(resA); Type resB = typeof(StringBuilder); Console.WriteLine(resB); Type resC = typeof(byte[]); Console.WriteLine(resC); } } }
実行結果
System.Int32
System.Text.StringBuilder
System.Byte[]
System.Int32
System.Text.StringBuilder
System.Byte[]
System.Int32 System.Text.StringBuilder System.Byte[]