Thursday, September 17, 2009

LINQPad as a .Net Snippet Code IDE

InI Posted a very simple question about "a ? b : c" expression to Stackoverflow last night. I was at Cafe with my friend discussing a case of this expression: if b and c are different types. Since we did not have Visual Studio available to test my codes out. I posted the question to Stackoverflow. Within minutes, we got several answers to confirm my guess: a Cast can be used before either one to make them the same type. The question is indeed a very simple one. However, this lead to a new discovery, at least for me, of a great IDE tool, only one EXE with 2MB size, for .Net C# snippet codes.

The tool is called as LINQPad. Its user interface is very simple. On the top are menus and tool bars. The left panel is for Database connection or database structure list. The right panel is composed of top and bottom parts: code IDE and result view. This tool supports C# & VB expression, statements, programs, and SQL. It is very easy to use. However, it is very buggy. This morning I give it a try. After I added a reference to my library and added several my namespace lists. I could not to get a simple Console out work in a new tab when I tried to show it to my work colleges. I had to restart the application and removed references and namespaces to get it back to work.

All the codes can be saved an xml file. Within the xml file, all the references, namespaces, and code snips are saved there. It is really cool. No wonder a user who answered my question recommended me to try this tool. I guess he knew I had no VIsual Studio available. We actually had a Mac computer.

The project is not an open source project. It is a closed project. The standard version is free and an advanced version with auto-completion feature is for sale. After I give it a thought for this back engine, I guess that application is not hard to create. I would use the .Net CodeCom namespace as .Net compiler engine to create a dynamic project with a template for a snippet as plug in codes, just as I posted in my previous blogs. The dynamic project could be a simple console application> if it compiles OK, then run it through Process class. The process is hidden and all the outputs can be redirected as output back to the application. The application interface parts can be done with MEF framework so that each view parts can be plugged with various UIs to support IDE code editor (such as supporting syntax color schemas for various languages), result view (grid or table layout) and other views.

With this structure, LinPad should be an open source project so that talent developers can make it much better and extendable. One person's dedication is great, but with Web world available, great resources from the world should be utilized.

Read More...

Tuesday, September 15, 2009

My Stackoverflow Reputation Points Reach to 1K

Today my reputation score at Stackoverflow reaches to 1007 points! The recent question on Parse String to Enum Type boosts my score over 1K points. I could touch this target earlier than up to today, but my main intention to use this web site is to help me to get the best and the quickest resources and answers for my programming questions or issues. I have not focused on gaining scores or badges at all. Therefore, I only spend some time to search and answer other people's questions when I have time.

The following is my statics:

  • Score: 1007
  • Badges: 15 bronze badges
    • Popular questions: 7
    • Tumbleweed: 1
    • Scholar: 1
    • Organizer: 1
    • Editor: 1
    • Commentatior: 1
    • Teacher: 1
    • Supporter: 1
    • Student: 1
  • Questions asked: 86
  • Questions answered: 34
  • Votes: 70
  • Tags: 66

With Stackoverflow, it have been my great resources to helpresolving my questions and issues. I got many great answers and explanations for my program questions. Normally, if I cannot resolve my issue within short period time, or I have concerns about my strategy, or just want to consult experts, I post my questions there. In most cases, I get answers in just less than 5 minutes. Sometimes, I have to wait longer. Soon I learned that I don't need to mark the quick response as answer right away. People compete on Stackoverflow for earning score points. However, sometimes, good ones may not be the quick ones. Just wait for the good ones.

Unfortunately, recently I cannot access Stackoverflow from my work by using my Blogger OpenID. Husky would not allow me to access to my Blogger login web page. I had to create another account (David Chu) by using Google OpenID. With that account, I have about 185 score points. I was reluctant to use that account to ask questions initially, since I preferred to use one account to accumulate scores. However, I do need to get my issues resolved during my work most of time. Therefore, recently I started to use that account more. Still if I can wait, I'll post my questions after work or early in the morning. That's the reason my reputation score points have grown slow. By the way, the total score of points of my two accounts have reached 1K more than one month ago.

Anyway, I set 1k as a target day to celebrate my reputation on Stackoverflow.

Cheers!

Read More...

Sunday, September 13, 2009

Free CodeRush Xpress Tool by DevExpress

toolsDevExpress released a free tool for .Net Visual Studio 2008 users: CodeRush Xpress. I found this out from DVRTV show 143: Mark Miller on CodeRush Xpress.

I knew this tool for long time but I have never tried the tool as I used Resharper before. With the free offering, I tried this re-factory tool right away. It is a nice tool for .Net developers. But I found that some features are not working such as Tab to Next Reference and Template snip codes for switch and for loops. Maybe there are some option settings I have not set up yet. I also realize that the tool does provide hint on the left scroll bar to indicate questionable codes such as greying out unreferenced using statements, like Resharper has (which provides hints as yellow marks).

Here are some additional links related to this tool:

Read More...

Sunday, September 06, 2009

CodeDom and Expression Calculator (2)

In order to compile a snip of codes dynamically, I need to define a template in the class of ExpressionEvaluation so that it can be used as a base. The template contains several parameters which will be replaced (such as a class name, a data type and an expression). I defined a string with parameters enclosed by {} so that those parameters can be easily replaced by dynamic values (string.Format(template, parameters...)).

Here is the template of snip codes in the class:


using System;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

public class ExpressEvaluation { // class name
  private const string _InitalizeValueCode = "private {0} _value = {1};"; // valType, valExp
  private const string _ClassName = "_CalculatorWithFormula"// 0
  private const string _MethodAnswer = "Answer"// 1
  private const string _SourceCodeTemplate = @"
using System;
public class {} {{ // {{0}} Class name
  {2}
  public {} {1}()  // {{1}} method to be called to get result
  {{
    return _value;
  }}
}}";


  public int GetAnswer()         // method to get result as type
  {
    return _value;
  }
}


The key methods in the class are BuildCodes() and GetAnswerByBuildAndRunCodes():

private static string BuildCodes(
    string valueExp,
    string varType)
{
    string initializeValueCodes = string.Format(
            _InitalizeValueCode, varType, valueExp);

    string codes = string.Format(_SourceCodeTemplate,
        _ClassName, _MethodAnswer, initializeValueCodes, varType);

    return codes;
}

private static T GetAnswserByBuildAndRunCodes<T>(
    string sourceCodes) where T : struct
{
    object value = default(T);
    CompilerResults cr = GetCompiler(sourceCodes);

    var instance = cr.CompiledAssembly.CreateInstance(_ClassName);
    Type type = instance.GetType();
    MethodInfo m = type.GetMethod(_MethodAnswer);
    value = m.Invoke(instance, null);

    return (T)value;
}

Those two methods are very straightforward. In GetAnswserByBuildAndRunCodes(), a method GetCompiler() is called to get a C# CompilerResults object in the current context:

private static CompilerResults GetCompiler(string codes)
{
    CSharpCodeProvider codeProvider = new CSharpCodeProvider();

    CompilerParameters parameters = new CompilerParameters();
    parameters.GenerateExecutable = false;
    parameters.GenerateInMemory = true;
    parameters.IncludeDebugInformation = false;

    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    {
        parameters.ReferencedAssemblies.Add(asm.Location);
    }

    return codeProvider.CompileAssemblyFromSource(parameters, codes);
}

Here is the complete source codes for download.

Read More...