Type of()とGetType()は,オブジェクトのすべての共通属性とすべての共通メソッドGetProperties()GetMethods()を取得する.
<1>
Type Of()とGetType()の違い
Type Of()とGetType()の違い using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @typeof
{
// TypeOf() GetType() :
//<1> TypeOf(): Class Type
//<2> GetType(): Class Type
//Typeof() GetType
//GetType() System.Object , ( )
//Typeof() int,string,String, ,
class Program
{
static void Main(string[] args)
{
// Class Type
var type = typeof(int);
Console.WriteLine(type); // System.int32
// Class Type
var type3 = typeof(Program);
Console.WriteLine(type3); // typeof.Program
//-----------------------------------------------------------
Program p = new Program();
// Class Type
var type2= p.GetType();
Console.WriteLine(type2); // typeof.Program
Console.ReadKey();
}
}
}
<.2>
オブジェクトタイプのすべての共通属性を取得します。GetProperties()とすべての公開メソッドGetMethods() using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace StringBuilders
{
class User
{
public User()
{
}
public void GetData()
{
}
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
private string Gender { get; set; }
}
class Program
{
static void Main(string[] args)
{
User u = new User();
// u 。 : User PropertyInfo
PropertyInfo[] strObject = u.GetType().GetProperties();
// User ( )
var strClass = typeof(User).GetProperties();
foreach (PropertyInfo v in strObject) // User
{
Console.WriteLine(v.Name);
}
Type c = typeof(User);
// Type 。 MethodInfo
MethodInfo[] methods = c.GetMethods();
foreach (MethodInfo m in methods) // User
{
Console.WriteLine(m.Name);
}
Console.ReadKey();
}
}
}
<3>
クラスインスタンスのタイプGetType()を取得 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class User
{
public string UserName { get; set; }
}
class Program
{
static void Main(string[] args)
{
User u = new User(); // User u
Type t = u.GetType(); // u
Console.WriteLine(t); // ConsoleApplication1.User
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace @typeof
{
// TypeOf() GetType() :
//<1> TypeOf(): Class Type
//<2> GetType(): Class Type
//Typeof() GetType
//GetType() System.Object , ( )
//Typeof() int,string,String, ,
class Program
{
static void Main(string[] args)
{
// Class Type
var type = typeof(int);
Console.WriteLine(type); // System.int32
// Class Type
var type3 = typeof(Program);
Console.WriteLine(type3); // typeof.Program
//-----------------------------------------------------------
Program p = new Program();
// Class Type
var type2= p.GetType();
Console.WriteLine(type2); // typeof.Program
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace StringBuilders
{
class User
{
public User()
{
}
public void GetData()
{
}
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
private string Gender { get; set; }
}
class Program
{
static void Main(string[] args)
{
User u = new User();
// u 。 : User PropertyInfo
PropertyInfo[] strObject = u.GetType().GetProperties();
// User ( )
var strClass = typeof(User).GetProperties();
foreach (PropertyInfo v in strObject) // User
{
Console.WriteLine(v.Name);
}
Type c = typeof(User);
// Type 。 MethodInfo
MethodInfo[] methods = c.GetMethods();
foreach (MethodInfo m in methods) // User
{
Console.WriteLine(m.Name);
}
Console.ReadKey();
}
}
}
<3>
クラスインスタンスのタイプGetType()を取得 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class User
{
public string UserName { get; set; }
}
class Program
{
static void Main(string[] args)
{
User u = new User(); // User u
Type t = u.GetType(); // u
Console.WriteLine(t); // ConsoleApplication1.User
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class User
{
public string UserName { get; set; }
}
class Program
{
static void Main(string[] args)
{
User u = new User(); // User u
Type t = u.GetType(); // u
Console.WriteLine(t); // ConsoleApplication1.User
Console.ReadKey();
}
}
}