Click here to Skip to main content
12,403,811 members (69,738 online)
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads

Stats

82.2K views
972 downloads
140 bookmarked
Posted

Some Useful Concurrency Classes and A Small Testbench

, 15 Jan 2007 CPOL
Useful concurrency classes and small test bench in C#

#region Using Directives

using System;
using System.Reflection;
using MPFramework.AppCore.PlatformUtilities;
using MPFramework.AppCore.Manufacturing.TypeHandling;

#endregion

namespace MPFramework.AppCore.Manufacturing.SpecializedTypes
{
	#region IReferencedValueType Interface
	/// <summary>
	/// Non-generic interface that just allows us to examine the Type of
	/// a wrapped Value, etc. You need this if you want to handle a collection
	/// of heterogeneous Types or discover Types at runtime. Use the Generic when
	/// you can....
	/// </summary>
	public interface IReferencedValueType
	{
		#region Properties
		/// <summary>
		/// Gets a BOXed version of the internal value. Sets the internal value from
		/// a BOXed version. This is needed for late-bound Type identification
		/// and casting.
		/// </summary>
		System.ValueType BoxedValue
		{ get; set; }
		#endregion // Properties
		#region Methods
		/// <summary>
		/// Gets the Type of the internal value.
		/// </summary>
		System.Type GetValueTypeType();
		/// <summary>
		/// Determines if the internal value of the current IRVT is the same as
		/// another's.
		/// </summary>
		/// <param name="otherIReferencedValueType">
		/// The other referenced Type to compare to.
		/// </param>
		System.Boolean IsValueSameAs(IReferencedValueType otherIReferencedValueType);
		/// <summary>
		/// Swaps the internal value with another.
		/// </summary>
		/// <param name="otherIReferencedValueType">
		/// The other referenced Type to switch internal values with.
		/// </param>
		/// <exceptions>
		/// <exception>
		/// This will throw an <see cref="ApplicationException"/> if internal Types
		/// do not match. ("Type Mismatch in SwapValues()").
		/// </exception>
		/// </exceptions>
		void SwapValues(IReferencedValueType otherIReferencedValueType);
		#endregion // Methods
	}
	#endregion IReferencedValueType Interface

	#region IReferencedValueType Generic Interface
	/// <summary>
	/// Generic interface that actually allows us to manipulate the Type.
	/// </summary>
	/// <typeparam name="UValueType">
	/// This is the ValueType that the Generic Interface exposes. The interface
	/// contains methods for manipulating the Type as a Generic ValueType without
	/// BOXing.
	/// </typeparam>
	public interface IReferencedValueType<UValueType> :	IReferencedValueType
		where UValueType : struct
	{
		#region Properties
		/// <summary>
		/// Gets/sets the internal value. Note that this Type need not be the same
		/// as the Type inside the BOX defined by BoxedValue. BoxedValue is generally
		/// required to provide a host for UValueType somehow, but need not contain
		/// a UValueType directly.
		/// </summary>
		UValueType Value
		{ get; set; }
		#endregion // Properties
		#region Methods
		/// <summary>
		/// Swaps the internal value with another of the same Type.
		/// </summary>
		/// <param name="otherIReferencedValueType">
		/// The other referenced Type to switch internal values with.
		/// </param>
		void SwapValues(IReferencedValueType<UValueType> otherIReferencedValueType);
		#endregion // Methods

	}
	#endregion // IReferencedValueType Generic Interface

	#region IValueProp Interface
	/// <summary>
	/// This interface allows us to access a BOXed <see typeparamref="UValueType"/>
	/// to extract its value without BOXing/unBOXing.
	/// </summary>
	/// <typeparam name="UValueType">
	/// This is our internal contained <see cref="System.ValueType"/>.
	/// </typeparam>
	public interface IValueProp<UValueType> where UValueType : struct
	{
		/// <summary>
		/// This Property simply gets/sets the internal value of a struct.
		/// </summary>
		UValueType Value
		{ get; set;}
	}
	#endregion // IValueProp Interface.

	#region A default implementation of a non-generic RVT
	/// <summary>
	/// <para>
	/// This class implements a simple reference Type that wraps a value type so
	/// that it can be used with managed synchronization techniques. This class
	/// does the simple and obvious thing - it just puts a UValueType directly in a
	/// BOX.
	/// </para>
	/// <para>
	/// Prior to .Net 2.0, we used this class a base class for Type-specific RVT's.
	/// Now we use Generics instead of any Type-specific RVT's, but we need to be
	/// backward-compatible yet for a while, so we need to keep this.
	/// </para>
	/// </summary>
	/// <remarks>
	/// This class implements three versions of the generic IEquatable. It sometimes
	/// needs to equated as a class and sometimes as an Interface reference in
	/// different applications.
	/// </remarks>
	public class ReferencedValueType : IReferencedValueType, IEquatable<ReferencedValueType>,
		IEquatable<IReferencedValueType>, IEquatable<System.ValueType> 
	{
		/// <summary>
		/// This is a direct pass-through to shut up the compiler. Our == and !=
		/// Ops are not defined on the System.Object, but the compiler does not
		/// understand this. One could simply ignore this, but we are a bit fastidious
		/// about compiler warnings. You might want to ignore it in a production system
		/// due to the extra overhead in the override call.
		/// </summary>
		/// <param name="obj">
		/// A <see cref="System.Object"/> to be tested for equality with us.
		/// </param>
		/// <returns>
		/// Just returns the result of <see cref="System.Object.Equals(object)"/>
		/// </returns>
		public override bool Equals(object obj)
		{
			return base.Equals(obj);
		}
		/// <summary>
		/// Same thing here - want to shut up the compiler.....
		/// </summary>
		/// <returns>
		/// Just returns the result of <see cref="System.Object.GetHashCode()"/>
		/// </returns>
		public override Int32 GetHashCode() { return base.GetHashCode(); } 
		#region Class Fields
		/// <summary>
		/// This is the Type of the contained value Type. We won't know
		/// this in this base class until we are instantiated - then it is immutable.
		/// </summary>
		protected System.Type m_valueTypeType = null;
		/// <summary>
		/// This is a container for our value Type. This is a BOXed value because
		/// we don't have the real Type until instantiation of the class. We also
		/// need this for operations with other RVT's.
		/// </summary>
		protected System.ValueType m_boxedValueType = null;
		#endregion // Class Fields
		#region Constructors
		/// <summary>
		/// For inheritors and reflection. We can't instantiate without knowing the Type.
		/// </summary>
		protected ReferencedValueType() { }
		/// <summary>
		/// This constructor loads the internal value Type.
		/// </summary>
		/// <param name="valueTypeType">
		/// The value to be loaded.
		/// </param>
		public ReferencedValueType(System.Type valueTypeType)
		{
			// Get the Type's Type.
			m_valueTypeType = valueTypeType;
			// Must be a value Type
			if(!(m_valueTypeType.IsValueType)) {
				ManufacturingUtils.ManufacturingErrorHandler.ProcessError(
				ManufacturingUtils.BadConstructionParamsError, null,
					null, true, null,
						"\nReferencedValueType(valueTypeType) (NOT Value TYPE)\n");
			}

			// Create a default BOXed Type and attach it to our BOX pointer.
			m_boxedValueType = (System.ValueType) valueTypeType.Assembly.CreateInstance(valueTypeType.FullName);
		}
		/// <summary>
		/// This constructor loads the internal value Type.
		/// </summary>
		/// <param name="boxedValueType">
		/// The reference to a BOXed value Type on the heap to be copied to our
		/// internal managed reference. Cannot be <c>null</c>.
		/// </param>
		/// <remarks>
		/// Note that this constructor DOES NOT create an independent copy of the
		/// wrapped value type that the input reference points to. Use this
		/// constructor when the input parameter points to a wrapped value Type
		/// on the heap that should be attached as this <see cref="ReferencedValueType"/>'s
		/// storage element.
		/// </remarks>
		public ReferencedValueType(System.ValueType boxedValueType)
		{
			// Get the Type's Type.
			m_valueTypeType = boxedValueType.GetType();
			// Must be a value Type
			if(!(m_valueTypeType.IsValueType)) {
				ManufacturingUtils.ManufacturingErrorHandler.ProcessError(
				ManufacturingUtils.BadConstructionParamsError, null,
					null, true, null,
						"\nReferencedValueType(BOXedValueType) (NOT Value TYPE)\n");
			}

			// Copy the incoming BOX pointer to our BOX pointer.
			m_boxedValueType = boxedValueType;
		}
		#endregion // Constructors
		#region Properties
		/// <summary>
		/// Gets/sets the internal value as a BOXed value.
		/// </summary>
		/// <value>
		/// <para>
		/// In the Get method, a managed reference to the single BOXed value owned
		/// by this instance of the class is returned. This is not an independent
		/// copy and will be potentially changed whenever this instance of the class
		/// is manipulated.
		/// </para>
		/// <para>
		/// In the Set method, the internal copy of our <see typeparamref="UValueType"/>
		/// is set by unBOXing the incoming managed reference.
		/// </para>
		/// </value>
		/// <exceptions>
		/// Exception in the "Set" method if the two wrapped value Types are
		/// different. This specific implementation of <see cref="IReferencedValueType"/>
		/// does not allow the internal Type of the BoxedValue to change after the class
		/// is constructed.
		/// </exceptions>
		public virtual System.ValueType BoxedValue
		{
			get	{
				// Return the BOXed value.
				return m_boxedValueType;
			}
			set	{
				if(value.GetType() != m_boxedValueType.GetType())
					throw new ApplicationException("Type mismatch in SwapValues()");
				m_boxedValueType = value;
			}
		}
#endregion // Properties
		#region Methods
		/// <summary>
		/// This is an IEquatable implementation for when the Type needs to be handled
		/// as a class, not an Interface. This implementation just calls IsValueSameAs().
		/// Override for specific applications.
		/// </summary>
		/// <param name="otherReferencedValueType">
		/// The other RVT that is to be compared by examining its internal value and
		/// comparing to our internal value.
		/// </param>
		/// <returns>
		/// <c>true if the wrapped values are equal.</c>
		/// </returns>
		public virtual System.Boolean Equals(ReferencedValueType otherReferencedValueType)
		{
			// Call the virtual function.
			return IsValueSameAs(otherReferencedValueType);
		}
		/// <summary>
		/// This demonstrates how to create an overloaded operator for the class.
		/// This is done with the standard operator keyword. Normally, one would
		/// wish to map this to the underlying Equals method, but not always.
		/// </summary>
		/// <param name="oneReferencedValueType">
		/// The first RVT to compare for equality.
		/// </param>
		/// <param name="anotherReferencedValueType">
		/// This is a different RVT to compare it to.
		/// </param>
		/// <returns>
		/// <c>true</c> if equal.
		/// </returns>
		/// <remarks>
		/// Note that operators are static and thus cannot be virtual. Also note,
		/// however, that operators can be mapped to virtual methods, in this case,
		/// calling the virtual Equals method. Operators can thus be made to "follow"
		/// an underlying virtual implementation as it is overridden.
		/// </remarks>
		public static bool operator ==(ReferencedValueType oneReferencedValueType,
			ReferencedValueType anotherReferencedValueType)
		{
			if(oneReferencedValueType.Equals(anotherReferencedValueType)) return true;
			return false;
		}
		/// <summary>
		/// This is the matching != operator that must always be here in C#.
		/// </summary>
		/// <param name="oneReferencedValueType">
		/// The first RVT to compare for equality.
		/// </param>
		/// <param name="anotherReferencedValueType">
		/// This is a different RVT to compare it to.
		/// </param>
		/// <returns>
		/// <c>false</c> if equal.
		/// </returns>
		public static bool operator !=(ReferencedValueType oneReferencedValueType,
			ReferencedValueType anotherReferencedValueType)
		{
			if(oneReferencedValueType == anotherReferencedValueType) return false;
			return true;
		}
		/// <summary>
		/// The is the implementation of <see cref="IEquatable&lt;IReferencedValueType&gt;"/>
		/// that just calls IsValueSameAs(). Override for specific applications.
		/// </summary>
		/// <param name="otherIReferencedValueType">
		/// The other IRVT that is to be compared by examining its internal value and
		/// comparing to our internal value.
		/// </param>
		/// <returns>
		/// <c>true if the wrapped values are equal.</c>
		/// </returns>
		public virtual System.Boolean Equals(IReferencedValueType otherIReferencedValueType)
		{
			// Call the virtual function.
			return IsValueSameAs(otherIReferencedValueType);
		}
		/// <summary>
		/// This demonstrates how to create an operator for the class.
		/// This time, it is done for comparison against another
		/// <see cref="IReferencedValueType"/>.
		/// </summary>
		/// <param name="referencedValueType">
		/// The RVT to compare for equality.
		/// </param>
		/// <param name="anIReferencedValueType">
		/// This is an arbitrary IRVT to compare it to.
		/// </param>
		/// <returns>
		/// <c>true</c> if equal.
		/// </returns>
		public static bool operator ==(ReferencedValueType referencedValueType,
			IReferencedValueType anIReferencedValueType)
		{
			if(referencedValueType.Equals(anIReferencedValueType)) return true;
			return false;
		}
		/// <summary>
		/// This is the matching != operator that must always be here in C#.
		/// </summary>
		/// <param name="referencedValueType">
		/// The RVT to compare for equality.
		/// </param>
		/// <param name="anIReferencedValueType">
		/// This is an arbitrary IRVT to compare it to.
		/// </param>
		/// <returns>
		/// <c>false</c> if equal.
		/// </returns>
		public static bool operator !=(ReferencedValueType referencedValueType,
			IReferencedValueType anIReferencedValueType)
		{
			if(referencedValueType == anIReferencedValueType) return false;
			return true;
		}
		/// <summary>
		/// Gets the Type of the internal value.
		/// </summary>
		public virtual System.Type GetValueTypeType()
		{ return m_valueTypeType; }
		/// <summary>
		/// Swaps the internal value with another of the same Type.
		/// </summary>
		/// <param name="otherReferencedValueType">
		/// The other referenced value Type to switch internal values with.
		/// </param>
		/// <exceptions>
		/// Exception if the two wrapped value Types are different.
		/// </exceptions>
		public virtual void SwapValues(IReferencedValueType otherReferencedValueType)
		{
			Type typeOfValueType = GetValueTypeType();
			if(typeOfValueType != otherReferencedValueType.GetValueTypeType())
				throw new ApplicationException("Unequal Types");
			// Just a managed pointer for the switch.
			System.ValueType tempObject;

			// Do the switch.
			tempObject = BoxedValue;
			BoxedValue = otherReferencedValueType.BoxedValue;
			otherReferencedValueType.BoxedValue = tempObject;
		}
		/// <summary>
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
		/// </summary>
		/// <param name="otherReferencedValueType">
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>. This parameter
		/// may be <c>null</c>, in which case, <c>false</c> is returned.
		/// </param>
		/// <returns>
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
		/// </returns>
		/// <remarks>
		/// Attributed as <c>PlatformSpecific</c> since reflection-based
		/// <see cref="System.ValueType"/> field comparisons are not in ECMA.
		/// </remarks>
		[PlatformSpecific(PlatformsSupported = CLRPlatformsSupported.MSWNT)]
		public virtual System.Boolean IsValueSameAs(IReferencedValueType otherReferencedValueType)
		{
			// Safety valve.
			if(otherReferencedValueType == null) return false;
			// Different Types can't be equal.
			if(GetValueTypeType() != otherReferencedValueType.GetValueTypeType()) return false;
			// Let's try for IComparable.
			//// Note that this Interface pointer could be a class field and be updated
			//// whenever BoxedValue was updated. If your application is doing lots of
			//// comparisons and you don't care about memory usage, this would be the
			//// way to go......
			IComparable boxedValueAsIComparable;
			if((boxedValueAsIComparable = (BoxedValue as IComparable)) != null)
			{
				if((boxedValueAsIComparable.CompareTo(otherReferencedValueType.BoxedValue) == 0)) {
				    return true;
				}
				else return false;
			}
			// Let the CLR examine the Types. The CLR's Equals(object1, object2) on
			// System.ValueType args examines each instance's fields and compares
			// field-by-field in MS's implementation. So we could use the following:
			// return Equals(BoxedValue, otherReferencedValueType.BoxedValue);
			// However, this is not in ECMA, so we are concerned......
			// krm todo -- check back on current Mono.
			//
			// An inheritor might well want to check for a specific Type of wrapped
			// struct in order to make a specialized comparison. This could be done here.
			//
			// A typical inheritor interested in speed will override the default Equals,
			// which is usually performed through reflection. There is one problem
			// with overriding the single argument "anObject.Equals(anotherObject)" method.
			// If you intend to place the RVT in a hastable or a dictionary you will
			// always need to override GetHashCode(System.Object) or ensure the constructor
			// that takes IEquatable is used. For our situation, where we always have
			// BOXed ValueTypes, it's better to provide a fresh set of equality
			// comparers for ValueTypes like the next two methods.
			return Equals(BoxedValue, otherReferencedValueType.BoxedValue);
		}
		/// <summary>
		/// This is a static Equals method defined for ValueType arguments. Note that
		/// this method will be invoked only for equality comparison of ValueTypes. It
		/// does not affect calls to Equals(object, object) and is completely separate
		/// from it.
		/// </summary>
		/// <param name="valueType1">
		/// The first <see cref="System.ValueType"/> to be compared.
		/// </param>
		/// <param name="valueType2">
		/// The second <see cref="System.ValueType"/> to be compared.
		/// </param>
		/// <returns>
		/// <c>true</c> if the two ValueTypes are equal. <c>false</c> if they
		/// are not or if they are different Types or if either is <c>null</c>.
		/// </returns>
		/// <remarks>
		/// <para>
		/// Note that this method simply calls into Equals(object, object). The
		/// method is here only to demonstrate the possibility of providing a distinct
		/// Equality method for ValueTypes.
		/// </para>
		/// <para>
		/// In a typical scenario, the user could filter on a selection of ValueTypes
		/// and provide some specific comparisons for them, delegating the rest to
		/// Equals(object, object). Note that Equals(object, object) uses reflection
		/// to examine each field of a ValueType object. If you are worried about
		/// speed with certain Types, or want a different comparison, here is a
		/// convenient place to trap and process them with a custom comparison.
		/// This static method could be provided in a utility class. Usually, though,
		/// you'd want to attach it to a particular class, so it would only need to
		/// know about the specific ValueTypes that the class might be designed to wrap.
		/// You might even want to keep this method private.
		/// </para>
		/// <para>
		/// Note that, like Equals(object, object), this method will cause BOXing of
		/// unBOXed structs. A System.ValueType is a subtype of System.Object which can
		/// only have a HOME on the GCheap.
		/// </para>
		/// </remarks>
		public static bool Equals(ValueType valueType1, ValueType valueType2)
		{
			// Safety valve.
			if((valueType1 == null) || (valueType2 == null)) return false; 
			// The joint Type of the two incoming values.
			Type typeType = null;
			// Incoming ValueTypes whose System.Type are not equal could be equal
			// in an arcane situation. We could be comparing just one field on two
			// ValueTypes that are of a different System.Type. We don't allow this
			// here. The Equals(IReferencedValueType) methods generally can provide
			// this functionality when we want to just compare two ValueTypes
			// based on their IReferencedValueType-ed-ness.
			if((typeType = valueType1.GetType()) != valueType2.GetType())
				return false;

			// Let's show how we make a special case out of SimpleStruct. Let's
			// say we were interested in only the first integer. If they match, we
			// want equality. This test is actually used in practice when the RVT
			// wraps a Byte array, part of which is data and part of which is control
			// information.
			if(typeType == typeof(CSharpSimpleStruct)) {
				if(((CSharpSimpleStruct)valueType1).FirstInt32 == ((CSharpSimpleStruct)valueType2).FirstInt32)
					return true;
				return false;
			}

			// Note that the cast to an object is necessary. If we leave it out,
			// the compiler will map the call to us again and result in a recursion.
			return Equals((object)valueType1, (object)valueType2);
		}
		/// <summary>
		/// This method performs equality comparisons, this time specifically on
		/// BOXed ValueTypes.
		/// </summary>
		/// <param name="valueType">
		/// This is a <see cref="System.ValueType"/> that is to be compared with
		/// our internal <see cref="BoxedValue"/>.
		/// </param>
		/// <returns>
		/// <c>true</c> if the BOXed ValueTypes agree.
		/// </returns>
		/// <remarks>
		/// <para>
		/// Note that the ValueType.Equals(ValueType) method defined here does
		/// not interfere with Equals(object). All the usual admonishions about
		///	redefining GetHashCode at the same time no longer apply, since Equals(object)
		/// is not overridden or hidden.
		/// </para>
		/// <para>
		/// Note that, like Equals(ValueType, ValueType), this method will cause boxing
		/// of unBOXed value Types.
		/// </para>
		/// </remarks>
		public bool Equals(System.ValueType valueType)
		{
			// We don't allow different Types.
			if(valueType.GetType() != BoxedValue.GetType())
				return false;

			// Let our static method handle the dirtywork.......
			return Equals(valueType, BoxedValue);
		}
		#endregion // Methods
	}
	#endregion // A default implementation of a non-generic RVT

	#region A default implementation of a generic RVT
	/// <summary>
	/// <para>
	/// This class implements a simple reference Type that wraps a value type so
	/// that it can be used with managed synchronization techniques. We reimplement
	/// IEquatable directed to our internal equality comparison. This is the generic
	/// version. It is implemented as deriving from the non-generic version because
	/// we must handle heterogeneous RVT's, including legacy RVT's that are based
	/// on the non-generic version.
	/// </para>
	/// <para>
	/// Note that this is only one specific example of an implementation of
	/// <see cref="IReferencedValueType&lt;UValueType&gt;"/>. We do use this in
	/// MPFramework along with several other implementations. As we go along, we will
	/// describe where certain design decisions have been made for this particular
	/// implementation. 
	/// </para>
	/// <para>
	/// This class also has special provisions for accepting and manipulating a
	/// <see typeparamref="UValueType"/> implementing <see cref="IValueProp&lt;UValueType&gt;"/>
	/// and manipulating it through that Interface. See the appropriate constructor.
	/// </para>
	/// </summary>
	/// <remarks>
	/// This class needs to implement <see cref="IEquatable&lt;IReferencedValueType&gt;"/>, which it
	/// reaffirms. Any derived class could also implement
	/// <c>IEquatable&lt;IReferencedValueType&lt;UValueType&gt;&gt;</c> if it wanted
	/// to compare a wrapped <see typeparamref="UValueType"/> with another.
	/// </remarks>
	public class ReferencedValueType<UValueType>: ReferencedValueType, IEquatable<IReferencedValueType>,
		IReferencedValueType<UValueType> where UValueType : struct
	{
#region Class Fields
		/// <summary>
		/// This is the unBOXed copy of the Type.
		/// </summary>
		protected UValueType m_UValueType = default(UValueType);
		/// <summary>
		/// This indicates that the BOX needs to be updated with the generic before
		/// reading it. This is used so that in situations where we are BOXing, the
		/// BOX is not updated unless necessary.
		/// </summary>
		protected System.Boolean m_boxIsDirty = false;
		/// <summary>
		/// This bool tells us if our contained value Type implements IValueProp.
		/// </summary>
		protected System.Boolean m_isIValueProp = false;
#endregion // Class Fields
#region Constructors
		/// <summary>
		/// Just embeds a default(UValueType).
		/// </summary>
		public ReferencedValueType()
			// Put the Type in it's box
			: base(default(UValueType))
		{
			Type typeType = GetValueTypeType();
			if(typeType is IValueProp<UValueType>) m_isIValueProp = true;
		}
			
		/// <summary>
		/// This constructor loads the internal value Type with a specific value.
		/// </summary>
		/// <param name="UValueType">
		/// The value to be loaded.
		/// </param>
		public ReferencedValueType(UValueType UValueType)
			// Put the Type in it's BOX
			: base(UValueType)
		{
			Type typeType = GetValueTypeType();
			if(typeType is IValueProp<UValueType>) {
				m_isIValueProp = true;
				return;
			}
			// If not an IValueProp, we will need to set the value.
			m_UValueType = UValueType;
		}
		/// <summary>
		/// This constructor accepts a <see typeparamref="UValueType"/> wearing a
		/// <see cref="IValueProp&lt;UValueType&gt;"/> and employs it to reference
		/// the struct inside the working BOX. This allows our internal methods
		/// to work without ever BOXing the contained <see typeparamref="UValueType"/>,
		/// since the interface allows us to move the <see typeparamref="UValueType"/> 
		/// into and out of a solitary BOX.
		/// </summary>
		/// <param name="vPStruct">
		/// This is the reference to the struct implementing IValueProp.
		/// </param>
		/// <exceptions>
		/// The constructor will throw an <see cref="ApplicationException"/> if the
		/// <see paramref="vPStruct"/> does not implement all of the comparison
		/// Interfaces that the underlying <see typeparamref="UValueType"/> does.
		/// This includes <see cref="IEquatable&lt;UValueType&gt;"/>,
		/// <see cref="IComparable&lt;UValueType&gt;"/> and <see cref="IComparable"/>.
		/// </exceptions>
		public ReferencedValueType(IValueProp<UValueType> vPStruct)
		{
			// For checks.
			bool UValueTypeImplements;
			bool tValuePropImplements;
			m_valueTypeType = typeof(UValueType);
			Type vPType = vPStruct.GetType();
			///////////////////////////////////////////////////////////////////
			//// We must now examine vPStruct to ensure it implements all of the
			//// equality and comparison ops that UValueType does. It's best to
			//// catch this at construction time and generate a reasonable error
			//// message. Note that we could have designed IValueProp to inherit
			//// from all of these interfaces, but we want to keep it general
			//// purpose. The client could very well build a derived interface
			//// that has more specific constraints.
			///////////////////////////////////////////////////////////////////
			//
			// IComparable.
			UValueTypeImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable), typeof(UValueType));
			tValuePropImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable), vPType);
			if(UValueTypeImplements && !tValuePropImplements){
				ManufacturingUtils.ManufacturingErrorHandler.ProcessError(
				ManufacturingUtils.BadConstructionParamsError, null,
					null, true, null,
						"\nReferencedValueType(IValueProp) (IValueProp doesn't implement IComparable)\n");
			}
			// IComparable<UValueType>.
			UValueTypeImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable<UValueType>), typeof(UValueType));
			tValuePropImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable<UValueType>), vPType);
			if(UValueTypeImplements && !tValuePropImplements){
				ManufacturingUtils.ManufacturingErrorHandler.ProcessError(
				ManufacturingUtils.BadConstructionParamsError, null,
					null, true, null,
						"\nReferencedValueType(IValueProp) (IValueProp doesn't implement IComparable<T>)\n");
			}
			// IEquatable<UValueType>.
			UValueTypeImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IEquatable<UValueType>), typeof(UValueType));
			tValuePropImplements
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IEquatable<UValueType>), vPType);
			if(UValueTypeImplements && !tValuePropImplements){
				ManufacturingUtils.ManufacturingErrorHandler.ProcessError(
				ManufacturingUtils.BadConstructionParamsError, null,
					null, true, null,
						"\nReferencedValueType(IValueProp) (IValueProp doesn't implement IEquatable)\n");
			}

			m_isIValueProp = true;
			m_boxedValueType = (System.ValueType) vPStruct;
		}
#endregion // Constructors
		#region Properties
		/// <summary>
		/// Gets/sets the internal value as a BOXed value.
		/// </summary>
		/// <value>
		/// <para>
		/// In the Get method, a managed reference to the single BOXed value owned
		/// by this instance of the class is returned. This is not an independent
		/// copy and will be potentially changed whenever this instance of the class
		/// is manipulated.
		/// </para>
		/// <para>
		/// In the Set method, the internal copy of our <see typeparamref="UValueType"/>
		/// is set by unBOXing the incoming managed reference.
		/// </para>
		/// </value>
		public override System.ValueType BoxedValue
		{
			get	{
				// Create a new BOXed value if we need to.
				if(!m_isIValueProp) {
					// If our UValueType is not wearing IValueProp, we must reconcile
					// the BOX. We avoid creating a new GCheap allocation until
					// the last minute and also use the flag to avoid it if possible.
					if(m_boxIsDirty) {
						m_boxedValueType = m_UValueType;
						m_boxIsDirty = false;
					}
				}
				return base.BoxedValue;
			}
			set {
				// Copy into our local value. This overwrites our m_boxedValueType
				// reference and let's the old fall out of scope. 
				base.BoxedValue = value;
				// If BOXing, pull out the value.
				if(!m_isIValueProp) {
					// This cast will succeed if base.BoxedValue did not throw an
					// exception.
					m_UValueType = (UValueType)m_boxedValueType;
				}
			}
		}
		/// <summary>
		/// Gets/sets the internal value. This Property reconciles the BOXed version. All
		/// access should be done through this.
		/// </summary>
		public virtual UValueType Value
		{
			get {
				// If BOXing, just return m_UValueType that is on the class.
				if(!m_isIValueProp)	return m_UValueType;
				// Just grab it through IValueProp - pull it out of the BOX.
				else return ((IValueProp<UValueType>)base.BoxedValue).Value;
			}
			set {
				// If BOXing, set the value.
				if(!m_isIValueProp) {
					m_UValueType = value;
					// Flag the BOX.
					m_boxIsDirty = true;
				}
				// Just write into the existing BOX without creating a new one.
				else ((IValueProp<UValueType>)base.BoxedValue).Value = value;
			}
		}
#endregion // Properties
		#region Methods
		/// <summary>
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
		/// </summary>
		/// <param name="otherReferencedValueType">
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
		/// </param>
		/// <returns>
		/// See <see cref="IReferencedValueType&lt;UValueType&gt;"/>.
		/// </returns>
		/// <remarks>
		/// This implementation employs reflection to ascertain whether
		/// <see typeparamref="UValueType"/> implements certain Interfaces that
		/// can be used in an equality comparison operation. The first one that
		/// is found is the one that is used for an equality comparison. If all
		/// else fails, the <c>UValueType.Equals(UValueType)</c> method is called.
		/// If this Equals method has not been overridden on <see typeparamref="UValueType"/>,
		/// <c>System.Object.Equals(System.Object)</c> will be called, the Generic
		/// will be BOXed and the test will be performed through reflection. The Interface that
		/// are checked in order are:
		/// </remarks>
		public override System.Boolean IsValueSameAs(IReferencedValueType otherReferencedValueType)
		{
			UValueType UValueType = default(UValueType);
			Type typeOfValue = GetValueTypeType();
			// Can't match if internal struct Types different.
			if(GetValueTypeType() != otherReferencedValueType.GetValueTypeType()) return false;
			// Figure out if the incoming IRVT is an IRVT<UValueType>.
			bool isOneOfUs
				= TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
					typeof(IReferencedValueType<UValueType>), otherReferencedValueType.GetType());
			if(isOneOfUs)
				// Pull out the value from Value without touching the BOX.
				UValueType = ((IReferencedValueType<UValueType>)otherReferencedValueType).Value;
			else
				// It's a nondescript IRVT, so we have to touch the BOX.
				UValueType = (UValueType)otherReferencedValueType.BoxedValue;
			// Let's first check if the Type implements IEquatable<UValueType>. Don't touch the BOXed
			// version until we make this determination, since that MAY cause a BOXing operation.
			// Although we do not do it here, the knowledge of whether the contained Type
			// implements an interface could be stored at construction time, since the Type
			// does not change. This is left here to demonstrate the way a Type can have
			// its interfaces examined without BOXing. The tradeoff would be speed versus
			// memory footprint of the RVT. We would have to allocate another word to
			// remember the implemented interfaces. The way it is here, we have to do a
			// bit of reflection each time we enter this method.
			bool implementsIEquatable = TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IEquatable<UValueType>), GetValueTypeType());
			if(implementsIEquatable) {
				IEquatable<UValueType> myValueAsEquatable = (BoxedValue as IEquatable<UValueType>);
				// Int32's and other built-in Types will be exiting here, since they implement
				// IEquatable.
				return myValueAsEquatable.Equals(UValueType);
			}
			// Now check if the Type implements IComparable<UValueType> - same deal.
			bool implementsGenericIComparable = TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable<UValueType>), GetValueTypeType());
			if(implementsGenericIComparable) {
				IComparable<UValueType> myValueAsGenericComparable = (BoxedValue as IComparable<UValueType>);
				if(myValueAsGenericComparable.CompareTo(UValueType) == 0)
					return true;
				else
					return false;
			}
			// Now check if the Type implements IComparable - same deal one more time.
			bool implementsIComparable = TypeHandlingUtils.DoesSecondTypeInheritFromFirstType(
				typeof(IComparable), GetValueTypeType());
			if(implementsIComparable) {
				IComparable myValueAsComparable = (BoxedValue as IComparable);
				// non-generic compare needs an object.
				if(myValueAsComparable.CompareTo(otherReferencedValueType.BoxedValue) == 0)
					return true;
				else
					return false;
			}
			// If we get here, we didn't get an answer yet - let the CLR do it
			// through reflection or we'll do it ourselves if we overrode Equals
			// on our custom UValueType.
			return Value.Equals(UValueType);
		}
		/// <summary>
		/// Swaps the internal value with another of the same Type.
		/// </summary>
		/// <param name="otherReferencedValueType">
		/// The other referenced Type to switch internal values with.
		/// </param>
		public virtual void SwapValues(IReferencedValueType<UValueType> otherReferencedValueType)
		{
			UValueType temp = Value;
			Value = otherReferencedValueType.Value;
			otherReferencedValueType.Value = temp;
		}
#endregion // Methods
	}
	#endregion // A default implementation of a non-generic RVT.

	#region Example Closed Types
	#region A Simple RVT Wrapping A Boolean
	/// <summary>
	/// This class implements a simple reference Type that wraps a <see cref="System.Boolean"/>
	/// type so that it can be used in a lock statement.
	/// </summary>
	public class BooleanRVT : ReferencedValueType<System.Boolean>
	{
		/// <summary>
		/// Sets the default - <c>false</c>.
		/// </summary>
		public BooleanRVT()
			: base(){}

		/// <summary>
		/// Constructor stuffs in a boolean value.
		/// </summary>
		/// <param name="boolValue">
		/// The value that the class should contain.
		/// </param>
		public BooleanRVT(System.Boolean boolValue) : base(boolValue) { }
	}
	#endregion // A Simple RVT Wrapping A Boolean
	#region A Simple RVT Wrapping An Int32
	/// <summary>
	/// This class implements a simple reference Type that wraps a <see cref="System.Int32"/>
	/// </summary>
	public class Int32RVT : ReferencedValueType<System.Int32>
	{
		/// <summary>
		/// Sets the default - <c>0</c>. 
		/// </summary>
		public Int32RVT()
			: base() { }

		/// <summary>
		/// Constructor stuffs in an Int32 value.
		/// </summary>
		/// <param name="int32Value">
		/// The value that the class should contain.
		/// </param>
		public Int32RVT(System.Int32 int32Value) : base(int32Value) { }
	}
	#endregion // A Simple RVT Wrapping An Int32
	#endregion // Example Closed Types

	#region Helper and Demonstration Classes
	///////////////////////////////////////////////////////////////////////////
	// This region contains several classes demonstrating how to build RVT's.
	///////////////////////////////////////////////////////////////////////////
	/// <summary>
	/// This class holds an internal struct inside a non-Generic ReferencedValueType.
	/// This example class demonstrates the possibility of wrapping an arbitrary
	/// struct that supports an RVT. This is just about the simplest RVT - it wraps
	/// a struct containing two integers and wearing no interfaces.
	/// </summary>
	public class RVTReferencedSSType : ReferencedValueType
	{
		/// <summary>
		/// Just sticks in the BOXed struct.
		/// </summary>
		public RVTReferencedSSType(CSharpSimpleStruct simpleStruct)
			: base(simpleStruct){}
	}
	/// <summary>
	/// This class holds an internal struct inside a Generic ReferencedValueType.
	/// This example class demonstrates the possibility of wrapping an arbitrary
	/// struct that supports an RVT. It also holds a class wrapping the RVTStruct
	/// so that unsafe methods may be employed to read/write the struct
	/// "inside the box".
	/// </summary>
	public class GenericRVTReferencedStructClass : ReferencedValueType<CSharpSimpleStruct>,
		IEquatable<GenericRVTReferencedStructClass>
	{
		RVTStructClass m_rVTStructClass;
		/// <summary>
		/// Sets the default - <c>false</c>.
		/// </summary>
		public GenericRVTReferencedStructClass()
			: base()
		{
			CSharpSimpleStruct tempValue = new CSharpSimpleStruct();
			Value = tempValue;
			m_rVTStructClass = new RVTStructClass(tempValue);
		}
		/// <summary>
		/// This implementation checks both Int32s.
		/// </summary>
		/// <param name="rVTRST">
		/// Example structure containing two Int32s.
		/// </param>
		/// <returns>
		/// <c>true</c> if both Int32s are the same as the other struct's.
		/// </returns>
		System.Boolean IEquatable<GenericRVTReferencedStructClass>.Equals(GenericRVTReferencedStructClass rVTRST)
		{
			if(Value.FirstInt32 != rVTRST.Value.FirstInt32) return false;
			if(Value.SecondInt32 != rVTRST.Value.SecondInt32) return false;
			return true;
		}
		/// <summary>
		/// Little test method just to demonstrate some pointer issues.
		/// </summary>
		public void ClosedGenericTouchStruct()
		{
			CSharpSimpleStruct rvtstruct = new CSharpSimpleStruct();
			unsafe {
				CSharpSimpleStruct* p1 = &rvtstruct;
				// This doesn't work because m_UValueType is moveable.
				//			RVTStruct* pa = &m_UValueType;
				//			int* p2 = &((RVTStruct)m_UValueType).FirstInt32;
				// This works because the Type (Int32) is known to be a non-managed Type.
				// Doesn't do anything with the BOXed value, however.
				fixed(Int32* p = &((CSharpSimpleStruct)m_UValueType).FirstInt32) { }
				// This works because all of CSharpSimpleStruct is an unmanaged Type
				// (i.e., it does not contain any unmanaged Types as fields.
				// What we lose is that we have to add another level of indirection.
				fixed(CSharpSimpleStruct* p2 = &m_rVTStructClass.m_rVTStruct) { }
				// This doesn't work because we are copying out of the BOX and the
				// result is not a HOME.
				// fixed(CSharpSimpleStruct* p3 = &((CSharpSimpleStruct) m_boxedValueType) ) { }
			}
		}

	}
	/// <summary>
	/// This is just a completely simple test class for wrapping a struct.
	/// </summary>
	public class RVTStructClass
	{
		/// <summary>
		/// The wrapped struct, held just as a non-generic field.
		/// </summary>
		public CSharpSimpleStruct m_rVTStruct;
		/// <summary>
		/// Constructor just accepts and installs the struct.
		/// </summary>
		/// <param name="rVTStruct">Incoming struct.</param>
		public RVTStructClass(CSharpSimpleStruct rVTStruct) { m_rVTStruct = rVTStruct; }
	}
	/// <summary>
	/// Same thing for the RVTEquatableStruct.
	/// </summary>
	public class GenericRVTEquatableReferencedStructClass : ReferencedValueType<RVTEquatableStruct>
	{
		/// <summary>
		/// Sets the default - <c>false</c>.
		/// </summary>
		public GenericRVTEquatableReferencedStructClass()
			: base()
		{
			RVTEquatableStruct tempValue = new RVTEquatableStruct();
			Value = tempValue;
		}
	}
	/// <summary>
	/// This struct holds two integers to demonstrate the use of structures inside
	/// an RVT. Note this is the C# version as distinct from the CIL version.
	/// </summary>
	public struct CSharpSimpleStruct
	{
		#region Struct Fields
		/// <summary>
		/// This is the first integer.
		/// </summary>
		public System.Int32 FirstInt32;
		/// <summary>
		/// This is the second integer.
		/// </summary>
		public System.Int32 SecondInt32;
		#endregion // Struct Fields
		#region Constructors
		/// <summary>
		/// Simple constructor just sets the values. 
		/// </summary>
		/// <param name="firstInt32">First int.</param>
		/// <param name="secondInt32">Second int.</param>
		public CSharpSimpleStruct(System.Int32 firstInt32, System.Int32 secondInt32)
		{
			FirstInt32 = firstInt32;
			SecondInt32 = secondInt32;
		}
		#endregion // Constructors
	}
	/// <summary>
	/// This struct holds two integers to demonstrate the use of structures inside
	/// an RVT. It implements IEquatable&lt;RVTEquatableStruct&gt;. A struct could
	/// also implement IComparable&lt;T&gt;"/> if it made sense.
	/// </summary>
	public struct RVTEquatableStruct : IEquatable<RVTEquatableStruct>
	{
		#region Struct Fields
		// Just a couple integers.
		/// <summary> First one.  </summary>
		public System.Int32 FirstInt32;
		/// <summary> Second one. </summary>
		public System.Int32 SecondInt32;
		#endregion // Struct Fields
		#region IEquatable implementation
		public System.Boolean Equals(RVTEquatableStruct value)
		{
			if((FirstInt32 == value.FirstInt32) && (SecondInt32 == value.SecondInt32)) return true;
			return false;
		}
		#endregion // IEquatable implementation
	}

	#region A Simple RVT Wrapping An Int32
	/// <summary>
	/// This class implements a simple reference Type that wraps a <see cref="System.Int32"/>.
	/// This class demonstrates how to access a private field on a struct with reflection.
	/// </summary>
	public class TestReflectionAccessInt32RVT : ReferencedValueType<System.Int32>
	{
		/// <summary>
		/// Sets the default - <c>0</c>. 
		/// </summary>
		public TestReflectionAccessInt32RVT()
			: base() { }

		/// <summary>
		/// Constructor stuffs in an Int32 value.
		/// </summary>
		/// <param name="int32Value">
		/// The value that the class should contain.
		/// </param>
		public TestReflectionAccessInt32RVT(System.Int32 int32Value) : base(int32Value) { }

		/// <summary>
		/// This is a little demonstration method that shows how we (almost) can get
		/// away with writing to the inside of a box directly without boxing.
		/// </summary>
		public Int32 InnerValue
		{
			set
			{
				// This is the name of the internal field on System.Int32. Obviously
				// this would be a static class-level variable in the real world.
				String fieldName = "m_value";
				// We'll need to use private binding for this.
				BindingFlags bindingFlags
					= ManufacturingUtils.PublicOrPrivateInstanceBindingFlags;
				// Grab the field with no special attribute filtering.
				FieldInfo Int32Accessor = TypeHandlingUtils.GetFieldInfo(
					typeof(Int32), fieldName, null, null, bindingFlags, true);
				// First set the unboxed value, because we are sneaking behind the
				// BoxedValue set accessor's back to do this - remember this is only
				// a demonstration!!.
				m_UValueType = value;
				// We are using reflection to set something inside the "box" - in this case,
				// BoxedValue.
				Int32Accessor.SetValue(BoxedValue, value, bindingFlags, null, null);
				// Gosh Darn it! There's that pesky boxing of "value", again, in the
				// SetValue call (Reflection needs to do run-time Type discovery on value).
			}
		}
		/// <summary>
		/// This is a little demonstration method that shows how we CAN get
		/// away with writing to the inside of a box directly without boxing, since
		/// Property has Type <see cref="System.Object"/>
		/// </summary>
		/// <remarks>
		/// Note that this method is now completly UN-TYPE-SAFE, since we are relying on
		/// the client to have passed a boxed version of Int32.
		/// </remarks>
		public System.ValueType BoxedInnerValue
		{
			set
			{
				// This is the name of the internal field on System.Int32. Obviously
				// this would be a static class-level variable in the real world.
				String fieldName = "m_value";
				// We'll need to use private binding for this.
				BindingFlags bindingFlags
					= ManufacturingUtils.PublicOrPrivateInstanceBindingFlags;
				// Grab the field with no special attribute filtering.
				FieldInfo Int32Accessor = TypeHandlingUtils.GetFieldInfo(
					typeof(Int32), fieldName, null, null, bindingFlags, true);
				// First set the unboxed value, because we are sneaking behind the
				// BoxedValue set accessor's back to do this - remember this is only
				// a demonstration!!.
				m_UValueType = (Int32)value;
				// We are using reflection to set something inside the "box" - in this case,
				// BoxedValue.
				Int32Accessor.SetValue(BoxedValue, value, bindingFlags, null, null);
				// Hey, no boxing this time!! But who wants to pass a boxed value in?
				// - that defeats the whole purpose, doesn't it??
			}
		}
	}
	#endregion // A Simple RVT Wrapping An Int32
	#region A Simple RVT Wrapping A Structure Containing An Int32 And Implementing IValueProp
	/// <summary>
	/// This class demonstrates how to use the specialized constructor on
	/// <see cref="ReferencedValueType&lt;UValueType&gt;"/> to wrap a
	/// <see cref="IValueProp&lt;UValueType&gt;"/> closed on an Int32. This
	/// class manipulates an integer ValueType through the Generic
	/// <see cref="IReferencedValueType&lt;UValueType&gt;"/> Interface. It employs
	/// the facility of <see cref="ReferencedValueType&lt;UValueType&gt;"/> to
	/// do so by exploiting <see cref="ReferencedValueType&lt;UValueType&gt;"/>'s
	/// special feature to accept and hold a <see cref="IValueProp&lt;UValueType&gt;"/>
	/// through one of it's constructors.
	/// </summary>
	public class Int32RVTVP : ReferencedValueType<System.Int32>
	{
		/// <summary>
		/// Sets the default - all zeroes on the struct.
		/// </summary>
		public Int32RVTVP()
			: base(new TestInt32VPStruct()) { }

		/// <summary>
		/// Constructor instantiates the <see cref="Int32VPStruct"/> with an integer
		/// value and wraps it inside an RVT.
		/// </summary>
		/// <param name="int32Value">
		/// The value that the internal struct should have and return as the "Value"
		/// property of <see cref="IValueProp"/>.
		/// </param>
		public Int32RVTVP(System.Int32 int32Value)
			: base(new TestInt32VPStruct(int32Value)) { }
	}
	#endregion // A Simple RVT Wrapping A Structure Containing An Int32 And Implementing IValueProp
	#region A Simple Struct Implementing IValueProp That is Wrapped in Int32RVTVP
	/// <summary>
	/// This structure wraps an Int32 to demonstrate how indirect access can be made
	/// through the <see cref="IValueProp&lt;UValueType&gt;"/> interface without
	/// boxing. This struct must implement <see cref="IEquatable&lt;UValueType&gt;"/>,
	/// <see cref="IComparable&lt;UValueType&gt;"/> and <see cref="IComparable"/>,
	/// since the Value Type, Int32, does.
	/// </summary>
	struct TestInt32VPStruct : IValueProp<System.Int32>, IEquatable<System.Int32>,
		IComparable<System.Int32>, IComparable
	{
		Int32 m_innerInt;
		/// <summary>
		/// This constructor builds a default Int32Struct, then sets its value.
		/// </summary>
		/// <param name="int32ToSet"></param>
		public TestInt32VPStruct(System.Int32 int32ToSet)
			: this()
		{
			this.m_innerInt = int32ToSet;
		}
		public Int32 Value
		{ get { return m_innerInt; } set { m_innerInt = value; } }
		#region Equality and Comparison Interface Implementations
		///////////////////////////////////////////////////////////////////////
		// In this section we implement the required interfaces as implicit
		// interfaces, since we want to avoid boxing the Int32. Generally we
		// want to avoid boxing any value Type, since that's why IValueProp was
		// designed in the first place :-)
		///////////////////////////////////////////////////////////////////////
		/// <summary>
		/// Mandatory implementation of <see cref="IEquatable&lt;Int32&gt;"/>
		/// because Int32 does. We use implicit implementation and call Int32's
		/// version.
		/// </summary>
		/// <param name="other">
		/// The Int32 to equate.
		/// </param>
		/// <returns>
		/// <c>true if the two ints are equal.
		/// </returns>
		public bool Equals(Int32 other)
		{
			// Let Int32 decide.
			return m_innerInt.Equals(other);
		}
		/// <summary>
		/// Mandatory implementation of <see cref="IComparable&lt;Int32&gt;"/>
		/// because Int32 does.
		/// </summary>
		/// <param name="other">
		/// The other Int32 to compare.
		/// </param>
		/// <returns>
		/// Just the normal Int32 comparison.
		/// </returns>
		public Int32 CompareTo(Int32 other)
		{
			// Let Int32 decide.
			return m_innerInt.CompareTo(other);
		}
		/// <summary>
		/// Mandatory implementation of <see cref="IComparable"/>
		/// because Int32 does.
		/// </summary>
		/// <param name="other">
		/// The other Int32 to compare.
		/// </param>
		/// <returns>
		/// Int32's comparison based on its own version.
		/// </returns>
		public Int32 CompareTo(System.Object other)
		{
			// Just pass onto Int32.
			return m_innerInt.CompareTo(other);
		}
		#endregion // Equality and Comparison Interface Implementations
	}
	#endregion // A Simple Struct Implementing IValueProp That is Wrapped in Int32RVTVP
	#region // A Class Demonstrating unmanaged Pointer Access
	/// <summary>
	/// This class defers to an internal class wrapping the RVTStruct
	/// so that unsafe methods may be employed to read/write the struct
	/// "inside the box".
	/// </summary>
	public class DeferredGenericRVTReferencedStructClass : ReferencedValueType<CSharpSimpleStruct>,
		IEquatable<DeferredGenericRVTReferencedStructClass>
	{
		RVTStructClass m_rVTStructClass;
		/// <summary>
		/// Sets the default - <c>false</c>.
		/// </summary>
		public DeferredGenericRVTReferencedStructClass()
			: base()
		{
			CSharpSimpleStruct tempValue = new CSharpSimpleStruct();
			Value = tempValue;
			m_rVTStructClass = new RVTStructClass(tempValue);
		}
		/// <summary>
		/// This implementation checks both Int32s.
		/// </summary>
		/// <param name="rVTRST">
		/// Example structure containing two Int32s.
		/// </param>
		/// <returns>
		/// <c>true</c> if both Int32s are the same as the other struct's.
		/// </returns>
		public System.Boolean Equals(DeferredGenericRVTReferencedStructClass rVTRST)
		{
			if(Value.FirstInt32 != rVTRST.Value.FirstInt32) return false;
			if(Value.SecondInt32 != rVTRST.Value.SecondInt32) return false;
			return true;
		}
		public void ClosedGenericTouchStruct()
		{
			CSharpSimpleStruct rvtstruct = new CSharpSimpleStruct();
			unsafe {
				CSharpSimpleStruct* p1 = &rvtstruct;
				// This doesn't work because m_UValueType is moveable.
				//			RVTStruct* p1 = &m_UValueType;
				//			int* p1 = &((RVTStruct)m_UValueType).FirstInt32;
				// This works because the Type (Int32) is known to be a non-managed Type.
				// Doesn't do anything with the BOXed value, however.
				fixed(Int32* p = &((CSharpSimpleStruct)m_UValueType).FirstInt32) { }
				// This works because all of CSharpSimpleStruct is an unmanaged Type
				// (i.e., it does not contain any unmanaged Types as fields.
				fixed(CSharpSimpleStruct* p2 = &m_rVTStructClass.m_rVTStruct) { }
				// This doesn't work because we are copying out of the BOX and the
				// result is not a HOME.
				// fixed(CSharpSimpleStruct* p3 = &((CSharpSimpleStruct) m_boxedValueType) ) { }
			}
		}
	}
	#endregion // A Class Demonstrating unmanaged Pointer Access
	#endregion // Helper and Demonstration Classes.
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

krogerma
Web Developer
United States United States
Kurt R. Matis received the B.S degree in Applied Mathemetics from Empire State College in 1981 and the PhD. degree in Electrical Engineering from Rensselaer Polytechnic Institute in 1984. He has been involved in several companies over the past 30 years, but has been most recently involved with the Macher-Plander Software Engineering Consortium, of which he is a co-founder. The Consortium is involved with education in .Net technologies and Software Quality Management topics.

Dr. Matis is a member of IEEE and the American Historical Truck Society. Kurt lives happily in Troy, NY with his beautiful wife, two beautiful daughters and his beautiful trucks.

Dr. Matis is interested in working with companies who wish assistance in porting legacy applications of all types to .Net. He can be reached at krogerma@aol.com.




You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 15 Jan 2007
Article Copyright 2007 by krogerma
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid