Using Microsoft Unit Test Wizard, it creates Accessor objects if you need to test a non-public property in another project. Inside my Unit Tests I create helper functions so tha
Here's my take on making that method generic.
public abstract class BaseAccount
{
public string Notes;
public virtual void Create() { ... }
}
public class Account : BaseAccount { ... }
public class Account_Accessor : BaseAccount { ... }
internal static void CreateAccount(out T account, bool saveToDatabase) where T : BaseAccount
{
DateTime created = DateTime.Now;
string createdBy = _testUserName;
account = (T)Activator.CreateInstance(typeof(T), new object[] { created, createdBy });
account.Notes = Utilities.RandomString(1000);
if (saveToDatabase)
account.Create();
}
I assume that Account and Account_Accessor are similar enough that they can share a similar class hiearchy, or could implement the same interface. In this example, I've provided an abstract class from which they both derive, but it is very easy to do it with an interface instead. However, the full implementation will have to be done in both class.
Knowing that, I can constraint the generic method so that T is only a child of BaseAccount. This way, I can access member from that base class without knowing the real type of T.