BencodeObject Class
Definition
Represents a mutable Bencode (BEP 3) dictionary as a string-keyed collection of child nodes.
public sealed class BencodeObject : BencodeNode, IDictionary<string, BencodeNode?>, ICollection<KeyValuePair<string, BencodeNode?>>, IEnumerable<KeyValuePair<string, BencodeNode?>>, IEnumerable
- Inheritance
-
BencodeObject
- Implements
- Inherited Members
- Extension Methods
Remarks
Keys are CLR strings encoded as UTF-8 byte strings on the wire. In-memory ordering is not contractual: the writer emits entries in canonical ascending bytewise key order regardless of insertion order. A value may be null in memory, but an object containing a null value cannot be written because Bencode has no null token. Adding a node that already belongs to another container throws an InvalidOperationException; removing or replacing a value detaches it, clearing its Parent so it can be added to another container.
var dictionary = new BencodeObject
{
["announce"] = BencodeValue.Create("http://tracker.example/announce"),
["info"] = new BencodeObject { ["name"] = BencodeValue.Create("data.bin") },
};
// Keys are emitted in canonical (ascending bytewise) order regardless of add order.
byte[] encoded = dictionary.ToUtf8Bytes();
Constructors
BencodeObject()
Initializes a new instance of the BencodeObject class that is empty.
public BencodeObject()
BencodeObject(BencodeNodeOptions)
Initializes a new instance of the BencodeObject class that is empty, using the supplied options to select the property-name comparison.
public BencodeObject(BencodeNodeOptions options)
Parameters
optionsBencodeNodeOptionsThe node options controlling property-name case sensitivity.
Remarks
When PropertyNameCaseInsensitive is true, in-memory property-name lookups use OrdinalIgnoreCase; otherwise they use Ordinal. The option does not affect serialization.
BencodeObject(IEnumerable<KeyValuePair<string, BencodeNode?>>)
Initializes a new instance of the BencodeObject class containing the supplied entries.
public BencodeObject(IEnumerable<KeyValuePair<string, BencodeNode?>> items)
Parameters
itemsIEnumerable<KeyValuePair<string, BencodeNode>>The initial entries.
Exceptions
- ArgumentNullException
Thrown when
itemsis null.- InvalidOperationException
Thrown when a value already belongs to another container.
Properties
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[string]
Gets or sets the value associated with the specified property name.
public BencodeNode? this[string key] { get; set; }
Parameters
keystringThe property name.
Property Value
- BencodeNode
The value associated with
key.
Remarks
Assigning over an existing entry detaches the replaced node, clearing its Parent.
Exceptions
- ArgumentNullException
Thrown when
keyis null.- KeyNotFoundException
Thrown by the getter when
keyis not present.- InvalidOperationException
Thrown when the assigned node already belongs to another container.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
public ICollection<string> Keys { get; }
Property Value
- ICollection<string>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
Values
Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.
public ICollection<BencodeNode?> Values { get; }
Property Value
- ICollection<BencodeNode>
An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.
Methods
Add(KeyValuePair<string, BencodeNode?>)
Adds an item to the ICollection<T>.
public void Add(KeyValuePair<string, BencodeNode?> item)
Parameters
itemKeyValuePair<string, BencodeNode>The object to add to the ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Add(string, BencodeNode?)
Adds an element with the provided key and value to the IDictionary<TKey, TValue>.
public void Add(string key, BencodeNode? value)
Parameters
keystringThe object to use as the key of the element to add.
valueBencodeNodeThe object to use as the value of the element to add.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the IDictionary<TKey, TValue>.
- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Contains(KeyValuePair<string, BencodeNode?>)
Determines whether the ICollection<T> contains a specific value.
public bool Contains(KeyValuePair<string, BencodeNode?> item)
Parameters
itemKeyValuePair<string, BencodeNode>The object to locate in the ICollection<T>.
Returns
- bool
true if
itemis found in the ICollection<T>; otherwise, false.
ContainsKey(string)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(string key)
Parameters
keystringThe key to locate in the IDictionary<TKey, TValue>.
Returns
- bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
CopyTo(KeyValuePair<string, BencodeNode?>[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public void CopyTo(KeyValuePair<string, BencodeNode?>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<string, BencodeNode>[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
DeepClone()
Creates a deep copy of this node and its entire subtree.
public override BencodeNode DeepClone()
Returns
- BencodeNode
An independent clone with no parent.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<string, BencodeNode?>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<string, BencodeNode>>
An enumerator that can be used to iterate through the collection.
GetValueKind()
Gets the kind of value this node represents.
public override BencodeValueKind GetValueKind()
Returns
- BencodeValueKind
The BencodeValueKind of this node.
Remove(KeyValuePair<string, BencodeNode?>)
Removes the first occurrence of a specific object from the ICollection<T>.
public bool Remove(KeyValuePair<string, BencodeNode?> item)
Parameters
itemKeyValuePair<string, BencodeNode>The object to remove from the ICollection<T>.
Returns
- bool
true if
itemwas successfully removed from the ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Remove(string)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public bool Remove(string key)
Parameters
keystringThe key of the element to remove.
Returns
- bool
true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original IDictionary<TKey, TValue>.
Exceptions
- ArgumentNullException
keyis null.- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
ToString()
Returns a string representation of this node.
public override string ToString()
Returns
- string
A textual rendering of this node.
TryGetPropertyValue(string, out BencodeNode?)
Attempts to get the value associated with the specified property name.
public bool TryGetPropertyValue(string propertyName, out BencodeNode? value)
Parameters
propertyNamestringThe property name to look up.
valueBencodeNodeWhen this method returns true, the value associated with
propertyName; otherwise null.
Returns
TryGetValue(string, out BencodeNode?)
Gets the value associated with the specified key.
public bool TryGetValue(string key, out BencodeNode? value)
Parameters
keystringThe key whose value to get.
valueBencodeNodeWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
WriteTo(Utf8BencodeWriter)
Writes the canonical Bencode encoding of this node to the supplied writer.
public override void WriteTo(Utf8BencodeWriter writer)
Parameters
writerUtf8BencodeWriterThe destination writer.
Exceptions
- BencodeSerializationException
Thrown when the subtree rooted at this node contains a null entry, which has no Bencode representation.
Explicit Interface Implementations
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |