We are supposed to instantiate our entities through a factory since they are set up differently on the client and server. I want to make sure this is the case but cant quite get it to work.
public interface IEntityFactory
{
TEntity Create<TEntity>() where TEntity : new();
}
public abstract class Entity
{
protected Entity()
{
VerifyEntityIsCreatedThroughFactory();
}
[Conditional("DEBUG")]
private void VerifyEntityIsCreatedThroughFactory()
{
foreach (var methodBase in new StackTrace().GetFrames().Select(x => x.GetMethod()))
{
if (!typeof(IEntityFactory).IsAssignableFrom(methodBase.DeclaringType)
|| methodBase.Name != "Create")
continue;
// The generic type is TEnitiy but I want the provided type!
if (methodBase.GetGenericArguments()[0] != GetType())
Debug.Fail(string.Format("Use factory when creating {0}.", GetType().Name));
}
}
}