最好的办法布尔方法添加到现有的类C# [英] Best way to add boolean method to existing class C#

查看:113
本文介绍了最好的办法布尔方法添加到现有的类C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定,这是一个非常简单的任务,但我不完全确定如何词它变成一个搜索查询。我得到的最好的是扩展方法,我无法理解他们或得到语法正确。

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.

我目前使用的简化了GTA V.制作MODS的组装这些组件包括带一堆附加到其方法PED的类型。

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.

我要找的是要添加自己的方法,可以的能力存储布尔值的扩展PED类。

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.

推荐答案

您可以使用的 ConditionalWeakTable 此任务:

You can use a ConditionalWeakTable for this task:

A ConditionalWeakTable< TKEY的,TValue> 对象是绑定一个管理对象字典,这是由一个键所表示,其附加属性,这是由一个值表示。对象的键是向其中属性附加到的TKEY的类的各个实例,并且其值是分配到相应的对象的属性值。

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.

例如:

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; }
    }
}



(你当然可以让 PedProperties 公共顶级类和揭露直接,如果你有很多属性来存储)。

(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:

ConditionalWeakTable< TKEY的,TValue> 类不同于其存储在集合中键的对象的生命周期管理的其他集合对象。通常,当对象存储在集合中,其寿命持续,直到它被删除(也有到对象没有额外的参考文献)或直到本身被破坏的集合对象。然而,在 ConditionalWeakTable< TKEY的,TValue> 类的添加一个键/值对表中并不能保证关键将持续即使它可以直接从存储在表(例如,如果该表包含一个键, A 的值,用一个值达到 V1 ,和第二个关键, B ,用一个值 P2 包含一个参考 A )。相反, ConditionalWeakTable< TKEY的,TValue方式> 自动只要一键没有其他引用的表外存在删除键/值条目

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.

这篇关于最好的办法布尔方法添加到现有的类C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆