Best way to add boolean method to existing class C# -


this question has answer here:

i'm sure pretty simple task i'm not entirely sure how word search query. best got extension methods , couldn't understand them or syntax right.

i'm using assembly simplifies making mods gta v. these assemblies include ped "type" bunch of methods attached it.

what i'm looking ability add own method can store bool value extension ped class.

example

any appreciated, thank you.

you can use conditionalweaktable task:

a conditionalweaktable<tkey, tvalue> object dictionary binds managed object, represented key, attached property, represented value. object's keys individual instances of tkey class property attached, , values property values assigned corresponding objects.

example:

public static class pedextensions {     private static readonly conditionalweaktable<ped, pedproperties> _props = new conditionalweaktable<ped, pedproperties>();      public static bool getmybool(this ped ped)     {         return _props.getorcreatevalue(ped).mybool;     }      public static void setmybool(this ped ped, bool value)     {         _props.getorcreatevalue(ped).mybool = value;     }      private class pedproperties     {         public bool mybool { get; set; }     } } 

(you of course make pedproperties public top-level class , expose directly, if have many properties store).

as table uses weak references, don't have worry memory leaks:

the conditionalweaktable<tkey, tvalue> class differs other collection objects in management of object lifetime of keys stored in collection. ordinarily, when object stored in collection, lifetime lasts until removed (and there no additional references object) or until collection object destroyed. however, in conditionalweaktable<tkey, tvalue> class, adding key/value pair table not ensure key persist, if can reached directly value stored in table (for example, if table contains 1 key, a, value v1, , second key, b, value p2 contains reference a). instead, conditionalweaktable<tkey, tvalue> automatically removes key/value entry no other references key exist outside table.


Comments