Archive for October, 2008

Google Search API for .NET 0.2 beta and Google Translate API for .NET 0.2 beta released

October 8, 2008

Update4

No more default http referrer. User must set it as a parameter of constructor.

Delete old Translator and Searcher classes.

Updated comments for better document.

Update3

Change all API Using string parameter instead of enum parameter.

Add new Enumeration class and enumerations for API parameters.

The API changed a lot this time. Please let me know whether you like these changes or not.

Update2

All codes are refacted (StyleCop format).

Update the APIs to support latest Google APIs.

Now can customize referrer, accept language, API key and timeout times.

Update1

Update the supported language list for Google translate api.

Set “http://code.google.com/p/google-api-for-dotnet/” as the default http referrer. (Now you cannot set your own http referrer)

No more v0.2, new features will come with v0.3.

Google APIs for .NET

Provides simple, unofficial, .NET Framework APIs for using Google Ajax RestFULL APIs (Search API, Language API etc.)

How LAZY I am. I should release nearly a month ago.

Indeed, 99+% of source have been finished when I release the Google Search API for .NET 0.1 and Google Translate API for .NET 0.1.1.

I just wait some bugs from the last versioin, so I can do some change.

I do fixed one bug, only one.

Ok, see what’s new.

The new versions are nearly no different for using, but the foundation is changed.

Replace Json.NET 2.0 by Microsoft official WCF.

And implement the HttpUtility, so not need the System.Web any more.

It now support .NET 3.5 sp1 Client Profile.

You can download here:

GoogleSearchAPI_0.3.1.zip

GoogleTranslateAPI_0.3.1.zip

More in formations, please visit the project’s site:

http://code.google.com/p/google-api-for-dotnet/

Advertisements

How to excute a LINQ Expression

October 8, 2008

Linq for .NET 3.5 is a very interesting new feature. It provide you some abilities that compiler has.

Thanks for Expression (under System.Linq.Expressions namespace), we can keep the expression tree but not compiled MSIL code.

When you use Linq like this,


var count = from dataItem in data where dataItem.Value > 100 select data.Value2;

You do not need to know the expression tree (You even do not need to add the using of System.Linq.Expressions), .NET compiler and runtime to all the things for you.

That good most of the times.

Sometimes, you need to do something by yourself. Just like my current project, I need get the result directly from a expression tree.


object GetResult(Expression m);

T GetResult<T>(Expression m);

The only one I can find to do this directly is Excute method in IQueryProvider interface.


public interface IQueryProvider
{
Object Excute(Expression expression);

TResult Excute
(Expression expression);
}

Looks great, but useless. It use an interface, and I cannot find a impliment in the framework.

Ok, it’s time to show you the truth. No more words. Just the code below.


public static object GetResult(this Expression m)
{
    if (m == null)
        throw new ArgumentException("m");
    Type type = typeof (Func<>).MakeGenericType(m.Type);
    LambdaExpression lambda = Expression.Lambda(type, m);
    Delegate callback = lambda.Compile();
    object result = callback.DynamicInvoke();
    return result;
}

public static T GetResult<T>(this Expression m)
{
    if (m == null)
        throw new ArgumentNullException("m");
    Expression<Func<T>> lambda = Expression.Lambda<Func<T>>(m);
    Func<T> callback = lambda.Compile();
    T result = callback();
    return result;
}

Compile to MSIL at runtime, isn’t it cool?!