Draw icon with the alpha channel enabled

Here’s how to draw an icon in a control utilizing the icon’s built-in alpha channel.

private void cmdEmail_Paint(object sender, 
  System.Windows.Forms.PaintEventArgs e)
{
  System.Drawing.Icon icon;
  icon = new Icon(
    GetType().Module.Assembly.GetManifestResourceStream(
      "ActiveSolution.Lernia.SFI.WinClient.Images.mail.ico"),
    16, 16); 
  e.Graphics.DrawIcon(icon,0,0);
}

The name of the icon resource is easily found using ildasm. Double-click on the “M A N I F E S T” node and look for the icon in the window that opens.

List web applications in IIS and their ASP.NET versions

C:\>aspnet_regiis.exe  -lk
W3SVC/  1.1.4322.0
W3SVC/1/ROOT/Butler/UtbildareSI/        2.0.50727.42
W3SVC/1/ROOT/Butler/SecuritySI/ 2.0.50727.42
W3SVC/1/ROOT/Butler/Public.SecuritySI/  2.0.50727.42
W3SVC/1/ROOT/Butler/PersonregisterSI/   2.0.50727.42
W3SVC/1/ROOT/Butler/CommonEntity/       2.0.50727.42
W3SVC/1/ROOT/Reports$SQL2005/   2.0.50727.42
W3SVC/1/ROOT/ReportServer$SQL2005/      2.0.50727.42

C:\>

SoapHeaders to Web Services in .Net

SoapHeaders can be used to send “extra” and optional parameters to web service methods and is a technique which can be extremely useful. See the following example:

Server-side (MyService.asmx):

public class MyService : System.Web.Services.WebService
{
   public class AddDateHeader : SoapHeader
   {
      public bool AddDate;
   }
   public AddDateHeader myheader;
   ...

   [WebMethod]
   [SoapHeader("myheader")]
   public string HelloWorld()
   {
      if (myheader != null && myheader.AddDate == true)
         return "Hello World " + DateTime.Now.ToString();
      else
         return "Hello World";
   }

Client-side:

localhost.MyService si = new localhost.MyService();
si.AddDateHeaderValue = new localhost.AddDateHeader();
si.AddDateHeaderValue.AddDate = true;
MessageBox.Show(si.HelloWorld());

Furthermore, these headers can be accessed in SoapExtension components. For example, there might be a SoapHeader that controls the transport of data.

Example:

Public Overrides Sub ProcessMessage(ByVal message As SoapMessage)
    Dim compress As Boolean = False
    Dim header As SoapHeader
    For Each header In message.Headers
        If header.GetType().Name = "Compress" Then
            compress = True
        End If
    Next
    ...

Sometimes it’s useful to have the header sent in both directions (both to and from the service). In this case, apply the attribute to the web method with the Direction attribute:

[SoapHeader("myheader", Direction = SoapHeaderDirection.InOut)]
public string HelloWorld()
{
    ...

Initializing arrays

Here’s a handy way of creating an array and pass it as a parameter for a function call:

MyFunc(new Kommentar[] {kom1, kom2} );

No need for clumsy temporary variables etc. Neat, isn’t it?

Here’s another variation:

VeckoFacit[] facitarr;
facitarr = new VeckoFacit[]
{
    new VeckoFacit(...),
    new VeckoFacit(...),
    new VeckoFacit(...)
};

Initializing a multidimensional array:

int[,] intArray = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
int[,] intArray = new int[,] { {1, 2}, {3, 4}, {5, 6} };

Cloning an object using serialization

Here’s an easy way of cloning an object (deep copy):

public T CloneObject<T>(T obj) where T:class
{
    if (obj== null)
        return null;

    MemoryStream memstream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(memstream, obj);
    memstream.Seek(0, SeekOrigin.Begin);
    T clone = (T)formatter.Deserialize(memstream);
    memstream.Close();
    return clone;
}

The requirement is of course that the object is serializable and marked with the [Serializable] attribute.