public static object CloneObject(object obj)
{
using (System.IO.MemoryStream memStream = new MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(null,
new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Clone));
binaryFormatter.Serialize(memStream, obj);
memStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(memStream);
}
}
Object Cloning c#.net
How to deliver multiple dlls and exe as a single file
Scenario: You are developing a dotnet appliation, For that you are adding some external dlls ex:Office.dll. Normally we need to build the application and provide the directory containing dlls and exe for the customer. How to deliver all the files as a single pack. Is it possible if so how?
Solution: Download the ilmerge.exe from the below link and install it in your machine.
http://www.microsoft.com/downloads/details.aspx?FamilyID=22914587-B4AD-4EAE-87CF-B14AE6A939B0&displaylang=en
After Building your application go to the command prompt and give the below syntax.
"path<ilmerge.exe>" /out:"($TargetDir)ExpectedApplicationname.exe" "($TargetPath)" *.dll.....
example for the arguments:
"c:\programfiles\ilmerge\ilmerge.exe" /out:"c:\myApp\SingleFile.EXE" "c:\myApp\bin\debug\myApp.exe" "c:\myApp\bin\debug\office.exe"
(OR)
You can also provide this command in the postbuild command so the singlefile will created directly when you build the application.
BackgroundWorker Control
The BackgroundWorker class allows you to run an operation on a separate, dedicated thread.
Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running( i.e it seems like white faded screen ). In such a case this background worker will helpful so the fading are avoided bcoz it was running under seperate dedicated thread.
Three events available in backgroundworker.
1.DoWork: In which we need write the business logic
2.Runworkercompleted: This event automatically fired when the bgworker completes its process
3.Progresschanged: from which we can get the percentage of completed process.
To start the bgworker use the method RunWorkerAsync()
Consider the code snippet below create a win form and select 2 button as named BgworkerButton,NormalButton add a background worker as backgroundWorker1 and test it.
string str = string.Empty;
public Form1()
{
InitializeComponent();
}
private void BgworkerButton_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10000; i++)
str = str + " bg" + i.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Completed");
}
private void NormalButton_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10000; i++)
str = str + " " + i.ToString();
MessageBox.Show("Completed");
}
Compare Generic.List vs Array
Consider that you r in a situation to use some collection object. What type you will prefer Generic.list or Array
Advantage of generic is, it is easy to handle. use the add, remove and new extension methods in .net2008 but which is efficient.
(i) for( int i = 0; i < 5000000; i++ )
{
for( int j = 0; j < Array.Length; j++ )
{
Array[j] = j;
}
}
(ii)for( int i = 0; i < 5000000; i++ )
{
for( int j = 0; j < List.Count; j++ )
{
List[j] = j;
}
}
the time for the execution is The results where 2620 for the Array and 13200 for the List in terms of nanoseconds.
The reasons are
1. it has to do with the memory not necessarily being contiguous, and so the lookup is slightly more complex
2. List generic version is actually making function calls in the generated code this will understand if u saw the IL code for the above two cases but not in case of array.
3. The List<T> class uses an array internally, and will do effectively the same operation as Array.Resize(). The only real difference between the two is that List<T> always doubles the size of the storage, so that you need to resize fewer and fewer times as the data gets larger. Of course, you could always use that strategy when using Array.Resize() as well,