怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理
  822 随笔 :: 0 文章 :: 2 评论 :: 16万 阅读

PropertySupport\Person.cs

public class Person
{
public string Name { get; set; }
public string getPropertyName()
{
return PropertySupport.ExtractPropertyName(() => Name);
}
}

PropertySupport\Program.cs

Person person = new Person();
string propertyName = PropertySupport.ExtractPropertyName(() => person.Name);
Console.WriteLine(propertyName); // 输出 "Name"
Console.WriteLine(person.getPropertyName()); // 输出 "Name"

PropertySupport\PropertySupport.cs

using System.Linq.Expressions;
public static class PropertySupport
{
public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
if (propertyExpression == null)
throw new ArgumentNullException(propertyExpression?.Name);
return ExtractPropertyNameFromLambda(propertyExpression);
}
internal static string ExtractPropertyNameFromLambda(LambdaExpression expression)
{
if (expression == null)
throw new ArgumentNullException(expression?.Name);
var memberExpression = expression.Body as MemberExpression;
// if (memberExpression == null)
// throw new ArgumentException(Resources.PropertySupport_NotMemberAccessExpression_Exception, expression.Name);
// var property = memberExpression.Member as PropertyInfo;
// if (property == null)
// throw new ArgumentException(Resources.PropertySupport_ExpressionNotProperty_Exception, expression.Name);
// var getMethod = property.GetMethod;
// if (getMethod.IsStatic)
// throw new ArgumentException(Resources.PropertySupport_StaticExpression_Exception, expression.Name);
return memberExpression?.Member.Name!;
}
}
posted on   超级无敌美少男战士  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2023-01-31 express_javscript_webframe
点击右上角即可分享
微信分享提示