Half way through that message when I have seen the problem.
I have a class called typefactory, this class has a dictionary of types to simple names of the types. This is a single and is required for the framework I have designed to work. This is in an assembly called framework. The call below is in a different assebly and causes the error.
coll.Add(IEntity2)(Activator.CreateInstance(TypeFactory.Instance.GetType("ScratchingFileEntity"))));
however this call works
coll.Add( (IEntity2)(Activator.CreateInstance(typeof(ScratchingFileEntity))));
Trouble is the collections are only known by simple name (string) at this point.....
Reply I was in the middle of......
OK here is the error. Its not really possible that I use the collections on the entites themselves as the type isnt know until runtime therefore im using standard interfaces like IEntity2 and know no specific information about the entities themselves. This section of the code is generic.
System.InvalidCastException: Unable to cast object of type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity' to type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity'.
Line 980: if(relatedEntity!=null)
Line 981: {
Line 982: _members = (UserEntity)relatedEntity;
Line 983: _members.ActiveContext = base.ActiveContext;
Line 984: _members.AfterSave+=new EventHandler(OnEntityAfterSave);
Source File: C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs Line: 982
Stack Trace:
[InvalidCastException: Unable to cast object of type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity' to type 'ScratchTracks.GeneratedCode.EntityClasses.UserEntity'.]
ScratchTracks.GeneratedCode.EntityClasses.ScratchingFileEntity.SetupSyncMembers(IEntity2 relatedEntity) in C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs:982
ScratchTracks.GeneratedCode.EntityClasses.ScratchingFileEntity.SetRelatedEntity(IEntity2 relatedEntity, String fieldName) in C:\temp\ST\DatabaseGeneric\EntityClasses\ScratchingFileEntity.cs:328
SD.LLBLGen.Pro.ORMSupportClasses.EntityCollectionBase2.Add(IEntity2 entityToAdd) +556
ScratchTracks.UI.Web.Controls.DynamicForm.PersistNewObject() in C:\temp\ST\UIWeb\Controls\DynamicForm.cs:155
ScratchTracks.UI.Web.Controls.InputControl.Submit_Click(Object sender, EventArgs e) in C:\temp\ST\UIWeb\Controls\InputControl.cs:322
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3839
source code
IEntity2 primaryEntity = persistingEntities[this.PersistedObject];
persistingEntities.Remove(this.PersistedObject);
UserEntity u = new UserEntity();
System.Collections.ArrayList al = u.GetMemberEntityCollections();
IEntityCollection2 coll = (IEntityCollection2)al[1];
//works
coll.Add(new ScratchingFileEntity());
//causes error coll.Add(IEntity2)(Activator.CreateInstance(TypeFactory.Instance.GetType("ScratchingFileEntity"))));