c# 大师们,如何将一个实体的属性名转换成string,或者变量名转换成string?

richwong 2011-07-09 05:43:24

比如
public class b
{
public string name{get;set;}
public int idid{get;set;}
public string woshishui{get;set;}

}

实例化b,得到 b.name ,b.idid, b.woshishui;

怎样能把b.woshishui这个属性,转换成"b.woshishui"或者 "woshishui"。

通过类反射PropertyInfo[] properties = type.GetProperties(); 然后遍历properties,就能得到全部的属性名和值,但是我只想得到任意一个。

试了很多方法行不通, 大师们,有招吗?

比如 Type bT = b.woshishui.GetType(); 然后bT.name 值就是"woshishui"..... 这是YY的,这个方法GetType不出,他是从属性值里面get的。。 。。。
...全文
1814 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
telossoft 2011-11-22
  • 打赏
  • 举报
回复
我们经常用typeof()
1L其实就是需要一个类似的propertyInfoOf()的功能, 这样不需要硬编码了
比如Orm中就可以写 "select from User where " + propertyInfoOf(User.Age).Name + " = 100"这样的语句了, 这样写即你把Age属性名改了VS会自动修改这个语句,或者编译时被发现这个错误
dragonimp 2011-09-07
  • 打赏
  • 举报
回复
我也想要这个功能,目的就是想要在编译期间检查属性,用字符串就得不到检查。

我的问题是,现在用的是framework2.0怎么办?linq不能用啊。有没有什么招不?



举例场景:以下attr和Member两个是String的,这样在调用时是硬编码,得不到编译器的类型检查。

public static void BindData(Control ctl, string attr, object ds, string Member)
{

ctl.DataBindings.Clear();
ctl.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
ctl.DataBindings.Add(attr, ds, Member);//.DataSourceUpdateMode= DataSourceUpdateMode.OnPropertyChanged;

}
richwong 2011-07-09
  • 打赏
  • 举报
回复
谢8楼,管用,哈哈
ruanwei1987 2011-07-09
  • 打赏
  • 举报
回复
showjim 2011-07-09
  • 打赏
  • 举报
回复
b.woshishui.?

从.?开始,它的值仅仅就是一个字符串了,与b或b.woshishui除了值以外,没有任何关系了。

可以参考8楼的方法把b.woshishui转换成一个代表式,而不是值
沝林 2011-07-09
  • 打赏
  • 举报
回复
prism源码里有类似的功能:


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;
}
}


使用时用Lambda表达式指向属性

var propertyName = PropertySupport.ExtractPropertyName(()=>this.PropA);


richwong 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 sp1234 的回复:]
性能问题,当然是几百倍上千倍的损失。除非不这样做你就编写不出可用的代码,否则不要滥用反射。
[/Quote]

可以不这样做,直接手写string,但是我想为了减少手写错误,和维护更改,我想从实体属性名里传入,因为手写的string全部和实体属性一致。

就是不知道c#有没有更好的办法能把 属性名轻易的变成“属性名”字符串。 折腾半天不出来,因为一用属性,属性就出来值或Null,得不到他自己的Name。。。。
  • 打赏
  • 举报
回复
性能问题,当然是几百倍上千倍的损失。除非不这样做你就编写不出可用的代码,否则不要滥用反射。
richwong 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]
b.woshishui.GetType()

你这样还是要知道 "woshishui" 还不是硬编码吗?

没搞懂你想要什么。
[/Quote]

就是类属性的反射,类属性的反射是针对类所有属性遍历反射的,我只想反射任意其中一个。

还有另外一种方法说明我的问题:

string aa="";

这时候我让程序知道 aa就是"aa"字符串,不是变量。 就是把字符串转换成变量的反向要求。呵呵,比较特殊的要求。

比如 通过一个方法 GETNAME(dynamic Propertie) 返回这个Propertie名的字符串,即"aa" 而不是aa的值。。。。。
richwong 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caozhy 的回复:]
C# code
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
public class b
{
public string name { get; set;……
[/Quote]

谢谢大师,不设置值可以吗,而且这样会反射全部类的属性啊,然后再根据值来查找,会不会性能比较不好呢
threenewbee 2011-07-09
  • 打赏
  • 举报
回复
和fangxinggood一样不知道你什么意思。
threenewbee 2011-07-09
  • 打赏
  • 举报
回复
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);
}
}
}
机器人 2011-07-09
  • 打赏
  • 举报
回复
b.woshishui.GetType()

你这样还是要知道 "woshishui" 还不是硬编码吗?

没搞懂你想要什么。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧