62,269
社区成员
发帖
与我相关
我的任务
分享
public class TestClass
{
private string name;
public string Name
{
get { return string.IsNullOrEmpty(name) ? "Name" : name; }
set { name = value; }
}
private string id;
public string Id
{
get { return string.IsNullOrEmpty(id) ? "Id" : id; }
set { id = value; }
}
public string GetProName(string proValue)
{
Type t = this.GetType();
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (pi.Name == proValue)
return pi.Name;
}
return "";
}
}
static void Main(string[] args)
{
TestClass tc = new TestClass();
Console.WriteLine(tc.GetProName(tc.Name));
Console.WriteLine(tc.GetProName(tc.Id));
}
/*
输出:
Name
Id
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Type t = typeof(Form1);
MethodInfo[] info = t.GetMethods();
foreach (MethodInfo i in info)
{
MessageBox.Show(i.Name);
}
}
}
}