Created
June 19, 2013 16:02
-
-
Save Zolomon/5815516 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| namespace DerivedPropertyDemo | |
| { | |
| class Program | |
| { | |
| internal class Service | |
| { | |
| public int ID { get; set; } | |
| } | |
| internal class RHService : Service | |
| { | |
| public string SomeOtherPropery { get; set; } | |
| } | |
| internal class VAService : Service | |
| { | |
| public string SomeOtherPropery { get; set; } | |
| } | |
| internal class Base | |
| { | |
| public List<Service> Services { get; set; } | |
| } | |
| internal class DerivedVA : Base | |
| { | |
| public List<VAService> Services { get; set; } | |
| } | |
| internal class DerivedRH : Base | |
| { | |
| public List<RHService> Services { get; set; } | |
| } | |
| public static void PrintWithBase(Base obj) | |
| { | |
| try | |
| { | |
| obj.Services.ForEach(x => Console.WriteLine("BaseObj: " + x.ID.ToString())); | |
| } | |
| catch (Exception e) | |
| { | |
| Console.WriteLine(e); | |
| } | |
| } | |
| static void Main(string[] args) | |
| { | |
| Base baseObj = new Base() {Services = new List<Service>() {new Service() {ID = 0}}}; | |
| DerivedRH rhObj = new DerivedRH() { Services = new List<RHService>() { new RHService() { ID = 1 } } }; | |
| DerivedVA vaObj = new DerivedVA() { Services = new List<VAService>() { new VAService() { ID = 2 } } }; | |
| baseObj.Services.ForEach(x => Console.WriteLine("BaseObj: " + x.ID.ToString())); // 0 | |
| rhObj.Services.ForEach(x => Console.WriteLine("RHService: " + x.ID.ToString())); // 1 | |
| vaObj.Services.ForEach(x => Console.WriteLine("VAService: " + x.ID.ToString())); // 2 | |
| PrintWithBase(baseObj); | |
| PrintWithBase(rhObj); // null, eftersom base.Services blir 'shadowed' | |
| PrintWithBase(vaObj); // null, eftersom base.Services blir 'shadowed' | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment