"Looser" prints in C #, dropping the inheritance tree

The question I want to ask is this:

Throws an inheritance tree (i.e. towards a more specialized class) from within an abstract class, is forgiven or even good, or is it always a bad choice with better options?

Now, an example of why I think it can be used forever.

I recently implemented Bencoding from the BitTorrent protocol in C #. A simple problem is how to present the data. I decided to do it this way

We have a class abstract BItemthat provides some basic functions, including static BItem Decode(string)one that is used to decode a Bencoded string in the required structure.

There are four classes of derivatives BString, BInteger, BListand BDictionaryrepresenting the four different data type to be coded. Now here is the hard part. BListand BDictionaryhave attributes this[int], this[string]respectively, to allow access to the properties of these data types, like an array.

Now the potentially terrible part:

BDictionary torrent = (BDictionary) BItem.DecodeFile("my.torrent");
int filelength = (BInteger)((BDictionary)((BList)((BDictionary)
             torrent["info"])["files"])[0])["length"];

Well, you get the picture ... Oh, it's hard on the eyes, not to mention the brain. So, I introduced something superfluous into the abstract class:

public BItem this[int index]
{
    get { return ((BList)this)[index]; }
}
public BItem this[string index]
{
    get { return ((BDictionary)this)[index]; }
}

Now we can rewrite this old code as:

BDictionary torrent = (BDictionary)BItem.DecodeFile("my.torrent");
int filelength = (BInteger)torrent["info"]["files"][0]["length"];

Wow, hey, preto, MUCH more readable code. But did I just sell part of my soul for implying knowledge of subclasses in an abstract class?

EDIT: , , , torrent["info"]["files"][0]["length"] , torrent["announce-list"][0][0] 90% . - , :(. , , 4 .

+5
10

, [int] [string] accessors BList/BDictionary. , , NotSupportedException() (, BItem).

,

 (BInteger)torrent["info"][0]["files"]["length"];

.

+5

, . , , , . , - .

+3

- , , BDictionary (?)... :

BDictionary torrent = BItem.DecodeFile("my.torrent");
int filelength = torrent.FileLength;

, .

+1

, , BItems , BItems , BItem. BItem, BCollection , - :

abstract class BCollection : BItem {

      public BItem this[int index] {get;}
      public BItem this[string index] {get;}
}

BList BDictionary BCollection. BCollection .

+1

, . , BItem DecodeFile(), BDictionary. torrent, .

, api :

BFile torrent = BFile.DecodeFile("my.torrent");
int filelength = torrent.Length;
+1

"", :

BDictionary torrent = BItem.DecodeFile("my.torrent");
int filelength = (int)torrent.Fetch("info.files.0.length");

, , ()

0
  • , .
  • , , - BItem, , BList BDictionary.

, ( ) , .

BString GetString(BInteger);
SetString(BInteger, BString);

BStrings, BList BItems. ( , 2 B 2 B)

0

. , , - , , , apparant, BList BDictionary. , , .

0

, .

class DecodedTorrent : BDictionary<BDictionary<BList<BDictionary<BInteger>>>>
{
}

DecodedTorrent torrent = BItem.DecodeFile("mytorrent");
int x = torrent["info"]["files"][0]["length"];

, , , , , .

0

BDictionary torrent = BItem.DecodeFile("my.torrent");int filelength = (BInteger)((BDictionary)((BList)((BDictionary)             torrent["info"])["files"])[0])["length"];

, BDictionary cast 'torrent' BDictionary

public BItem this[int index]{&nbsp; &nbsp; get { return ((BList)this)[index]; }}public BItem this[string index]{&nbsp; &nbsp; get { return ((BDictionary)this)[index]; }}

This will not produce the desired result, since the return type is still an abstract version, so you still have to drop it.

The rewritten code should be

BDictionary torrent = BItem.DecodeFile("my.torrent");int filelength = (BInteger)((BList)((BDictionary)torrent["info"]["files"])[0])["length"];

This is as bad as the first lot

0
source

All Articles