110,534
社区成员
发帖
与我相关
我的任务
分享
b.woshishui.?
public static class PropertySupport
{
public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
{
throw new ArgumentNullException("propertyExpression");
}
var memberExpression = propertyExpression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException(Resources.PropertySupport_NotMemberAccessExpression_Exception, "propertyExpression");
}
var property = memberExpression.Member as PropertyInfo;
if (property == null)
{
throw new ArgumentException(Resources.PropertySupport_ExpressionNotProperty_Exception, "propertyExpression");
}
var getMethod = property.GetGetMethod(true);
if (getMethod.IsStatic)
{
throw new ArgumentException(Resources.PropertySupport_StaticExpression_Exception, "propertyExpression");
}
return memberExpression.Member.Name;
}
}
var propertyName = PropertySupport.ExtractPropertyName(()=>this.PropA);
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class b
{
public string name { get; set; }
public int idid { get; set; }
public string woshishui { get; set; }
}
class Program
{
static void Main(string[] Args)
{
b ab = new b() { name = "name", idid = 123, woshishui = "shui" };
string PropertyName = typeof(b).GetProperties().Where(x => ab.woshishui == x.GetValue(ab, null).ToString()).First().Name;
Console.WriteLine(PropertyName);
}
}
}