On Activator.CreateInstance...
April 15. 2008
0 Comments
- Posted in:
- .Net Framework
- C#
When you use .NET Activator.CreateInstance to create a new object of a certain type, make sure you reference the Project / DLL that contain the type definition or load it manually somehow to an AppDomain. Otherwise your call will fail. Say you have the following solution:
Solution Foo
Project Bar1
IUser
IDataMapper
IUser Load(...);
Project Bar2 (references Bar1)
ADSpecificDataMapper : Bar1.IDataMapper
IUser Load(...) { ... }
Project Bar3
User : IUser
IDataMapper m_Mapper = Settings.Instance.UserMapper
IUser Load(...) { return m_Mapper.Load(...) }
Project Baz (references Bar1)
Settings
Settings Instance = new Settings() //singleton
IDataMapper Mapper = CreateMapper(ConfigurationManager.AppSettings[...])
IDataMapper CreateMapper(SpecificMapperClassNameFromAppConfig)
Project ConsoleTester (references Baz, Bar3)
Main
IUser user = User.Load(...);
Note that ConsoleTester is not referencing Project Bar2 (which contain the ADSpecificDataMapper that will be created through the Factory Method CreateMapperin Baz.Settings class. This will cause it to fail. Adding Bar2 to the ConsoleTester project reference will remedy the problem.