English 中文(简体)
Beginner: Extending a class in C#, am I doing it wrong?
原标题:

Again disclaimer disclaimer still learning C# and OOP generally so I hope you ll be patient with me :)

I am currently working with a CMS that has a class called FileVersion which basically contains a list of properties pertaining to a file such as filename, filetype, size in bytes, id, date uploaded, is-latest-version, etc.

A list of FileVersions is contained within a File which has it s own unique ID. When you want to download a particular file from the CMS, the URL is built using something like the following:

string URL = "/files/"+file.id.toString()+"/"+file.fileVersion.Last().filename;

Now for a particular control I m working on (dealing specifically with files that are documents), it makes sense to be able to store the URL along with all the other FileVersion information for later use. So what I ve decided to do is create my own class called DocumentVersion which extends FileVersion. This is what it looks like:

public partial class DocumentVersion : FileVersion
{
    public DocumentVersion() : base() { }
    public string link;
}

Now I should note, that it appears that I don t need to implement an interface here - but don t take that as gospel, that s why I m here.

However when I try to cast my FileVersion as a DocumentVersion like so:

DocumentVersion dv = ((DocumentVersion)fileversion);

I get the following exception:

Unable to cast object of type Foo.CMS.FileVersion to type CoA.DocumentVersion .

My inkling is that it s because I m trying to extend a class in a different namespace from where the original resides, but like I said, OOP is relatively new to me so I could be wrong.

Thanks in advance for your help. This community has been so valuable! I just hope once I m more skilled up I can give some back :).

最佳回答

Are you trying to downcast ? i.e. take an existing FileVersion and declare it to be a DocumentVersion ?

If so, you can t do that, since what you have is nothing more than a FileVersion.

Instead you need to create a new DocumentVersion object. Your DocumentVersion constructor will take the arguments required for a FileVersion and then call the FileVersion constructor via base().

Note that if you have an existing FileVersion object, you may want an object to wrap this, rather than have an object deriving from it. e.g. your DocumentVersion wouldn t derive from FileVersion, but contain a private reference to a FileVersion, plus additional data as required. That may be more appropriate in this scenario.

e.g. (in OO terms this is composition)

public class DocumentVersion {
   private FileVersion fv;
   private String url;
   public DocumentVersion(FileVersion fv, String url) {
      this.fv = fv;
      this.url = url;
   }
}

See here for more info on composition.

问题回答

You cannot cast your FileVersion to DocumentVersion because your fileversion variable contains a FileVersion, not a DocumentVersion. Inheritance means the following:

  • Every DocumentVersion is a FileVersion, but
  • Not every FileVersion is a DocumentVersion!

If the object was created as a FileVersion, it s just a FileVersion. Period. If it was created as a DocumentVersion, you can use it everywhere where you would use a FileVersion (see rule #1 above), and you can use the DocumentVersion features of it.

So, when creating the FileVersion object, you need to create a DocumentVersion instead (if you have control over that part of the code), then the cast will work. Those DocumentVersions could be stored in the same list as the FileVersions, since every DocumentVersion is also a FileVersion.


EDIT: Since the above two rules are so vital to understanding the OO principle, let me illustrate them with an example: DocumentVersion = dog and FileVersion = animal. Then the above rules would be: (1) Every dog is an animal, but (2) not every animal is a dog. So, you can create a list of animals, store all kinds of animals in there (dogs, cats, things that are just "animal", ...), but you cannot cast a variable of type animal to a dog, unless what is in there has been created as a dog (or as a poodle (class poodle : dog), which is a dog by rule (1)).

In OO terms: If you store a dog in a variable of type animal, then the static type of the object is "animal", and the dynamic type is "dog". You can only cast an element to some type T if its dynamic type is T or a subtype thereof.

You can t make father (FileVersion) to act like son (DocumentVersion).
Only vica versa => you can make son as father.

(FileVersion)documentVersion //valid
(DocumentVersion)fileVersion //invalid (because fileVersion can t know about stuff that derived type has)

That s OOP basics.

DocumentVersion dv = null;

if (fileversion is DocumentVersion)
{
    dv = fileversion as DocumentVersion;
}

(As most answers have said, you can t cast back up - FileVersion has no idea what a DocumentVersion is).

One way to get around it is to copy the fields in the constructor of DocumentVersion

public partial class DocumentVersion : FileVersion
{
    public DocumentVersion() : base() { }

    public DocumentVersion(FileVersion version) : this()
    {
        this.id = version.id;
        // etc.
    }

    public string Link { get;set; }
}

Or create a static method on DocumentVersion that returns a DocumentVersion from the FileVersion you provide it, e.g:

public static void New(FileVersion version)
{
    this.id = version.id;
    // etc.
}

Another technique mentioned, is composition. You store the FileVersion as a property in DocumentVersion

public class DocumentVersion
{
    public FileVersion FileVersion { get;set; }
    public string Link { get;set; }
}

Since you cannot convert a FileVersion to a DocumentVersion and all you need to do is add a method to get the URL, you could create an extension method that takes a FileVersion:

public static string GetLink(this FileVersion fileVersion) {
  return "/files/"+fileVersion.id.ToString()+"/"+fileVersion.FileVersion.Last().Filename
}

Note : this only works if you are using C# 3.0.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签