紀錄 Reflection 的作法
City Class
namespace ReflectionProperty
{
public class City
{
public int CityId { get; set; }
public string CityName { get; set; }
}
}
Program.cs Codeusing System.Reflection;
namespace ReflectionProperty
{
class Program
{
static void Main(string[] args)
{
// 抓 Type 的 3 種方法
// 方法1:System.Object.GetType()
City c = new City();
Type t = c.GetType();
// 方法2:Type.GetType()
Type t = Type.GetType("ReflectionProperty.City");
// 方法3: Typeof()
Type t = typeof(City);
if (t == null)
{
Console.WriteLine("找不到 Type");
return;
}
Console.WriteLine(string.Format("Type FullName:{0}",t.FullName));
// 利用 Type.GetProperties() 來抓 Property
PropertyInfo[] pis = t.GetProperties();
foreach (PropertyInfo pi in pis)
{
Console.WriteLine(string.Format("Property Name:{0}", pi.Name));
}
}
}
}
![[C#] Reflection - Property](https://farm6.staticflickr.com/5795/20719459235_96203b5b9e_o.jpg)
沒有留言:
張貼留言