Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Test.Program;
- class BaseClass {
- public virtual string TypeName => typeof(BaseClass).Name;
- public int BaseOnly => 1;
- }
- class DerivedClass1 : BaseClass {
- public override string TypeName => this.GetType().Name;
- public virtual string DerivedFrom => this.GetType()!.BaseType!.Name;
- public int Derived1Only => 2;
- }
- class DerivedClass2 : DerivedClass1 {
- public int Derived2Only => 3;
- }
- class Program
- {
- static void Main() {
- List<BaseClass> l = new();
- l.Add(new BaseClass());
- l.Add(new DerivedClass1());
- l.Add(new DerivedClass2());
- l.ForEach(c => Console.WriteLine(c switch {
- DerivedClass2 d2 => $"{d2.TypeName}:\tDerivedFrom = {d2.DerivedFrom}, Derived2Only = {d2.Derived2Only}",
- DerivedClass1 d1 => $"{d1.TypeName}:\tDerivedFrom = {d1.DerivedFrom}, Derived1Only = {d1.Derived1Only}",
- BaseClass b => $"{b.TypeName}:\t\tBaseOnly = {b.BaseOnly}"
- }));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement