首页 > 其他分享 >csharp_获取属性的字符串名称

csharp_获取属性的字符串名称

时间:2024-01-31 14:45:34浏览次数:17  
标签:Name csharp 字符串 new PropertySupport expression public string 属性

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

标签:Name,csharp,字符串,new,PropertySupport,expression,public,string,属性
From: https://www.cnblogs.com/zhuoss/p/17999223

相关文章

  • 9.附加属性
    我们在学习布局控件时,其实也已经使用过附加属性了。下面我们来看一些例子<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><ButtonGrid.Row="0"Content="按钮1"/><But......
  • c# ComboBox控件的常用一些属性和用法、事件及数据绑定方法
    一、常用属性和用法1、Text:获取或设置与此控件关联的文本。//设置默认值this.comboBox1.Text="请选择内容";//orcomboBox1.Items.Add("请选择内容");comboBox1.SelectedIndex=0;2、SelectedIndex:获取或设置指定当前选定项的索引。(设置新索引会SelectedIndexChanged......
  • CSharp: iText 8.0 in donet 4.8.1
     usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.IO;usingSystem.Text;usingiText.IO.Font;usingiText.IO.Image;usingiText.Kernel.Font;usingiTe......
  • 8.依赖属性
    WPF的依赖属性系统,它是指WPF提供的一组服务,专门用来扩展WPF的属性功能,而受到这些服务支持的属性就称为依赖属性。WPF的依赖属性系统对于开发者而言,几乎是感知不到的,它通过DependencyProperty类型的一些静态方法成员,提供一系列注册依赖属性或附加属性的功能,让我们可以向依赖属性系......
  • 151. 反转字符串中的单词(中)
    目录题目法一、双指针法二、字符串常用操作题目给你一个字符串s,请你反转字符串中单词的顺序。单词是由非空格字符组成的字符串。s中使用至少一个空格将字符串中的单词分隔开。返回单词顺序颠倒且单词之间用单个空格连接的结果字符串。注意:输入字符串s中可能会......
  • C# - 使用 Spire.PDF 将HTML网页、网页内容、HTML字符串转换为PDF
    将HTML转换为PDF可实现格式保留、可靠打印、文档归档等多种用途,满足不同领域和情境下的需求。本文将通过以下两个示例,演示如何使用第三方库Spire.PDFfor.NET和QT插件在C#中将Html网页(URL)或HTML字符串转为PDF文件。 HTML转PDF所需工具:1.Spire.PDFfor.NET首先需要安装S......
  • C# - 将HTML网页、HTML字符串转换为PDF
    将HTML转换为PDF可实现格式保留、可靠打印、文档归档等多种用途,满足不同领域和情境下的需求。本文将通过以下两个示例,演示如何使用第三方库Spire.PDFfor.NET和QT插件在C#中将Html网页(URL)或HTML字符串转为PDF文件。 HTML转PDF所需工具:1.Spire.PDFfor.NET首先需要安装Sp......
  • csharp 发布订阅 接口
    event_learn\MyEvent.csnamespaceMyEvent;publicdelegatevoidMyEventHandler(objectsender,EventArgse);//定义一个自定义的事件接口,包含一个事件声明publicinterfaceIMyEvent{//使用event关键字声明一个事件,指定委托类型和事件名称eventMyEven......
  • csharp 发布订阅 传递参数
    event_learn\Program.cs//定义一个派生自EventArgs的自定义类,用于封装数据publicclassMyEventArgs:EventArgs{//定义一个公共的字符串属性,用于存储和获取数据publicDateTime?EmitDate{get;set;}}//定义一个发布者类,它有一个MyEvent事件public......
  • csharp 发布订阅 设计模式
    event_learn\Program.csusingSystem;//定义一个发布者类,它有一个MyEvent事件publicclassPublisher{//声明一个事件,使用EventHandler委托类型publiceventEventHandlerMyEvent;//定义一个触发事件的方法protectedvirtualvoidOnMyEvent()......