Compile and Run .NET program with just .NET framework on your system.

Compile and Run .NET program with just .NET framework on your system.

Did you know that every .NET framework installation also installs VB & C# compiler.

Using these compilers you can compile and run C# or VB.NET program on machine with just .NET framework installed. This can be useful on system where installing full visual studio is not an option.

Here is how to do it

If .NET framework installed on you system, C:\Windows\Microsoft.NET\Framework directory  should list all the versions. Here is what is on my machine.

image

Once you find required .NET version, directory content should list csc.exe and vbs.exe compiler executable. As shown below.

image

Now use C# or VB compiler to compile the program as follows. For example use .NET 2.0 compiler to generate .exe c:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe Program.cs

image

Advertisements

C# 5 Callerinfo Attribute

As application developer we log information related to execution (information or error) to log files. This information is helpful for tracing, debugging, and creating diagnostic tools. If the log message has file name, line number and function name it is easy to narrow down the issue file and function.

In C/C++ developer can use  __FILE__ , __LINE__ and __FUNCTION__ macros to print file name, function name and line information along with the log message.

Following code c++ code illustrate the usage

// logger function
void LogMessage(std::string caller ,std::string LogMessage)
{
	std::cout<<__FILE__<<":"<<__LINE__<<" - "<<caller<<" "<<LogMessage<<std::endl;
}
// main function
int main(int argc, char* argv[])
{
	LogMessage (__FUNCTION__,"Starting Application");
	return 0;
}

If you are .Net developer  prior to .Net 4.0  you would have used System.Diagnostics.StackFrameclass to get this information .

public class Program
{
	public static void LogMessage(String message)
	{
		// get access to caller stackframe
		StackFrame frame = new System.Diagnostics.StackTrace(true).GetFrame(1);
		String file = frame.GetFileName();
		int line = frame.GetFileLineNumber();
		String member = frame.GetMethod().Name;
		
		var s = String.Format("{0}:{1} - {2}: {3}", file, line, member, message);
		Console.WriteLine(s);
	}
	static void Main(string[] args)
	{
		LogMessage("Starting the application ...");
	}
}

In C# 5 and VB.Net 11 there is easier way of getting this information using CallerInfo attribute.By using Caller Info attributes, you can obtain information about the caller to a method.

You can obtain file path of the source code, the line number in the source code, and the member name of the caller.

Here is the sample which uses C#5 feature ( available in .Net 4.5 )

public class Program
{
	public static void LogMessage(    
	string message,
	[CallerFilePath] string file = "",
	[CallerLineNumber] int line = 0,
	[CallerMemberName] string member = "")
	{
		var s = string.Format("{0}:{1} - {2}: {3}", file, line, member, message);
		Console.WriteLine(s);
	}
	static void Main(string[] args)
	{
		LogMessage("Starting the application ...");
	}
}

More information about Callinfo attribute can be found here
1. MSDN

.Net Reflector alternatives

If you are in the .net programming world , you might have used free tool .Net Reflector, tool which allows you browse analyze and decompile .net assembly. It can be used to inspect, navigate, search, analyze, and browse the contents of a
.NET component such as an assembly and translates the binary information (IL) to a human-readable form.

Unfortunately this tool no longer available as free tool . Red gate software which maker this tool announced that  they will  be charging license fee to use this software.You can use the existing version without license fee. If you want to upgrade you have to buy.

If you are .Net Reflector user , still wants free tool to analyze assembly , here are two alternatives. Both products listed below are still beta