Archive for the ‘.Net’ Category.
11th December 2007, 03:34 pm
Here is the summary of new language features as mentioned on the “What’s new on ORCAS
LINQ is all in both of the languages and indeed this is the main feature for .NET Framework 3.5. Writing any type of queries is the purpose of LINQ at the end. Considering the abilities of LINQ, everything was possible before as well. LINQ makes us to get rid of the strings (the red coloured stuff) from the program in order to minimise the typo errors, easy to read programs by syntax highlighting. But all the best is that gives the ability to write declarative and functional style programs.
Beside the new language features, as a compiler improvement, it is very surprising that C# still doesn’t have background compilation. There is background syntax checking but no compilation. I believe this is a definite need for C# because it is really helpful. For instance the F# projects do background compilation and syntax checks, that way it easy to investigate “silly errors” while coding. Also this was one of the powerful features that I found on eclipse while working on a Java project.
Visual basic has that feature moreover it has also automatic syntax fixing as well. Likewise if you call a method with lower case letters it is automatically converted to the actual method on the next line. Actually in a type inferenced language this is needed, because it is not easy to recognise the type information of all the members.
Anyway I just wrote a quick macro to give the feeling of background compilation for C#. It is not really sophisticated but it works. Just put it into EnvironmentEvent macro in Visual Stuio.
Dim lastbuilt As DateTime
Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean) Handles TextDocumentKeyPressEvents.AfterKeyPress
Dim doc = DTE.ActiveDocument
Dim diff = DateTime.Now.Subtract(lastbuilt)
If Not Char.IsLetterOrDigit(Keypress(0)) And diff.Seconds > 5 Then
DTE.ExecuteCommand("Build.BuildSelection")
lastbuilt = DateTime.Now
doc.Activate()
End If
End Sub
XML in Language
All of the best is that now XML is a first class citizen in VB. I wouldn’t expect this as a serious feature but after trials it makes extremely relevant to use XML in Visual Basic. You get syntax highlighting and even intellisense for XML if the namespaces are specified and even more.
Having XML literals in the language, it makes really sense to use XLinq with VB.
Like consider the xml stored by messenger. You could just assign to a variable just like that.
Dim msn = <?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="1">
<Message Date="25/03/2007" Time="22:35:47" DateTime="2007-03-25T21:35:47.173Z" SessionID="1">
<From><User FriendlyName="koko"/></From>
<To><User FriendlyName="opopop"/></To>
<Text Style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">151515</Text>
</Message>
<Message Date="25/03/2007" Time="22:35:55" DateTime="2007-03-25T21:35:55.344Z" SessionID="1">
<From><User FriendlyName="koko"/></From>
<To><User FriendlyName="opopop"/></To>
<Text Style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">5959959</Text></Message>
</Log>
It will have the type of System.Xml.Linq.XDocument.
Let’s define the XML Stylesheet :
Dim xslt = <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Log">
<html>
<head>
<title> Message Log for </title>
</head>
<body style='margin:0'>
<table border='1'>
<tr>
<td> From </td>
<td> To </td>
<td> Message </td>
</tr>
<xsl:for-each select="/Log/Message">
<tr>
<td><xsl:value-of select="From/User/@FriendlyName"/></td>
<td><xsl:value-of select="To/User/@FriendlyName"/></td>
<td><xsl:value-of select="Text"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If we want to do an XSLT transformation to that snippet, it is even easier than it used to be.
Dim xTransform = New System.Xml.Xsl.XslCompiledTransform()
xTransform.Load(xslt.CreateReader())
xTransform.Transform(msn.CreateReader(), New System.Xml.XmlTextWriter("test.html", New System.Text.UnicodeEncoding()))
I think working with xml data using Visual Basic should be considered as a manipulation language. Since we are all becoming multilingual this shouldn’t be a problem.
Tags:
cs,
csharp,
linq,
macro,
vb,
xlinq,
XML,
xsl,
xslt Category:
.Net,
Articles,
C#,
Visual Basic,
XML,
linq |
Comment
10th December 2007, 04:31 pm
JSON (JavaScript Object Notation) is a lightweight data-interchange format. JSON is an object notation in text format that Javacript engine fully understands and there is no need to parse for it.
The library contains some basic types that Javascript requires. IJSONObject, JSONElement, JSONNumber<T>, JSONString, JSONBoolean, JSONCollection, JSONArray and JSONObject.
- JSONObject : Represents JSON objects beginning and ending with curly braces ({}). It is a collection of JSONElement objects. Object members consist if string and values separated by colon in JSON notation.
- JSONArray : Represents a javascript array that begin and end with braces and contain values. In the notation values are separated by commas, but the library does all the syntactic issues.
- JSONCollection : A base type for JSONObject and JSONArray.
- JSONElement : Base class for all JSON types. It has the base properties and can be used with arrays or objects or individually.
- JSONString : Represents JavaScript string type and created by double quotes. It is inherited from a JSONElement
- JSONBoolean : Represents JavaScript boolean type
- JSONNumber<T> : Represents a javascript number type, it can be integer, or floating point numbers.
JSON Tools for .NET is the library that helps to build JSON objects from .NET Framework objects. I just wanted to share the code that I was working previously. It is not a complete library. There is a need for some helper classes for working with dynamic objects and collections. JSONReflector might be useful as a new feature.
You can download the JSON Tools for .NET and play with it. Also, you are very welcome to join and contribute to the project.
A simple example of JSON output looks like this:
{
"Total" : 45500,
"Offset" : 0,
"Result_List" : [
{ "Description" : "JSON (JavaScript Object Notation) (Pronounced like Jason, IPA /d?e?s?n/ ) is a lightweight computer data interchange format. It is a text-based, human-readable format for ... ",
"Title" : "JSON - Wikipedia, the free encyclopedia",
"URL" : "http://en.wikipedia.org/wiki/Json" } ,
{ "Description" : "object {} { members } members pair pair , members pair string : value array [] [ elements ] elements value value , elements value string number object array true false null",
"Title" : "JSON",
"URL" : "http://www.json.org/" } ,
] }
This example is the one of the outputs from the sample directory. It is actually a live search query displayed in JSON string.
In C#, each object is contracted with an IJSONObject interface. That interface has a single WriteObject Method that does the JavScript representation of the actual object.
Here is a sample implementation for a result type object.
public JSONObject WriteObject()
{
JSONNumber<int> jTotal = new JSONNumber<int>("Total", m_Total);
JSONNumber<int> jOffset = new JSONNumber<int>("Offset", m_Offset);
List<JSONObject> ResultObjects = new List<JSONObject>(m_Results.Count);
foreach (SearchResult r in m_Results)
{
ResultObjects.Add(r.WriteObject());
}
JSONArray jResults = new JSONArray("Result_List", ResultObjects);
JSONObject js = new JSONObject("LiveResults", jTotal, jOffset, jResults);
return js;
}
Another representation of a result by using different JSON types.
public JSONObject WriteObject()
{
JSONString jDescription = new JSONString("Description", m_Description);
JSONString jTitle = new JSONString("Title", m_Title);
JSONString jURL= new JSONString("URL", m_URL);
JSONObject js = new JSONObject("Result", jDescription, jTitle, jURL);
return js;
}
I liked the way it creates JSON object out of an object. No reflection, no dynamic code emit, which means it should have no problems for busy heavy servers. On the other hand a JSONReflector and a JSONParser could be implemented using the same class library.
7th December 2007, 07:00 pm
It has been a long time since I haven’t blogged. Lots of things have happened on my side and on the development world. After finishing the internship at Microsoft Research Cambridge and submitting my thesis, I started to work at European Product Development Center in Dublin. I am very excited to work at Microsoft again.
I started a new project and used Visual Studio 2008 for that. I say used because the project is prematurely ended. That’s why I find time to write actually. Anyway that’s another story. From now on, I will try to blog more often about F#, C# 3.0, Parallel FX Library and Silverlight.
Here are the developer news :). There were so many exciting things happened.
First F# is getting productized. I want to congratulate for the achievement and looking forward for the next updates. It is great news for the developer and research world. I am really happy for F# being a “product”. What does that mean as a developer point of view is that, the language will not die and it will be more stable and developped more quickly. A functional language getting to the main stream will involve lots of new development and research ideas. The team is now focusing on stability and feature fixing and I am really looking for the future releases.
Moreover, Visual Studio 2008 is out there. At first it looks very similar in terms of user interface. But I have to admit that it’s lot better and faster. Here are the quick impressions. I like the window list when navigating between the tabs with Ctrl Tab with a small preview window. I liked the transparency of the code complete display. when you press Ctrl it becomes transparent so that it is possible to read the code underneath, you don’t have to navigate away. Of course beside the visual enhancements, the language extensions such as LINQ, DLINQ, XLINQ, collection initialisers and functional constructs in C# and VB (lambda expressions, anonymous types), new designers and new classes (pipes..) in the framework are really welcome. More classes are now supporting the generic types and most of nongeneric types became abbreviated.I hope the same goes for all of the typed collection types as well. Such as XMLNodeList etc :). Typed is something good..
Finally the project management system of Visual Studio 2008 allows us to target to lower frameworks as well. So there is no need to hack visual studio to compile on other frameworks. This is also possible because there is no update on the CLR, on the other hand it is not possible to target 1.1 because of the CLR change but who wants to do that anymore ?
Happy coding…
4th October 2007, 07:27 pm
What a nice morning after everything… It has been more than a week since I finished my coursework which means that I unofficially finished my masters. Things are started to change now, I really didn’t understand how this month has passed and couldn’t believe it’s just finished. Well the truth is I enjoyed every single moment of and very happy about it. Something is going to happen soon, I’ll tell you later but fingers crossed for the moment…
Back to business, the source code of the .NET Framework will be released under MS-RL license. Moreover debugging from Visual Studio, looking to the source locally will be possible. What an announcement! I just couldn’t believe it. Since the BCL Team insisted on the comments, I really wonder about the comments, as well as the code.
Have you ever wanted to look inside some of the BCL methods? Have you ever wondered about some exception thrown by our code? Are you curious to see what comments BCL developers write in their code?
Well yes to all those questions. This is a big step for the developer community. Previously Rotor is available as a research license but we weren’t sure it was the real .NET framework also it wasn’t usable from Visual Studio. Although the current license will allow only as a reference the code rather than using, I assume there will be some further movements to be more open, we’ll see. Also now you could see the news on Slashdot and digg so everybody is talking about it which is absolutely perfect
12th September 2007, 08:29 pm
Although I am extremely busy this month, I can’t stop reading those wonderful articles about parallelism on October’s MSDN Magazine. Here are the articles, a very first gentle introduction to PLINQ (Parallel LINQ) by Joe Duffy,Ed Essey, Parallel Performance using System.Concurrency namespace and a ThreadPool article. Apparently in the near future we will have some more concurrency dlls.
Going back to work, enjoy!
23rd August 2007, 04:44 pm
Creating a class from a string might be crucial for some very dynamic projects. It’s a single line of code that I wanted to share but I think it has a lot of power.
Simply get the executing assembly and call the GetType method. If your assembly is one of the linked assembly or even a dynamically loaded assembly, you might need to call GetReferencedAssemblies() method as well.
namespace reflectme
{
using System;
public class hello
{
public hello()
{
Console.WriteLine("hello");
Console.ReadLine();
}
static void Main(string[] args)
{
Type t = System.Reflection.Assembly.GetExecutingAssembly().GetType("reflectme.hello");
t.GetConstructor(System.Type.EmptyTypes).Invoke(null);
}
}
}
Bear in mind it will be extremely slow. Don’t do that