Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this my class:
C#
public partial class DaysLast       
      {      
        public Nullable<int> DAY10 { get; set; }
        public Nullable<int> DAY11 { get; set; }
        public Nullable<int> DAY12 { get; set; }
        public Nullable<int> DAY13 { get; set; }
        public Nullable<int> DAY14 { get; set; }
        public Nullable<int> DAY15 { get; set; }
        public Nullable<int> DAY16 { get; set; }      
      }


    DaysLast Prod = new DaysLast();  
    for (int i = 0; i < 16; i++)               
            {
     if (test1.date==test2.date){

      Prod[i]=test2[i].Hour
     }

    }

My question is can we change dynamically property name. So that i will have value
C#
{DAY10 =5,Day11=2, Day12=null,....}
or   {DAY7 =5,Day8=2, Day10=2,....}

I tried to used dynamic flexible = new System.Dynamic.ExpandoObject();
but work with values and property name didn't changed.
C#
dynamic flexible = new System.Dynamic.ExpandoObject(); 
             flexible.DAY00 =test2[i].Hour


What I have tried:

My question is can we change dynamically property name. So that i will have value
C#
{DAY10 =5,Day11=2, Day12=null,....}
or   {DAY7 =5,Day8=2, Day10=2,....}

I tried to use
C#
dynamic flexible = new System.Dynamic.ExpandoObject();
but work with values and property name didn't changed.
Posted
Updated 18-Nov-19 8:25am
v2
Comments
Christiaan van Bergen 2-May-18 11:17am    
Changing propertynames? NAFAIK. But, maybe if you tell us a bit more about why you would like to have this ability, we can help you find a suitable way to do just that.
OriginalGriff 2-May-18 11:40am    
Why do you think that would be a good idea?
What are you trying to do that you think that would be necessary and helpful?
Member 13808254 2-May-18 12:19pm    
This required for my project.That's why i am thinking like this
RmcbainTheThird 10-Jun-19 7:49am    
It may be required but what is the justification? What will changing the property names accomplish? What problem will it solve?

1 solution

If you insist on wanting to "change the property name (which I personally think is a TERRIBLE idea), just create a couple of getter/setter extension method that does what you want:

C#
public static int MyRenamedProperty(this MyObject obj)
{
    return obj.DAY10;
}

public static void MyRenamedProperty(this MyObject obj, int value)
{
    obj.DAY10 = value;
}


Note that this is just a suggestion, and may or may not represent your best option, and that I don't care either way.

EDIT (2022.6.10) -----------------
I'm not sure why this was voted down, or why it took almost 4 years to do it, but what-the-f*ck-ever...
 
Share this answer
 
v2

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