Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Add properties/variable to a class at runtime?

So say I have the following class I created...

Code
Public Class House
    Public Property Door As String
    Public Property Roof As String
    Public Property Window As String
End Class

What if I wanted to add another property to that Class, like Window2 during runtime, is it possible?

Something like, House.AddProperty("Window2", String)
You'd be better off just making like a Dictionary<string, string> and adding like this myProperties.Add("window", "whatever value"); instead of trying to edit it at runtime.

[Image: Syr3DlV.png]
(Apr 2, 2019 07:56 PM)gigajew Wrote:  You'd be better off just making like a Dictionary<string, string> and adding like this myProperties.Add("window", "whatever value"); instead of trying to edit it at runtime.


This is much better! Thank you
(Apr 2, 2019 09:31 PM)Ajumma Wrote:  This is much better! Thank you
You can also achieve this by using custom dynamic types in certain scenarios
https://docs.microsoft.com/en-us/dotnet/...work-4.7.2
https://docs.microsoft.com/en-us/dotnet/...m_Object__

Or you can use 3rd party pacages
https://www.nuget.org/packages/Westwind.Utilities/
Code
public class House:Westwind.Utilities.Expando
    {
        public string Door { get; set; }
        public string Roof { get; set; }
        public string Window { get; set; }

        public House():base()
        {
        }

        public House(object instance):base(instance)
        {
        }
    }

Code
class Program
    {
        static void Main(string[] args)
        {
            dynamic myHouse = new ExpandoObject();

            myHouse.Door = "1";
            myHouse.Window = "white trim 49x49";

            Console.WriteLine(myHouse.Window);

            var house = new House();

            house.Door = "1";
            house.Roof = "big red";
            house.Window = "white trim 48x48";
            //One way to add
            house["Window1"]= "white trim 49x49";
            //Second way to add
            dynamic add = house;
            add.Windows2 = "white trim 50x50";          

            Console.WriteLine(house.Door);
            Console.WriteLine(house.Roof);
            Console.WriteLine(house.Window);
            Console.WriteLine(house["Window1"]);
            Console.WriteLine(add.Windows2);
            Console.ReadLine();

        }
    }
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58
(Apr 3, 2019 04:58 AM)itcorp Wrote:  You can also achieve this by using custom dynamic types in certain scenarios
https://docs.microsoft.com/en-us/dotnet/...work-4.7.2
https://docs.microsoft.com/en-us/dotnet/...m_Object__

Or you can use 3rd party pacages
https://www.nuget.org/packages/Westwind.Utilities/
Code
public class House:Westwind.Utilities.Expando
   {
       public string Door { get; set; }
       public string Roof { get; set; }
       public string Window { get; set; }

       public House():base()
       {
       }

       public House(object instance):base(instance)
       {
       }
   }

Code
class Program
   {
       static void Main(string[] args)
       {
           dynamic myHouse = new ExpandoObject();

           myHouse.Door = "1";
           myHouse.Window = "white trim 49x49";

           Console.WriteLine(myHouse.Window);

           var house = new House();

           house.Door = "1";
           house.Roof = "big red";
           house.Window = "white trim 48x48";
           //One way to add
           house["Window1"]= "white trim 49x49";
           //Second way to add
           dynamic add = house;
           add.Windows2 = "white trim 50x50";          

           Console.WriteLine(house.Door);
           Console.WriteLine(house.Roof);
           Console.WriteLine(house.Window);
           Console.WriteLine(house["Window1"]);
           Console.WriteLine(add.Windows2);
           Console.ReadLine();

       }
   }

Thanks for the suggestion, I'll save it for future reference but for now, I'd like to use as little 3rd party tools as possible if I can. I forget its there sometimes xD

(Apr 3, 2019 07:11 AM)Ajumma Wrote:  Thanks for the suggestion, I'll save it for future reference but for now, I'd like to use as little 3rd party tools as possible if I can. I forget its there sometimes xD


You're welcome,but i think using of dynamic it's a solution for you.All the best
buy me a coffee eth:0x88621A52aa5Eb3b6503628369687111810186c58


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Source] by HackersNov 19, 2024 01:51 PM 15 677 Nov 19, 2024 01:51 PM
Last Post: Hackers
[C] by lockheadApr 22, 2021 07:10 AM 0 146 Apr 22, 2021 07:10 AM
Last Post: lockhead
[Shell] by Emre_Aug 31, 2019 05:10 AM 0 98 Aug 31, 2019 05:10 AM
Last Post: Emre_