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

查看:130
本文介绍了最好的方法添加布尔方法到现有类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.

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.

我正在寻找的是添加我自己的方法的能力,这个方法可以存储bool值作为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.

推荐答案

此任务的https://msdn.microsoft.com/en-us/library/dd287757.aspx =nofollow> ConditionalWeakTable : / p>

You can use a ConditionalWeakTable for this task:


A ConditionalWeakTable< TKey,TValue> object是一个绑定被管对象,其由键表示,附加到由其值表示的属性。对象的键是属性所附属的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; }
    }
}

c> PedProperties 一个公共顶级类,如果你有很多属性可以存储,直接暴露)。

(You could of course make PedProperties a public top-level class and expose that directly, if you have many properties to store).

您不必担心内存泄漏:


ConditionalWeakTable< TKey,TValue& code>类在其管理存储在集合中的密钥对象生存期方面与其他集合对象不同。通常,当对象存储在集合中时,其生存期持续到它被删除(并且没有对对象的附加引用),或者直到集合对象本身被销毁。但是,在 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天全站免登陆