// 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 { object GetObject(); } public interface IBaseBusiness : IBaseBusiness { new 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 abstract class BaseBusiness : IBaseBusiness { public abstract T GetObject(); object IBaseBusiness.GetObject() { return GetObject(); } } public class FooBusiness : BaseBusiness { public override Foo GetObject() { return new Foo(); } } public class BarBusiness : BaseBusiness { public override Bar GetObject() { return new Bar(); } } public abstract class LazyLoadTest { protected abstract IBaseBusiness GetBusinessObject(Type theType); public void TestLazyLoad(string typnam) { IBaseBusiness business = GetBusinessObject(Type.GetType(typnam)); Console.WriteLine(business.GetObject()); } } public class CompanyEntityTest : LazyLoadTest { protected override IBaseBusiness GetBusinessObject(Type theType) { if(theType == typeof(Foo)) { return new FooBusiness(); } else if(theType == typeof(Bar)) { return 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("E.Foo"); cet.TestLazyLoad("E.Bar"); } } }