如何在c#中更改运行时的类属性名称? [英] How to change class property name at runtime in c#?

查看:302
本文介绍了如何在c#中更改运行时的类属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中在运行时更改类属性名称?



让我们说,

公共类客户

{

public int CustId {get; set;}

public string CusName {get;设置;}



}



因此,在运行时我想更改属性名称,让我们说CustId将CustomerId&

CusName改为客户名称。



我的尝试:



我想使用反射我们可以实现这些。

How to Change class property name at runtime in C#?

Let's Say,
public class Customer
{
public int CustId {get; set;}
public string CusName {get; set;}

}

So, during runtime I want to change property name, lets say CustId to "CustomerId" &
CusName to "CustomerName".

What I have tried:

I guess using reflection we can achieve these.

推荐答案

根据评论,你正在使用< a href =http://joshclose.github.io/CsvHelper/> CsvHelper [ ^ ]将数据导出到CSV文件,并且您想要更改列的标题。



无需更改要执行此操作的属性名称。只需设置类映射 [ ^ ]:

Based on the comments, you're using CsvHelper[^] to export data to a CSV file, and you want to change the titles of the columns.

There's no need to change the property name to do that. Just set up a class mapping[^]:
public class CustomerMap : CsvClassMap<Customer>
{
    public CustomerMap()
    {
        Map(m => m.CustId).Name("CustomerId");
        Map(m => m.CusName).Name("CustomerName");
    }
}
...
using (var writer = new CsvWriter(...))
{
    writer.Configuration.RegisterClassMap<CustomerMap>();
    writer.WriteRecords(listOfCustomers);
}


你为什么要这样做?如果你看看面向对象的编程如何工作
Why do you want to do this? It makes no sense if you look to how Object orientated programming works


这篇关于如何在c#中更改运行时的类属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆