Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
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.
Posted
Updated 3-Mar-16 7:59am
Comments
F-ES Sitecore 3-Mar-16 13:19pm    
You can't do this, if you explain what you're trying to achieve there might be an alternative such as using a Dictionary or some other structure.
vaibhav10Dec1987 3-Mar-16 13:45pm    
Actually I am implementing file write functionality, in which I got a list having type customer but while display column name to csv file I want "CustomerId" not a "CustId". & If I manipulate these things at the time of writing a file it may cause performance issue because of large record.

Note : While writing a csv file I used Nuget package CsvHelper.
Afzaal Ahmad Zeeshan 3-Mar-16 14:07pm    
No, reflection is not used to modify the assemblies. What I think would be possible, it to use attributes and pass other name as an attribute. Like

[RuntimeName("CustomerID")]

But you will have to write that attribute yourself.

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[^]:
C#
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);
}
 
Share this answer
 
Comments
vaibhav10Dec1987 8-Mar-16 12:51pm    
Hi Richard, Thanks for the solution. It works. But in my case class name & their properties are getting in runtime. So in that scenario I am stuck. It would be great if you help me out these.
Why do you want to do this? It makes no sense if you look to how Object orientated programming works
 
Share this answer
 
Comments
vaibhav10Dec1987 3-Mar-16 13:46pm    
Actually I am implementing file write functionality, in which I got a list having type customer but while display column name to csv file I want "CustomerId" not a "CustId". & If I manipulate these things at the time of writing a file it may cause performance issue because of large record.

Note : While writing a csv file I used Nuget package CsvHelper.
Dave Kreskowiak 3-Mar-16 13:56pm    
Does the library you're using support attributes on the data items? Like [Description] or [Name] or something like that.

The idea is that you don't change the property names but add an attribute that tells the export code to export a different name in the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900