How do I instantiate a BO in C#
The first thing to be clear on is what you want to work with when using or creating an object. I.e. if I have a BO that inherits from the _BaseBO in the project, which in turn inherits from the SF BusinessLayer (etc.), I can work with it as the actual typed BO from my project (like CustomersBO) or as a _BaseBO or as a BusinessLayer. E.g. all of the following are valid ways to create a CustomersBO using Activator.CreateInstance:
Type boType = Common.GetTypeFromReferencedAssemblies(boTypeName);
CustomersBO custBO = (CustomersBO)Activator.CreateInstance(boType);
_BaseBO custAsBaseBO = (_BaseBO)Activator.CreateInstance(boType);
BusinessLayer custAsBusinessLayerBO = (BusinessLayer)Activator.CreateInstance(boType);
Note that Activator.CreateInstance takes a Type and then creates an object of that type, but it returns an Object. The actual type is of the specific type you create, CustomersBO in this example. I.e. in all instances in this example, the actual object returned by CreateInstance is a CustomersBO. We are just
casting to a type we want to work with. As long as the object supports the cast, we're good.
In the case of this sample app, I left it as _BaseBO because in Ivan's original sample he was using the GetFieldValueByPrimaryKey method (I ended up not using this, so the _LookupBusinessObject could be changed to by of type BusinessLayer rather than _BaseBO).
Note that this sort of thing isn't of much use:
BusinessLayer bo = new BusinessLayer();
bo=CType(Activator.CreateInstance............);
The BusinessLayer object created on the first line is just created, then gets dereferenced on the next line, and thus setup for garbage collection. Either use something like I was using above or something like this:
BusinessLayer bo = null;
bo=CType(Activator.CreateInstance............);
Setting to null is not strictly necessary, but the VS compiler barks if you don't do it.
Is this starting to make more sense?