|
|
June 07 Being interested in C# 4.0 new features, I played around with c# dynamic lookup. I wrote some dirty code as follows: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Collections;
namespace ConsoleApplication6
{
public class JokerObject : DynamicObject
{
Dictionary<string, object> members = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (members.TryGetValue(binder.Name, out result)) return true;
else return false;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (members.ContainsKey(binder.Name))
{
members[binder.Name] = value;
}
else
{
members.Add(binder.Name, value);
}
return true;
}
public override string ToString()
{
StringBuilder strBuilder = new StringBuilder();
foreach (var item in members)
{
strBuilder.Append(item.Key + " : " + item.Value + Environment.NewLine);
}
return strBuilder.ToString();
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
result = null;
if (binder.CallInfo.ArgumentCount < 1)
{
SaySomething();
}
else
{
ShowArgs(binder,args);
}
return true;
}
private void ShowArgs(InvokeMemberBinder binder, object[] args)
{
Console.WriteLine("Your method name: " + binder.Name);
Console.WriteLine("Your method arguments: ");
foreach (var item in args)
{
Console.WriteLine("\t" + item);
}
}
private void SaySomething()
{
Console.WriteLine("Hello!");
}
}
class Program
{
static void Main(string[] args)
{
dynamic joker = new JokerObject();
joker.Name = "Nyi Nyi";
joker.Age = 18;
Console.WriteLine(joker.ToString());
joker.A();
joker.M(1,2,"abc");
Console.ReadLine();
}
}
}
JokerObject class inherits from DynamicObject in order to get dynamic behavior at runtime. Then an instance “joker ” of JokerObject class is instantiated using dynamic keyword. This new keyword allows you to declare an object whose method calls will be resolved at runtime. You may notice that i don’t define the properties Name, Age and the methods A(),M() in JokerObject class. Calling those members will be resolved at runtime. If it cannot be resolved, you will get RuntimeBinderException.
When you run the above code , the output will be:
Name : Nyi Nyi Age : 18
Hello! Your method name: M Your method arguments: 1 2 abc June 05 While playing around with VS 2010, I came across a new linq operator - Zip. It can merge two sequences with the help of the specified predicate function. The signature of the Zip extension method is as follow: public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector);
Here's the sample code. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
var ageRecord = new[] { 31, 28, 27 ,33};
var friends = new[] { "Elvin", "Pranalan", "Peijiun" ,"Paresh"};
var myOpinions = new[] { "Guru", "A Tiger", "Energetic", "Sweet" };
var zipQuery = ageRecord.Zip(friends, (age, friend) => friend + " is " + age + " years old.")
.Zip(myOpinions, (result1, myOpinion) => result1 + " I think he's " + myOpinion + ".");
zipQuery.ToList().ForEach(result => Console.WriteLine(result));
}
}
}Here’s the output:
Elvin is 31 years old. I think he's Cool. Pranalan is 28 years old. I think he's A Tiger. Peijiun is 27 years old. I think he's Energetic. Paresh is 33 years old. I think he's Sweet. February 08 These days I have been playing around with WPF. In this post I will talk about one of my test projects which converts XML file to FlowDocument. Download: Project Source Code This is the main window of the project.
When you click on Parse Button , the following story.xml (you can also see this file in the project source code) will be converted to WPF FlowDocument. <?xml version="1.0" encoding="UTF-8" ?> <story columnwidth="200" columngap="10" background="white"> <head> <title></title> </head>
<body> <body.head> <hedline> <hl1 fontfamily="georgia" fontcolor="black" fontsize="24" fontstyle="normal" fontweight="bold" >Dec 2008: Free air access between Asean capitals</hl1> <hl2 fontfamily="georgia" fontcolor="gray" fontsize="14" fontstyle="normal" fontweight="bold" >Transport ministers take big step forward.Grouping will also work towards full liberalisation by 2010</hl2> </hedline> <byline> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="normal" >Karamjit Kaur</p> <p fontfamily="georgia" fontcolor="black" fontsize="11" fontstyle="italic" fontweight="normal" >Aviation Correspondent</p> <p fontfamily="georgia" fontcolor="black" fontsize="11" fontstyle="normal" fontweight="normal" >Singapore</p> </byline> </body.head>
<body.content> <block class="textfigure"> <textfigure background="yellow" width="2Column" horizontalalignment="left" verticalalignment="center"> <p fontfamily="georgia" fontcolor="black" fontsize="14" fontstyle="italic" fontweight="bold" textalignment="left"></p> <p fontfamily="georgia" fontcolor="black" fontsize="14" fontstyle="italic" fontweight="bold" textalignment="left"></p> </textfigure> </block>
<block class="text"> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="normal">The decision – which also applies to cargo services – is significant, said industry watchers yesterday.</p> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="normal">Although the December 2008 date had been set four years ago, there was no certainty that all members of the 10-nation grouping would agree to free up air travel by then, they said.</p> <p>Mr Anthony Concil, a spokes man for the International Air Transport Association, a 240-member airline grouping told The Straits Times last night “Clearly, this is a big and con crete step in the right direc tion...What we are seeing is tha Asean has picked up the bal and is moving forward.”</p> <p>Yesterday, the grouping went a step further, affirming its commitment to freer skies by saying it was working towards full liberalisation by 2015.</p> <p>This means airlines from member countries will be able to criss-cross the region without restriction.</p> <p>So Singapore Airlines, for ex- ample, can pick up passengers in Kuala Lumpur and then fly to Bangkok or any other Asean destination.</p> <p>The announcement came at the end of the 13th Asean Trans- port Ministers Meeting.</p> <p>By 2015, the 10 member countries – Singapore, Malay- sia, Brunei, Thailand, Cambo- dia, Indonesia, Laos, Myanmar, the Philippines and Vietnam – also hope to have in place a Sin- gle Aviation Market, which could potentially also remove restrictions on foreign owner- ship of airlines.</p> <p>The decisions taken yester- day have major implications.</p> <p>Although Singapore carriers already have unrestricted ac- cess to points in Thailand and Brunei, many other key routes, such as Singapore-Kuala Lumpur and flights to Vietnam and other points in the fast-growing Indochina market, remain tightly regulated.</p> <p>There is also an existing ban on low-cost carriers flying to key destinations in Indonesia such as Jakarta and Bali.</p> <p>Prime Minister Lee Hsien Loong set the tone for the meet- ing when he opened it earlier in the day and urged the transport ministers to push ahead with plans to progressively establish Asean-wide open skies.</p> <p>He said: “We have to follow through quickly on this, and meet the first milestone of free- ing access between Asean capi- tals by December 2008 on time.”</p> <p>Noting that member coun- tries were at different stages of economic development and may therefore have different priorities, Mr Lee said the more developed Asean members “ought to take the lead in set- ting the pace of integration by moving ahead first”, either bilat- erally or as a smaller sub-group, and put in place market- and consumer-friendly liberalisation measures, such as open skies agreements.</p> <p>He said: “Such bold steps will put Asean in a strong posi- tion to benefit from the rapid growth in air travel around the region. It will also show the world that Asean can take deci- sive action to transform its plans into concrete outcomes.”</p> <p>An efficient and well-inte- grated transport system will ac- celerate Asean’s economic de- velopment and enhance the group’s competitiveness, Mr Lee said, adding that the quality of transport infrastructure is one key factor that decides where businesses are located and that, in turn, influences the flow of investments and talent.</p> </block> </body.content> <body.end> <block class="endline"> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="bold" >karam@sph.com.sg</p> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="bold" >ADDITIONAL REPORTING </p> <p fontfamily="georgia" fontcolor="black" fontsize="12" fontstyle="normal" fontweight="bold" >BY GOH CHIN LIAN</p> <p>SEE ALSO HOME H2</p> </block> </body.end> </body> </story>
If you click on Save Button, the FlowDocument object will be serialized into c:\story.xaml file.
If you find my code helpful to you or if you wanna point out something, please write a comment. :)
Have fun with WPF!
December 18 Let's dive into XAML world. Let's start writing a piece of declarative programming code. First and foremost, .NET Framework 3.5 should be installed in your machine. Let's use the great tool XamlHack. After installing XamlHack, open it.
Type something like the following.
Switch to Design Tab. You will see the result of your markup.
Let's modify some attributes of TextBlock and see the result. <TextBlock FontSize="20" Foreground="Red" FontWeight="Bold" FontStyle="Italic" Background="DarkBlue" Height="20" Width="200" TextAlignment="Center">Hello World!</TextBlock> There are other tools like XamlHack. Here are the links. Have fun with XAML.
December 15 Right now I am working in a project where I am using .NET 3.5 and WPF. I am very interested in WPF. I think I should record my WPF experiences. So I decided to write a blog. First of all, I am rather poor in English. If you come across my blog and find any mistakes in my English, kindly correct me. Thanks.
|