Will calling a new method/property work if called on a "base" abstract object
-
I am working with SharpCompress and creating a parser that creates objects mimicking real folder structures to use in various browsable controls. Since the IEntry and IArchiveEntry interfaces in SharpCompress have all the makings of an "info" class, I have created two custom Info classes derived from System.IO.FileSystemInfo. Because most of the properties are NOT virtual, I have to use the new keyword to override them so that I can grab the info from the Entry object instead of the file system. I want to use the base class FileSystemInfo to supply much of the data for my virtual file system object class. However, I am not sure if using the FileSystemInfo object will work with the derived classes that hide the original properties. Check the relevant part of my code below:
public class IOObjectNode : TreeNode;
{
FileSystemInfo info;
FileInfo file;
DirectoryInfo folder;
ArchiveDirectoryInfo virtualFolder;
ArchiveFileInfo virtualFile;IEntry entry; public string FileName { get { return info.Name; } } public string Path { get { return System.IO.Path.GetDirectoryName(info.FullName); } } public string FullName { get { return info.FullName; } } public DateTime LastWriteTime { get { return info.LastWriteTime; } } public DateTime CreationTime { get { return info.CreationTime; } } public long Size { get { if (file != null) return file.Length; else return 0; } } public long Length { get { return Size; } } public IOObjectNode() { } public void Populate(IEntry entry) { SharpEntry = entry; } public void Populate(FileSystemInfo info) { ShellInfo = info; } public FileSystemInfo ShellInfo { get { return info; } protected set { info = value; file = value as FileInfo; folder = value as DirectoryInfo; virtualFolder = value as ArchiveDirectoryInfo; virtualFile = value as ArchiveFileInfo; UpdateResources(); } } public IEntry SharpEntry { get { return entry; } protected set { entry = value; UpdateResources(); } }
}
Notice my use of "info" in some of the properties above. Will this work with my custom class instances when info is the base class?
-
I am working with SharpCompress and creating a parser that creates objects mimicking real folder structures to use in various browsable controls. Since the IEntry and IArchiveEntry interfaces in SharpCompress have all the makings of an "info" class, I have created two custom Info classes derived from System.IO.FileSystemInfo. Because most of the properties are NOT virtual, I have to use the new keyword to override them so that I can grab the info from the Entry object instead of the file system. I want to use the base class FileSystemInfo to supply much of the data for my virtual file system object class. However, I am not sure if using the FileSystemInfo object will work with the derived classes that hide the original properties. Check the relevant part of my code below:
public class IOObjectNode : TreeNode;
{
FileSystemInfo info;
FileInfo file;
DirectoryInfo folder;
ArchiveDirectoryInfo virtualFolder;
ArchiveFileInfo virtualFile;IEntry entry; public string FileName { get { return info.Name; } } public string Path { get { return System.IO.Path.GetDirectoryName(info.FullName); } } public string FullName { get { return info.FullName; } } public DateTime LastWriteTime { get { return info.LastWriteTime; } } public DateTime CreationTime { get { return info.CreationTime; } } public long Size { get { if (file != null) return file.Length; else return 0; } } public long Length { get { return Size; } } public IOObjectNode() { } public void Populate(IEntry entry) { SharpEntry = entry; } public void Populate(FileSystemInfo info) { ShellInfo = info; } public FileSystemInfo ShellInfo { get { return info; } protected set { info = value; file = value as FileInfo; folder = value as DirectoryInfo; virtualFolder = value as ArchiveDirectoryInfo; virtualFile = value as ArchiveFileInfo; UpdateResources(); } } public IEntry SharpEntry { get { return entry; } protected set { entry = value; UpdateResources(); } }
}
Notice my use of "info" in some of the properties above. Will this work with my custom class instances when info is the base class?
Nevermind. Found the answer myself with a few test classes:
public class CoreClass
{
public int Value { get { return 1; } }
}public class ClassA : CoreClass
{
public new int Value { get { return 2; } }
}public class ClassB : CoreClass
{
public new int Value { get { return 3; } }
}public class ClassC : CoreClass
{
public new int Value { get { return 4; } }
}public class ClassD : CoreClass
{
public new int Value { get { return 5; } }
}public class ContainerClass
{
public CoreClass CoreObject { get; set; }
public int CoreValue { get { return CoreObject.Value; } }public ContainerClass(CoreClass instance) { CoreObject = instance; }
}
static void Main(string[] args)
{
ContainerClass a = new ContainerClass(new ClassA());
ContainerClass b = new ContainerClass(new ClassB());
ContainerClass c = new ContainerClass(new ClassC());
ContainerClass d = new ContainerClass(new ClassD());int av = a.CoreValue; int bv = b.CoreValue; int cv = c.CoreValue; int dv = d.CoreValue;
}
Bummer... all the int values in Main were 1. So looks like it calls the base class method and not the derived one. Looks like I will need to try a different approach.
-
Nevermind. Found the answer myself with a few test classes:
public class CoreClass
{
public int Value { get { return 1; } }
}public class ClassA : CoreClass
{
public new int Value { get { return 2; } }
}public class ClassB : CoreClass
{
public new int Value { get { return 3; } }
}public class ClassC : CoreClass
{
public new int Value { get { return 4; } }
}public class ClassD : CoreClass
{
public new int Value { get { return 5; } }
}public class ContainerClass
{
public CoreClass CoreObject { get; set; }
public int CoreValue { get { return CoreObject.Value; } }public ContainerClass(CoreClass instance) { CoreObject = instance; }
}
static void Main(string[] args)
{
ContainerClass a = new ContainerClass(new ClassA());
ContainerClass b = new ContainerClass(new ClassB());
ContainerClass c = new ContainerClass(new ClassC());
ContainerClass d = new ContainerClass(new ClassD());int av = a.CoreValue; int bv = b.CoreValue; int cv = c.CoreValue; int dv = d.CoreValue;
}
Bummer... all the int values in Main were 1. So looks like it calls the base class method and not the derived one. Looks like I will need to try a different approach.
Well yes - that is what the
new
keyword is there to do: Knowing When to Use Override and New Keywords - C# Programming Guide | Microsoft Docs[^] If you want to extend behaviour, you override properties / methods."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
Well yes - that is what the
new
keyword is there to do: Knowing When to Use Override and New Keywords - C# Programming Guide | Microsoft Docs[^] If you want to extend behaviour, you override properties / methods."I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
Well you can't override non-virtual/abstract methods or properties and I can't change Microsoft's base classes. Pretty annoying because it would be a simple solution to a now a more complex problem. I am guessing I will have to make a wrapper.