还是简单封装一下吧,C# 示例L@_@K using System;namespace Scratch { class Program { static void Main(string[] args) { string propName = "IWanted";
if (!TypeExistsProperty(typeof(string), propName)) { Console.Write("Are you crazy?"); } Console.Read(); } public static bool TypeExistsProperty(Type givenType, string propertyName) { if (givenType == null || propertyName== null) return false; return ((givenType.GetProperty(propertyName)) != null); } } }
Type T= GetType(); if (T!= null) { PropertyInfo[] PropertyInfos = T.GetProperties(); foreach (PropertyInfo P in PropertyInfos) textBox1.AppendText((P!=null?P.Name:"") + "\r\n"); }
if (pi == null)
{
throw new Exception("不存在名为" + propName + "的属性");
}
你的就改成这个样:PropertyInfo pi = busObject.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
if (pi == null)
{
throw new Exception("不存在名为" + propName + "的属性");
}
using System;namespace Scratch
{
class Program
{
static void Main(string[] args)
{
string propName = "IWanted";
if (!TypeExistsProperty(typeof(string), propName))
{
Console.Write("Are you crazy?");
}
Console.Read();
} public static bool TypeExistsProperty(Type givenType, string propertyName)
{
if (givenType == null || propertyName== null)
return false; return ((givenType.GetProperty(propertyName)) != null);
}
}
}
if (T!= null)
{
PropertyInfo[] PropertyInfos = T.GetProperties();
foreach (PropertyInfo P in PropertyInfos)
textBox1.AppendText((P!=null?P.Name:"") + "\r\n");
}