// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public interface IBaseBusiness { T GetObject(); } public class Foo { public override string ToString() { return "I am a Foo"; } } public class Bar { public override string ToString() { return "I am a Bar"; } } public class FooBusiness : IBaseBusiness { public Foo GetObject() { return new Foo(); } } public class BarBusiness : IBaseBusiness { public Bar GetObject() { return new Bar(); } } public abstract class LazyLoadTest { protected abstract IBaseBusiness GetBusinessObject(); public void TestLazyLoad() { IBaseBusiness business = GetBusinessObject(); Console.WriteLine(business.GetObject()); } } public class CompanyEntityTest : LazyLoadTest { protected override IBaseBusiness GetBusinessObject() { if(typeof(T) == typeof(Foo)) { return (IBaseBusiness)new FooBusiness(); } else if(typeof(T) == typeof(Bar)) { return (IBaseBusiness)new BarBusiness(); } else { throw new Exception("No Foo or Bar"); } } } public class Program { public static void Main(string[] args) { CompanyEntityTest cet = new CompanyEntityTest(); cet.TestLazyLoad(); cet.TestLazyLoad(); } } }