Friday, January 16, 2009

AppDomain : Listing Assemblies within the Current AppDomain and Creating an new AppDomain (Code Snippet)

using System.Reflection;

private void Form1_Load(object sender, EventArgs e)
{
AppDomain dom = AppDomain.CurrentDomain;

Assembly[] loadedAssembly = dom.GetAssemblies();
foreach (Assembly a in loadedAssembly)
listBox1.Items.Add(a.GetName().Name );
}

private void btnCreateAppDomain_Click(object sender, EventArgs e)
{
AppDomain aDo = AppDomain.CreateDomain("Shalvin");

Assembly[] loadedAssembly = aDo.GetAssemblies();
foreach (Assembly a in loadedAssembly)
listBox2.Items.Add(a.GetName().Name);
}










Related Blogs
Reflection in .Net
.Net Intermediate Language

.Net Remoting Walkthrough

Remoting is the process of two pieces of software communicating across application domains.
Start a Class Library call it say ShalvinObject. Create a class that inherits from MarshalByRefObject.

namespace ShalvinObject
{
class RemoteObject : MarshalByRefObject
{
public int Mul(int i, int j)
{
return i * j;
}
}

Now we are going to host the service. This can be done with Console, Windows Forms or Web Service.
Letsuse Windows Forms.
Add to a reference to the previously created dll, ie. ShalvinObject.dll and System.Runtime.Remoting.


using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

private void Form1_Load(object sender, EventArgs e)
{
TcpChannel c = new TcpChannel(900);
ChannelServices.RegisterChannel(c);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ShalvinObject.RemoteObject), "Shalvin", WellKnownObjectMode.Singleton);
}

Now for testing the service lets create another Windows Application. Here too set references to ShalvinObject.dll and System.Runtime.Remoting.


using ShalvinObject;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;


private void Form1_Load(object sender, EventArgs e)
{
TcpChannel c = new TcpChannel();
ChannelServices.RegisterChannel(c);
object obj = Activator.GetObject(typeof(ShalvinObject.RemoteObject), "tcp://localhost:900/Shalvin");
RemoteObject ro = (RemoteObject)obj;
int intRes = ro.Mul(56, 45);
MessageBox.Show(intRes.ToString());
}


Related Blog
Currency Conversion Web Service with Asp .Net

Creating and Consuming Class Library in C#, Object Initializer

Monday, January 12, 2009

Hashtable and DictionaryEntry

HashTable Class of System.Collections namespace represents a collection of key/value pairs that are organized based on the hash code of the key.

using System.Collections;

Hashtable ht = new Hashtable();
private void Form1_Load(object sender, EventArgs e)
{
ht.Add("Am", "Ambily");
ht.Add("Di", "Divya");
foreach (DictionaryEntry de in ht)
{
listBox1.Items.Add(de.Key);
listBox2.Items.Add(de.Value);
}
}

private void button1_Click(object sender, EventArgs e)
{
ht.Add(textBox1.Text, textBox2.Text);
ShowHashTable();
}

private void ShowHashTable()
{
listBox1.Items.Clear();
listBox2.Items.Clear();
foreach (DictionaryEntry de in ht)
{
listBox1.Items.Add(de.Key);
listBox2.Items.Add(de.Value);

}


VB .Net
















Dim ht As New Hashtable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ht.Add("Am", "Ambily")
ht.Add("Di", "Divya")
ShowHT()
End Sub



Private Sub ShowHT()
ListBox1.Items.Clear()
ListBox2.Items.Clear()
For Each de As DictionaryEntry In ht
ListBox1.Items.Add(de.Key)
ListBox2.Items.Add(de.Value)
Next
End Sub



Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
ht.Add(txtKey.Text, txtValue.Text)
ShowHT()
End Sub


IDE Generated Code



Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.ListBox2 = New System.Windows.Forms.ListBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtKey = New System.Windows.Forms.TextBox
Me.txtValue = New System.Windows.Forms.TextBox
Me.btnAdd = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.Location = New System.Drawing.Point(23, 41)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(120, 95)
Me.ListBox1.TabIndex = 0
'
'ListBox2
'
Me.ListBox2.FormattingEnabled = True
Me.ListBox2.Location = New System.Drawing.Point(160, 41)
Me.ListBox2.Name = "ListBox2"
Me.ListBox2.Size = New System.Drawing.Size(120, 95)
Me.ListBox2.TabIndex = 1
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(37, 160)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(25, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Key"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(37, 190)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(34, 13)
Me.Label2.TabIndex = 3
Me.Label2.Text = "Value"
'
'txtKey
'
Me.txtKey.Location = New System.Drawing.Point(82, 153)
Me.txtKey.Name = "txtKey"
Me.txtKey.Size = New System.Drawing.Size(100, 20)
Me.txtKey.TabIndex = 4
'
'txtValue
'
Me.txtValue.Location = New System.Drawing.Point(82, 183)
Me.txtValue.Name = "txtValue"
Me.txtValue.Size = New System.Drawing.Size(100, 20)
Me.txtValue.TabIndex = 5
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(82, 220)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(75, 23)
Me.btnAdd.TabIndex = 6
Me.btnAdd.Text = "&Add"
Me.btnAdd.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.btnAdd)
Me.Controls.Add(Me.txtValue)
Me.Controls.Add(Me.txtKey)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ListBox2)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtKey As System.Windows.Forms.TextBox
Friend WithEvents txtValue As System.Windows.Forms.TextBox
Friend WithEvents btnAdd As System.Windows.Forms.Button


Related Blog
ReadOnlyCollectionBase Class
Generics and Collection Initializer in .Net

Thursday, January 8, 2009

ReadOnlyCollectionBase Class

Provides the abstract base class for a strongly typed non-generic read-only collection.This class makes the underlying collection available through the InnerList property.
Since ReadOnlyCollectionBase implements IEnumerable Interface you can iterate through the collection.

//class
using System.Collections;
class ShalvinBlogs : ReadOnlyCollectionBase

{
public void Add(String value)
{
InnerList.Add(value);
}
public string this[int index]

{
get
{
return (string)InnerList[index];
}
}
}

//Forms
ShalvinBlogs sb = new ShalvinBlogs();

private void Form1_Load(object sender, EventArgs e)
{
sb.Add("Shalvinpd.blogspot.com");
sb.Add("DotNetShalvin.blogspot.com");
foreach (string str in sb)
listBox1.Items.Add(str);
}
private void button1_Click(object sender, EventArgs e)

{
textBox1.Text = sb[0];
}

Related Blogs
Generics and Collection Initializer in .Net
Hashtable and DictionaryEntry