![]() |
|
[.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 HouseWhat 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 youYou 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.ExpandoCode class ProgramRE: 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 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 |