1. Caller
Information
Caller
Information can help us in tracing, debugging and creating diagnose tools. This
is the new core feature in the .net Framework 4.5. It will help us to avoid
duplicate codes which are generally invoked in many methods for same purpose,
such as logging and tracing. This feature is implemented using new compiler
services in 4.5 and helps us to get caller method's name, its line number and
file path. This feature helps us in logging.
We could get the
below information of caller method:
Allows you
to obtain the full path of the source file that contains the caller. This is
the file path at the time of compile.
Allows you
to obtain the line number in the source file at which the method is called.
You apply
the CallerMemberName attribute to an optional parameter that has a default
value. You must specify an explicit default value for the optional parameter.
You can't apply this attribute to parameters that aren't specified as optional.
How to use:
1.
Open the
new Console Application using VS Studio 2012
2.
Add the
Name space System.Runtime.CompilerServices
3.
See
the below code regarding the use of CallerMemberName,
CallerLineNumber and CallerFilePath attributes part of System.Runtime.CompilerServices
namespace to get details of caller's method as shown below:
using System;
using System.Runtime.CompilerServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
InsertLog("Main method error message.");
TestMethod();
Console.ReadKey();
}
private static void TestMethod()
{
InsertLog("TestMethod failed.");
}
private static void InsertLog(string errorMessage, [CallerMemberName] string memberName = ""
, [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
Console.WriteLine("Error message : " + errorMessage + "\r\nCalled From: " + memberName + " Method \r\nLine Number: " + sourceLineNumber.ToString() + "\r\nFile Path: " + sourceFilePath);
Console.WriteLine("\n------------------------------------");
}
}
}
|
2. Zip Compression
Earlier .net framework does not support for the Zip
Compression. The developers has to use the third party utilities. 4.5 .net Framework version supports for the
zip compression and extraction which is supported by most of all the operating
systems. Zip compression
improvements to reduce the size of a compressed file
The System.IO.Compression
namespace contains classes that provide basic compression and decompression
services for streams.
Below example
shows how to create and extract a zip archive by using the ZipFile class. It compresses the contents of a folder
into a zip archive, and then extracts that content to a new folder. To use the ZipFile class, you must reference the System.IO.Compression.FileSystem
assembly in your project.
using System;
using
System.IO.Compression;
namespace
ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string startPath = @"D:\Junk\source";
string zipPath = @"d:\junk\result.zip";
string extractPath = @"d:\junk\extract";
ZipFile.CreateFromDirectory(startPath,
zipPath);
ZipFile.ExtractToDirectory(zipPath,
extractPath);
Console.ReadKey();
}
}
}
|
No comments:
Post a Comment