Last active
August 29, 2015 14:15
-
-
Save occar421/85bf53c863d71107878d to your computer and use it in GitHub Desktop.
Expression for static inheritance of C#
This file contains 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; | |
namespace SingletonStaticInheritance.Abstract | |
{ | |
public interface IStaticInheritable<T> where T : IPropertyContainer | |
{ | |
T ClassProperty { get; } | |
} | |
public interface IPropertyContainer { } | |
public abstract class VehiclePropertyContainerBase : IPropertyContainer | |
{ | |
public abstract int Cost { get; } | |
public abstract float Weight { get; } | |
public virtual void Write() | |
{ | |
Console.WriteLine($"Cost:{Cost}, Weight:{Weight}"); | |
} | |
} | |
public class Car : IStaticInheritable<VehiclePropertyContainerBase> | |
{ | |
public static VehiclePropertyContainerBase Property { get; } = CarContainer.Singleton; | |
public VehiclePropertyContainerBase ClassProperty { get; } = Property; | |
private class CarContainer : VehiclePropertyContainerBase | |
{ | |
static public VehiclePropertyContainerBase Singleton { get; } = new CarContainer(); | |
public override int Cost { get; } = 250; | |
public override float Weight { get; } = 1000; | |
} | |
} | |
public class Train : IStaticInheritable<VehiclePropertyContainerBase> | |
{ | |
public static VehiclePropertyContainerBase Property { get; } = TrainContainer.Singleton; | |
public VehiclePropertyContainerBase ClassProperty { get; } = Property; | |
private class TrainContainer : VehiclePropertyContainerBase | |
{ | |
static public VehiclePropertyContainerBase Singleton { get; } = new TrainContainer(); | |
public override int Cost { get; } = 12200; | |
public override float Weight { get; } = 31880; | |
} | |
} | |
} |
This file contains 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
namespace SingletonStaticInheritance.Interface | |
{ | |
public interface IStaticInheritable<T> where T : IPropertyContainer | |
{ | |
T ClassProperty { get; } | |
} | |
public interface IPropertyContainer { } | |
public interface IVehiclePropertyContainer : IPropertyContainer | |
{ | |
int Cost { get; } | |
float Weight { get; } | |
} | |
public class Car : IStaticInheritable<IVehiclePropertyContainer> | |
{ | |
public static IVehiclePropertyContainer Property { get; } = CarContainer.Singleton; | |
public IVehiclePropertyContainer ClassProperty { get; } = Property; | |
private class CarContainer : IVehiclePropertyContainer | |
{ | |
static public IVehiclePropertyContainer Singleton { get; } = new CarContainer(); | |
public int Cost { get; } = 250; | |
public float Weight { get; } = 1000; | |
} | |
} | |
public class Train : IStaticInheritable<IVehiclePropertyContainer> | |
{ | |
public static IVehiclePropertyContainer Property { get; } = TrainContainer.Singleton; | |
public IVehiclePropertyContainer ClassProperty { get; } = Property; | |
private class TrainContainer : IVehiclePropertyContainer | |
{ | |
static public IVehiclePropertyContainer Singleton { get; } = new TrainContainer(); | |
public int Cost { get; } = 12200; | |
public float Weight { get; } = 31880; | |
} | |
} | |
} |
This file contains 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 SingletonStaticInheritance.Abstract; | |
namespace SingletonStaticInheritance | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
foreach (var item in new IStaticInheritable<VehiclePropertyContainerBase>[] { new Car(), new Train() }) | |
{ | |
item.ClassProperty.Write(); | |
//item.ClassProperty.Cost; -> 250, 12200 | |
//Car.Property.Cost; -> 250 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment