public class Disposer<T> { static public void DisposeOf(ref T obj) { if (!ReferenceEquals(obj, null)) { if (obj is IDisposable) ((IDisposable)obj).Dispose(); obj = null; } } } ... // To use Disposer<MyClass>.DisposeOf(ref myObject);
July 15, 2005
Generic Dispose
Use this generic class to dispose of any reference object, whether it is IDisposable or not. I havent tried this class yet so I do not know if it will work. It is using c# Generics. I am assuming a C# generic class can have a static method.
.Net Deep Copy or Cloning Using Serialization
Heres some code I developed to make a deep copy of an object using serialisation.
public class Cloner { static public object _GenericDeepCopy(object target) { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); // Serialize the object into the stream. bf.Serialize(ms, target); //Position streem pointer back to first byte. ms.Seek(0, SeekOrigin.Begin); // Deserialize into another object. object clone = bf.Deserialize(ms); // Release memory. ms.Close(); return clone; } static public XXX _XXXDeepCopy(XXX dcLookUpTable) { MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); // Serialize the object into the stream. bf.Serialize(ms, dcLookUpTable); //Position streem pointer back to first byte. ms.Seek(0, SeekOrigin.Begin); // Deserialize into another object. XXX cloneObject = (XXX)bf.Deserialize(ms); // Release memory. ms.Close(); return cloneObject; } }
July 14, 2005
Using Flag based Enumerated Types
class EnumSample
{
#region ControlData
/// <summary>
/// Flag based enumerated type
/// </summary>
[Flags]
private enum ControlData
{
/// <summary>
/// No data of any kind available
/// </summary>
None = 0,
/// <summary>
/// Hist graph data is available
/// </summary>
Hist = 1,
/// <summary>
/// Curve graph data is available
/// </summary>
Curve = 2,
/// <summary>
/// XXX data is available
/// </summary>
XXX = 4
}
/// <summary>
/// Record what data is available for the viewer
/// </summary>
private ControlData m_ControlData = ControlData.None;
/// <summary>
/// Is the given Data Available
/// </summary>
/// <param name="datatype">Type of data required</param>
/// <returns></returns>
public bool IsDataAvailable(ControlData datatype)
{
return ((m_ControlData & datatype) != ControlData.None);
}
/// <summary>
/// Add the given Data Available
/// </summary>
/// <param name="datatype">Type of data now available</param>
public void AddDataAvailable(ControlData value)
{
m_ControlData |= value;
}
#endregion ControlData
}
Drawing Rounded Rectangles
Based on some code from here
I tidied it up a bit.
using System.Drawing; using System.Drawing.Drawing2D; class GraphicsHelper { #region Drawing Rounded Rectangles /// <summary> /// Draw a rounded rectangle with the specified radius of the /// rounded corners /// </summary> /// <param name="pen"></param> /// <param name="left"></param> /// <param name="top"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="radius">radius of the rounded corners</param> public static void _DrawRoundedRectangle(Graphics graphics, Pen pen, int left, int top, int width, int height, int radius) { using (GraphicsPath gp = new GraphicsPath()) { _AddRoundedRectPath(gp, left, top, width, height, radius); gp.CloseFigure(); graphics.DrawPath(pen, gp); } } /// <summary> /// Draw a filled rounded rectangle with the specified radius /// of the rounded corners /// </summary> /// <param name="brush"></param> /// <param name="left"></param> /// <param name="top"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="radius">radius of the rounded corners</param> public static void FillRoundedRectangle(Graphics graphics, Brush brush, int left, int top, int width, int height, int radius) { using (GraphicsPath gp = new GraphicsPath()) { _AddRoundedRectPath(gp, left, top, width, height, radius); gp.CloseFigure(); graphics.FillPath(brush, gp); } } /// <summary> /// Add a rounded rectangle to the given graphics path with /// the specified radius of the rounded corners /// </summary> /// <param name="gp"></param> /// <param name="left"></param> /// <param name="top"></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="radius">radius of the rounded corners</param> private static void _AddRoundedRectPath(GraphicsPath gp, int left, int top, int width, int height, int radius) { // Top line gp.AddLine(left+radius, top, left+width-radius, top); // Top right arc gp.AddArc(left+width-radius, top, radius, radius, 270, 90); // Right hand side line gp.AddLine(left+width, top+radius, left+ width, top+height-radius); // Bottom right arc gp.AddArc(left+width-radius, top+height-radius, radius, radius, 0, 90); // Bottom line gp.AddLine(left+width-radius, top+height, left+radius, top+height); // Bottom left arc gp.AddArc(left, top+height-radius, radius, radius, 90, 90); // Left hand side line gp.AddLine(left, top+height-radius, left, top+radius); // Top left arc gp.AddArc(left, top, radius, radius, 180, 90); } #endregion Drawing Rounded Rectangles }
Subscribe to:
Posts (Atom)