Generic Base Class/Retaining Previous Version of An Object

I have the following base class:

using System;
using System.Collections.Generic;
using System.Reflection;

namespace CharlesLaboratory
{
public abstract class EntityPreviousState<TEntity>
{
private TEntity _previousVersion;

[NonComparable(true)]
public TEntity PreviousVersion
{
get
{
return _previousVersion;
}
set
{
_previousVersion = value;
}
}

private bool _deleted;

[NonComparable(true)]
public bool Deleted
{
get
{
return _deleted;
}
set
{
_deleted = value;
}
}

/// <summary>
/// This method returns a set of key/value pairs that contain the names and values of any properties not implementing
/// the NonComparable attribute that are different from the previous version of the type that
/// was built when it was instantiated.
/// </summary>
/// <returns>Dictionary<string,string></returns>
public Dictionary<string, string> ChangedProperties()
{
Dictionary<string, string> propertyList = new Dictionary<string, string>();

Type thisType = this.GetType();
PropertyInfo[] thisProperties = thisType.GetProperties();

Type prevType = PreviousVersion.GetType();
PropertyInfo[] prevProperties = prevType.GetProperties();

foreach (PropertyInfo thisProp in thisProperties)
{
foreach (PropertyInfo prevProp in prevProperties)
{
if (prevProp.Name == thisProp.Name)
{
if (!NonComparableProperty(prevProp))
{
if (prevProp.CanRead == true || thisProp.CanRead == true)
{
object oPrevValue = prevProp.GetValue(this, null);
object oThisValue = thisProp.GetValue(PreviousVersion, null);

if (!oPrevValue.Equals(oThisValue))
propertyList.Add(prevProp.Name, oPrevValue.ToString());
}
}
}
}
}

return propertyList;
}


private bool NonComparableProperty(PropertyInfo prevProp)
{
object[] attributesArray = prevProp.GetCustomAttributes(false);
foreach (object attribute in attributesArray)
{
NonComparableAttribute nca = (NonComparableAttribute)attribute;
if (nca != null)
{
return true;
}
}
return false;
}
/// <summary>
/// This abstract method will be implemented in all subclasses so that copy behavior specific to the type
/// can be provided.
/// </summary>
/// <returns></returns>
protected abstract TEntity CreateCopy();
}
}

Its purpose is to allow any subclasses to retain a previous version of themselves, and allow for reporting of changes, without having to hardcode property names. This class inherits it:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Security.Permissions;

namespace CharlesLaboratory
{
public class BusinessEntityOne : EntityPreviousState<BusinessEntityOne>
{
public BusinessEntityOne()
{
}

public BusinessEntityOne(string name, Int32 code)
{
_name = name;
_code = code;
PreviousVersion = CreateCopy();
}

private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}

private System.Int32 _code;
public System.Int32 Code
{
get
{
return _code;
}
set
{
_code = value;
}
}

protected override BusinessEntityOne CreateCopy()
{
BusinessEntityOne newCopy = new BusinessEntityOne();
newCopy.Name = this._name;
newCopy.Code = this._code;
return newCopy;
}

}
}

(I haven't provided the code for the NonComparable attribute; you can infer what it does based on the code)

I use this in one solution, it works great.

In my actual development solution, my unit tests bomb, giving me a VerificationException.

I have reached my wit's end. Help, anyone?

[4509 byte] By [CharlesSustek] at [2007-12-28]
# 1

Not sure what unit testing framwork / methodology you're using. However, what's going on is that your code is not verifiable as type-safe, so when run under a security policy (.net policy or a sandboxed appdomain, the latter is the most likely case for a unit testing tool) that prohibits unverifiable code, an exception is thrown.

PhilipRieck at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
PhilipRieck wrote:

Not sure what unit testing framwork / methodology you're using. However, what's going on is that your code is not verifiable as type-safe, so when run under a security policy (.net policy or a sandboxed appdomain, the latter is the most likely case for a unit testing tool) that prohibits unverifiable code, an exception is thrown.

I'm using Visual Studio 2005's built-in unit testing. Why would the unit test bomb on it, yet when I create an instance of the subclass in a console app, it works brilliantly?

How should I go about resolving this?

CharlesSustek at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Charles Sustek wrote:

PhilipRieck wrote:

Not sure what unit testing framwork / methodology you're using. However, what's going on is that your code is not verifiable as type-safe, so when run under a security policy (.net policy or a sandboxed appdomain, the latter is the most likely case for a unit testing tool) that prohibits unverifiable code, an exception is thrown.

I'm using Visual Studio 2005's built-in unit testing. Why would the unit test bomb on it, yet when I create an instance of the subclass in a console app, it works brilliantly?

How should I go about resolving this?

Solved it. It was a permissions thing. I added the following line to the AssemblyInfo.cs file in my class library project:

[assembly: ReflectionPermission(SecurityAction.RequestMinimum, MemberAccess = true)]

Now it works brilliantly. Thanks for your help.

CharlesSustek at 2007-9-3 > top of Msdn Tech,Visual C#,Visual C# General...