「C#」typeofでオブジェクトの型を取得する

書式
typeof(オブジェクトの型)
typeof() を使用してオブジェクトの型を取得します。演算子 typeof ( GetType Visual Basic) は、 を表す オブジェクトを Type 取得するために使用されます String 。
このオブジェクトから、 メソッドを使用して、開始位置と長さを受け取るオーバーロードを表す Type GetMethod MethodInfo String.Substring を取得します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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);

    }
  }
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
System.Int32
System.Text.StringBuilder
System.Byte[]
System.Int32 System.Text.StringBuilder System.Byte[]
System.Int32
System.Text.StringBuilder
System.Byte[]

 

C#

Posted by arkgame