如
public class A
{
public int Pro1 { get; set; }
public int Pro2 { get; set; }
}A a = new A();
如何获取a.Pro1的字符串名称。即如何获得"Pro1"
public class A
{
public int Pro1 { get; set; }
public int Pro2 { get; set; }
}A a = new A();
如何获取a.Pro1的字符串名称。即如何获得"Pro1"
using System.Reflection;
Type t = typeof(A);
foreach(PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BidngFlags.Public))
{
Console.WriteLine(pi.Name);
}
System.Reflection.PropertyInfo[] propertys =对像.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo info in propertys)
{
//info.Name 属性名称
}
{
A a = new A();
Type t = a.GetType(); foreach(PropertyInfo info in t.GetProperties())
{
Console.WriteLine(info.Name);
} Console.ReadLine();
}
如果我已经知道是"Pro1"就不需要再获取这个值了啊~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); A AA = new A();
AA.Pro1 = 1;
AA.Pro2 = 2; B BB = new B();
BB.Pro1 = 1;
BB.Pro2 = 2; MessageBox.Show(((DefaultValueAttribute)(AA.GetType().GetProperty(AA.GetNeededPropName)
.GetCustomAttributes(typeof(DefaultValueAttribute), false)[0])).Value.ToString()); //1 MessageBox.Show(((DefaultValueAttribute)(BB.GetType().GetProperty(BB.GetNeededPropName)
.GetCustomAttributes(typeof(DefaultValueAttribute), false)[0])).Value.ToString()); //4
} class X
{
virtual public String GetNeededPropName { get; set; }
} class A : X
{
[DefaultValue(1)]
public int Pro1 { get; set; } [DefaultValue(2)]
public int Pro2 { get; set; } public override string GetNeededPropName
{
get
{
return "Pro1";
}
}
} class B : X
{
[DefaultValue(3)]
public int Pro1 { get; set; } [DefaultValue(4)]
public int Pro2 { get; set; } public override string GetNeededPropName
{
get
{
return "Pro2";
}
}
}
}
}
非常感谢你的回复~但是我不得不说这不是我想要的结果~
按照你的方法~这个类就只能返回一个名称,而我想要的是根据属性来返回这个属性的名称~
比如Pro1就返回“Pro1”,Pro2就返回"Pro2",而不是在代码中硬编码~
听不懂了,是不是返回defaultvalue特征设置的那个属性名?[defaultvalue(1)]
pro1pro2返回pro1?或者你写段伪代码说明你的思路
如果说一定要有一个办法来解决,我只想到一个运算符:& 也就是取变量地址运算符,这是unsafe的,但也许对lz有用。不过我还是没有办法给出一个满足lz要求的解决方案来
{
static void Main(string[] args)
{
A a = new A(10,20);
Console.WriteLine(a.GetPropertyName(a,a.Pro1));
}
} public class A
{
public A(int pro1, int pro2)
{
Pro1 = pro1;
Pro2 = pro2;
} public int Pro1 { get; set; }
public int Pro2 { get; set; } public string GetPropertyName(A a, object value)
{
foreach (PropertyInfo info in this.GetType().GetProperties())
{
if (info.GetValue(a, null).ToString() == value.ToString())
{
return info.Name;
}
}
return string.Empty;
//throw new Exception("Not Found This Property");
}
}
{
static void Main(string[] args)
{
A a = new A(10,20);
Console.WriteLine(A.GetPropertyName(a,a.Pro1));
}
} public class A:PropertyEnmunble<A>
{
public A(int pro1, int pro2)
{
Pro1 = pro1;
Pro2 = pro2;
} public A()
{
Pro1 = -1;
Pro2 = -2;
} public int Pro1 { get; set; }
public int Pro2 { get; set; }
} public class PropertyEnmunble<T>
{
public static string GetPropertyName(T a, object value)
{
foreach (PropertyInfo info in a.GetType().GetProperties())
{
if (info.GetValue(a, null).ToString() == value.ToString())
{
return info.Name;
}
}
return string.Empty;
}
}
使用这个方法的前提是实体中属性的值都得不一样~否则就不对了~
比如
static void Main(string[] args)
{
A a = new A(20, 20);
Console.WriteLine(A.GetPropertyName(a, a.Pro1));
Console.WriteLine(A.GetPropertyName(a, a.Pro2));
Console.ReadLine();
}两次输出都是Pro1
但是我想要的结果是输出Pro1和Pro2
{
putlic static readonly string ReadID="ID";
public static readonly string ReadContetn="Content";public int ID{get;set;}
public string Content {get;set;}}其实他想要的是这种效果,使用Read字段可以获得属性所对应的名称,但是以静态字段的方法没办法同步更新,也就是说属性改了要是没改字段就会出问题,这种问题不是编译其间可以预防的,所以他想要的结果是在编译的时候就能提示这个字段不是这个类的,我也在考虑着问题,当确实没有好的解决办法
public class A
{
public int PropA { get; set; } public string PropB { get; set; }
} class Program
{
static void Main(string[] args)
{
var s = GetPropName<A>(a => a.PropB);
Console.WriteLine(s);
} static string GetPropName<T>(Expression<Func<T, object>> expr)
{
switch (expr.Body.NodeType)
{
case ExpressionType.MemberAccess:
return ((MemberExpression)expr.Body).Member.Name;
case ExpressionType.Convert:
return ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
default:
return null;
}
} }