Hack Forums
[.NET] Add properties/variable to a class at runtime? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Add properties/variable to a class at runtime? (/showthread.php?tid=5956300)



Add properties/variable to a class at runtime? - Ajumma - Apr 2, 2019

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)


RE: Add properties/variable to a class at runtime? - gigajew - Apr 2, 2019

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.




RE: Add properties/variable to a class at runtime? - Ajumma - Apr 2, 2019

(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


RE: Add properties/variable to a class at runtime? - itcorp - Apr 3, 2019

(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/api/system.dynamic.dynamicobject?redirectedfrom=MSDN&view=netframework-4.7.2
https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject.trygetmember?redirectedfrom=MSDN&view=netframework-4.7.2#System_Dynamic_DynamicObject_TryGetMember_System_Dynamic_GetMemberBinder_System_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();

        }
    }



RE: Add properties/variable to a class at runtime? - Ajumma - Apr 3, 2019

(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/api/system.dynamic.dynamicobject?redirectedfrom=MSDN&view=netframework-4.7.2
https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicobject.trygetmember?redirectedfrom=MSDN&view=netframework-4.7.2#System_Dynamic_DynamicObject_TryGetMember_System_Dynamic_GetMemberBinder_System_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




RE: Add properties/variable to a class at runtime? - itcorp - Apr 3, 2019

(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