When your BP class in one module needs to talk to the BP for another module, you might write code like this:
AB_FrameworkBusinessObjectProcessBase<TermsCodeEntity> _termsBP; AB_FrameworkBusinessObjectProcessBase<TermsCodeEntity> _TermsBP { get { if (_termsBP == null) { Type bpType = Type.GetType("BOS.TermsCodeBusinessProcess.TermsCodeBP, BOS.TermsCodeBusinessProcess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); _termsBP = Activator.CreateInstance(bpType) as AB_FrameworkBusinessObjectProcessBase<TermsCodeEntity>; } return _termsBP; } }
One reason to use code like this is when your BP modules need to talk to each other through methods inherited from AB_FrameworkBusinessObjectProcessBase. If the BPs referenced each other directly you'd have a cirular reference problem, and neither BP could be compiled cleanly. But if one BP uses reflection it can instantiate the other BP at runtime, and since the methods it is calling are in the base class there's no compile-time dependency on the referenced module's non-base methods.
For this code to work, the BP project needs to include a reference to BOS.TermsCodeBusinessProcess. Otherwise bpType will be null, and CreateInstance will throw an Argument Null Exception.
Since reflection is being used and the type is specified with a string, this is not a compile-time error. It won't become a problem until run-time. Even then, it won't always be a problem; if this module is referenced in a program that also references BOS.TermsCodeBusinessProcess, GetType will be able to find the dll and won't return null. The exception will only occur when this module is used separately from BOS.TermsCodeBusinessProcess.
In this particular case, there's no need for reflection. If you already know the Type of object you need to create, add the reference and instantiate the object normally:
AB_FrameworkBusinessObjectProcessBase<TermsCodeEntity> _termsBP; AB_FrameworkBusinessObjectProcessBase<TermsCodeEntity> _TermsBP { get { if (_termsBP == null) { _termsBP = new TermsCodeBusinessProcess.TermsCodeBP(); } return _termsBP; } }
Reflection should only be used when you don't know the Type until run-time, and all you have is a string containing the type's full name. And in that case, you need to make sure that you've got references to all of the possible types you might have to instantiate in every application where your code is used.