Best way to add boolean method to existing class C# [duplicate]

青春壹個敷衍的年華 提交于 2019-12-22 09:59:17

问题


I'm sure this is a pretty simple task but I'm not entirely sure how to word it into a search query. The best I got was Extension methods and I couldn't understand them or get the syntax right.

I'm currently using an assembly that simplifies making mods for GTA V. These assemblies include a Ped "type" with a bunch of methods that are attached to it.

What I'm looking for is the ability to add my own method that can store a bool value as an extension to the Ped class.

Any help would be greatly appreciated, thank you.


回答1:


You can use a ConditionalWeakTable for this task:

A ConditionalWeakTable<TKey, TValue> object is a dictionary that binds a managed object, which is represented by a key, to its attached property, which is represented by a value. The object's keys are the individual instances of the TKey class to which the property is attached, and its values are the property values that are assigned to the 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 could of course make PedProperties a public top-level class and expose that directly, if you have many properties to store).

As the table uses weak references, you don't have to worry about memory leaks:

The ConditionalWeakTable<TKey, TValue> class differs from other collection objects in its management of the object lifetime of keys stored in the collection. Ordinarily, when an object is stored in a collection, its lifetime lasts until it is removed (and there are no additional references to the object) or until the collection object itself is destroyed. However, in the ConditionalWeakTable<TKey, TValue> class, adding a key/value pair to the table does not ensure that the key will persist, even if it can be reached directly from a value stored in the table (for example, if the table contains one key, A, with a value V1, and a second key, B, with a value P2 that contains a reference to A). Instead, ConditionalWeakTable<TKey, TValue> automatically removes the key/value entry as soon as no other references to a key exist outside the table.




回答2:


The right answer is the one you were already given, extension methods.

public static class PedExtensions
{
    public static bool CheckIfTrue(this Ped ped)
    {
         if(ped.something != "this value")
         {
              return true;
         }

         return false;
    }
}

The "this" in the method signature refers to the thing the code is appended to. so var myPed = new Ped(); myPed.CheckIfTrue(); will run the code in the extension.



来源:https://stackoverflow.com/questions/31480798/best-way-to-add-boolean-method-to-existing-class-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!