December 2004 - Posts

C# Command Line

Well i came across a snippet complier .. but was so comfortable with command prompt and needed some this that would just run that one line of code.. the i read about Monad. Well why not just use the framework to execute that one line and kinda ended up writing this small piece for doing stuff like

C# Command Line Tool v1.0
Note: Mail your comments to
sajay.antony (at the rate) gmail.com

>Console.WriteLine("Hello World");
Hello World
>for(int i=10;i<16;i++) Console.WriteLine("Hex value = {0:x}",i);
Hex value = a
Hex value = b
Hex value = c
Hex value = d
Hex value = e
Hex value = f
>quit

 

using System;

using System.Reflection;

using System.Windows.Forms;

using System.Text;

 

namespace CsCmd

{

    public class ExecutorBase

    {

        public ExecutorBase()

        {

        }

        public virtual void eval()

        {

           

        }

    }

}

 

 

namespace CsCmd

{

 public class ExpressionParser

 {

  ExecutorBase myobj = null;

  public ExpressionParser()

  {

  }

  public bool init(string expr)

  {

   Microsoft.CSharp.CSharpCodeProvider cp

                                = new Microsoft.CSharp.CSharpCodeProvider();

   System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();

   System.CodeDom.Compiler.CompilerParameters cParam

                         = new System.CodeDom.Compiler.CompilerParameters();

   cParam.GenerateInMemory = true;

   cParam.GenerateExecutable = false;

   cParam.ReferencedAssemblies.Add("system.dll");

   cParam.ReferencedAssemblies.Add("system.windows.forms.dll");

   cParam.ReferencedAssemblies.Add("CsCmd.exe");

 

  

   string src = @"using System;

        using System.Windows.Forms;

     

       class myclass:CsCmd.ExecutorBase

       {{      

         public override void eval()

            {{

                {0}

            }}

        }}";

 

   src = string.Format(src, expr);

   System.CodeDom.Compiler.CompilerResults cResults

                                   = ic.CompileAssemblyFromSource(cParam,src);

   foreach (System.CodeDom.Compiler.CompilerError ce in cResults.Errors)

    Console.WriteLine(ce.ErrorText);

 

   if (cResults.Errors.Count == 0 && cResults.CompiledAssembly != null)

   {

    Type ObjType = cResults.CompiledAssembly.GetType("myclass");

    try

    {

     if (ObjType != null)

     {

      myobj = (ExecutorBase)Activator.CreateInstance(ObjType);

     }

    }

    catch (Exception ex)

    {

     Console.WriteLine(ex.Message);

    }

    return true;

   }

   else

    return false;

  }

 

   public void eval()

     {

         if (myobj != null)

            myobj.eval();

     }

 }

 

 

    public class MainApp

    {

 

        const string AppVersion = "C# Command Line Tool v1.0 beta \nAuthor : Sajay Antony \nNote: Mail your comments to sajay.antony@gmail.com \n";

        public static void Main()

        {

            ExpressionParser mp = new ExpressionParser();

 

            Console.WriteLine(AppVersion);

 

            string input =string.Empty;

            //Console.TreatControlCAsInput = true;

            Console.Write(">");

            input = NextToken();

            do

            {

                switch (Parse(input))

                {

                    case CommandType.Statement:

                            mp.init(input);

                            mp.eval();

                        break;

                }

                Console.Write(">");

                input = NextToken();

            }

            while (input != "quit");

        }

 

        const string WriteLine = "Console.WriteLine({0});";

        public static string NextToken()

        {

           

            string input=string.Empty;

            input += Console.ReadLine();

            return input;

        }

 

 

        public enum CommandType

        {

            Statement,

            ToolCommand

        }

 

        public static CommandType Parse(string input)

        {

            CommandType returnVal;

            switch (input)

            {

                case "help":

                     Console.WriteLine("Type quit to exit program");

                     returnVal = CommandType.ToolCommand;

                     break;

                case "quit":

                       returnVal = CommandType.ToolCommand;

                    break;

                default:

                    returnVal = CommandType.Statement;

                    break;

 

            }

             return returnVal;

         }

 

 

    }

}

 

the skeleton was from code project by Vlad Tepes

with 2 Comments

Hello World in MSIL

I just had to put this down. By the way its on framework 2.0

// Metadata version: v2.0.40607
.assembly extern mscorlib
{
   .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
   .ver 2:0:3600:0
}

.assembly First{}
.module first.exe

// =============== CLASS MEMBERS DECLARATION ================

.class private auto ansi beforefieldinit ClassA
      
extends
[mscorlib]System.Object
{
     .method
public hidebysig static void Main() cil managed
    {
      
.
entrypoint
          
ldstr "Hello World"
           
call void [mscorlib]System.Console::WriteLine(string)
          
nop
          
ret
   
}
// end of method
} // end of class

// =============== CLASS MEMBERS DECLARATION ================

with 1 Comments

Sealed Classes, stucts and the C# Compiler

Check the code below.
What kinda of messages would the compiler produce. if any error would occur.?
 
//-------------------------------------------------------
struct MyStruct
{
 int MyInt;
}
 

sealed class MySealedClass
{
 
}
 
//Error : inherting from struct
class MyClass:MyStruct
{
 
}

//Error : inherting from sealed class
class MySealedChild:MySealedClass
{
 
}
//-------------------------------------------------------
 
 
incase your still wondering .. both produce CS0509.. check it out below.. funny how structs work just like as sealed classes but dont be misled they are value types
 
 
Compiler output
 
MyClass.cs(13,7): error CS0509: 'MyClass' : cannot inherit from sealed class 'MyStruct'
MyClass.cs(1,8): (Location of symbol related to previous error)
MyClass.cs(18,7): error CS0509: 'MySealedChild' : cannot inherit from sealed class 'MySealedClass'
MyClass.cs(7,14): (Location of symbol related to previous error)
 
 
with 0 Comments

MSIL - .class and why not .struct

Even wondered how the struct got compiled to IL, if you have and didnt know then check this out. It comes using a  .class and somehow makes the impression that its a struct. For c++ guys i still wonder where this definition of stuct would fit in. Yes i have to make a note that its a value type and there ends the difference, dont know if im missing out on something here

//Struct declaration
struct MyStruct
{
 int MyInt;
}

//Generated partial MSIL
.class private sequential ansi sealed beforefieldinit MyStruct
           extends
[mscorlib]System.ValueType
{
      
.field private int32
MyInt
}

I havent yet checked on all words inside the IL spec.. so do let me know if you have any feed back on this.

with 0 Comments

COR System Directory - Framework Installation From MSCOREE.dll

If you ever wanted the to find the frameworks COR system directory. Then the mscoree.dll has the method for just that.

 

using System.Text;
using System.Runtime.InteropServices;

public class CORSystemDir{

   [DllImport("mscoree.dll")]
   private static extern int GetCORSystemDirectory(
      [MarshalAs(UnmanagedType.LPWStr)]StringBuilder pbuffer,
       int cchBuffer, ref int dwlength);
    
   public
static void Main(){

             int
MAX_PATH = 260;
             StringBuilder sb = new StringBuilder(MAX_PATH);
             GetCORSystemDirectory(sb, MAX_PATH, ref MAX_PATH);
             System.Console.WriteLine(sb.ToString());
   }

}

with 0 Comments

DevPartner Studio 7.2 - .NET naming conventions

Developers can ensure that their method and variable names use the naming conventions as put forth in the Microsoft .NET Framework General Reference Naming Guidelines document. DevPartner code review will automatically check for appropriate names and flag anything that doesn't fit within those naming conventions.

http://www.compuware.com/products/devpartner/1500_ENG_HTML.htm

with 0 Comments

XML Resource files from SQL 2k