Archive for January 2007

Microsoft AJAX 1.0 RTM

It has been more than a year since Atlas is out. I remember playing with the first hands-on-labs for doing a search query. I also really liked the Ajax Control Toolkit since the first day, still using two controls in a project. Now that baby is out of CTP, BETA and ASP.NET AJAX 1.0 is released. A lot has changed since it’s first release. The objects in javascript library, the method names, object names and the product name have changed. As a result, we have 2 separate products. ASP.NET 2.0 AJAX Extensions 1.0 (Client and Server), ASP.NET AJAX Control Toolkit . There is one more product which is ASP.NET AJAX Futures January CTP. This is the additional javascript library to provide additional functionality.

Interestingly, it has been shipped with source code.  There is no escape for the client source code release but they also include the server side controls which is good. Although we don’t have the server side controls code, Scott Guthrie announced the server side controls source code will  be released shortly. Client side is released with Microsoft Permissive License (Ms-PL) and server side with Microsoft Reference License (Ms-RL) The client side is more flexible in terms of license to extend the library.  

Thanks to the team…

Google Wide Search Box for Firefox Stylish

Sometimes Google’s search box is not enough as a size. So I decided to add a simple CSS class to the textbox to display wider and clearer.To add some css style sheets to sites in Firefox, there is a powerful addon called stylish. So a simple stylish css code can provide that change. As a result Google search box becomes wider and with font colour red. It is optimized for  wide screens width sized 1280.

Before
before.png


After
after.png

You can install it from UserStyles.org or just copy the code to your stylish add-on. You need to have either GreaseMonkey or Stylish add-ons installed on Firefox

@namespace url(http://www.w3.org/1999/xhtml);
/*Coded by Can Erten */
@-moz-document url-prefix(http://www.google.) 
{	
	[name="q"] {width:925px; font-size:14pt!important;
			 color:red!important; font-weight:bold!important }
}

Vista Developer Day at Microsoft UK

I just surprised when I see an event from Microsoft about Windows Vista. I was expecting it, but not before the real release of Vista. Anyway, it is a bit late for telling that, but Vista Developer Launch is this weekend at Microsoft Campus in Reading.

This is a 2 day event 19th and 20th January. 19th January will be some sessions about Windows Vista or Office 2007. 20th January will be practice. So they invite us to code in a huge room which sounds really cool. They also claim that they have 1000 of Windows Vista copies.

On Friday, I have to work so I will not join the first day. However if I can wake up, I really want to go on Saturday for coding with people :). If you like to join me, just contact me… I was invited for a dinner on Friday night and I just couldn’t make it..

WebDD - conference in Reading for web developers and web designers - Registration is Open

WebDD , a free confence in Reading for web developers and web designers, registration is open. It is organized by community and hosted by Microsoft. It is on Saturday 3rd February 2007 at Microsoft Reading, UK. Scott Guthrie will be there. You can find the speakers list and sessions.

There will be also a Geek Dinner like previous DDD events. The registration for the dinner is wiki style as previously. I will be there as well.

See you there.

Reflection for Properties and Fields with DynamicMethod

Lastly I posted about the coolest exception while working with System.Reflection.Dynamicmethod class. I found the solution for my bug, and now it works like a charm. There are two classes for accessing properties or fields, one with the generic implementations and one with object implementation. Using the reflection with dynamicmethod reflection we can get 80% more performance compared to traditional reflection. 

Be careful if you are about to use classes that have generic classes as properties. There are some problems with it.

Property that might cause problems of a class

 public class aField<T>
	{
		private T m_Value;
 
 
        public override string ToString()
        {
            return Value.ToString();
        }
 public interface IEntity
    {
         aField<string> CREUSER
        {
            get;
            set;
        }

.

Code for object

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
 
namespace CE.Reflection
{
    public class DynamicReflectionHelper
    {
        public delegate object GetPropertyFieldDelegate(object obj);
        public static GetPropertyFieldDelegate GetPropertyorField(PropertyInfo pi, FieldInfo fi)
        {
            if (pi != null && fi != null)
                throw new NotSupportedException("one of the parameters should be null");
 
            if (pi != null || fi != null)
            {
                string methodName = string.Empty;
                if (pi != null)
                    methodName = pi.Name;
                else
                    methodName = fi.Name;
 
                Module mod = null;
                if (pi != null)
                    mod = pi.Module;
                else
                    mod = fi.Module;
 
 
                DynamicMethod dm = new DynamicMethod("GetPropertyorField_" + methodName, typeof(object), new Type[] { typeof(object) }, mod, true);
                ILGenerator il = dm.GetILGenerator();
 
                il.Emit(OpCodes.Ldarg_0);
                if (pi != null)
                {
                    il.EmitCall(OpCodes.Callvirt, pi.GetGetMethod(), null);
                    if (pi.PropertyType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, pi.PropertyType);
                    }
                }
                else if (fi != null)
                {
                    il.Emit(OpCodes.Ldfld, fi);
                    if (fi.FieldType.IsValueType)
                    {
                        il.Emit(OpCodes.Box, fi.FieldType);
                    }
                }
 
 
                il.Emit(OpCodes.Ret);
 
                return (GetPropertyFieldDelegate)dm.CreateDelegate(typeof(GetPropertyFieldDelegate));
 
            }
            else
                throw new NullReferenceException("no field or property");
        }
        public static GetPropertyFieldDelegate GetPropertyorField(object o, string memberName)
        {
            Type v = o.GetType();
            PropertyInfo pi = v.GetProperty(memberName);
            FieldInfo fi = v.GetField(memberName);
 
            return GetPropertyorField(pi, fi);
 
        }
 
        public delegate void SetPropertyFieldDelegate(object obj, object m_Value);
 
        public static SetPropertyFieldDelegate SetProperyorField(object o, string memberName)
        {
            Type v = o.GetType();
            PropertyInfo pi = v.GetProperty(memberName);
            FieldInfo fi = v.GetField(memberName);
 
 
            return SetProperyorField(pi, fi);
 
        }
 
        public static SetPropertyFieldDelegate SetProperyorField(PropertyInfo pi, FieldInfo fi)
        {
            if (pi != null && fi != null)
                throw new NotSupportedException("one of the parameters should be null");
            if (pi != null || fi != null)
            {
                string methodName = string.Empty;
                if (pi != null)
                    methodName = pi.Name;
                else
                    methodName = fi.Name;
 
                DynamicMethod dm = new DynamicMethod("SetPropertyorField_" + methodName, typeof(void),
                    new Type[] { typeof(object), typeof(object) }, pi.Module, true);
                ILGenerator il = dm.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
 
 
                if (pi != null)
                {
                    il.EmitCall(OpCodes.Callvirt, pi.GetSetMethod(true), null);
 
                    if (pi.PropertyType.IsValueType)
                    {
                        il.Emit(OpCodes.Unbox_Any, pi.PropertyType);
                    }
                }
                else
                {
                    il.Emit(OpCodes.Stfld, fi);
                    if (pi.PropertyType.IsValueType)
                    {
                        il.Emit(OpCodes.Unbox_Any, pi.PropertyType);
                    }
                }
 
 
                il.Emit(OpCodes.Ret);
 
                return (SetPropertyFieldDelegate)dm.CreateDelegate(typeof(SetPropertyFieldDelegate));
            }
            else
            {
                throw new NullReferenceException("no field or property");
            }
        }
    }
}

Code for generic object

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
 
namespace CE.Reflection
{
    public class DynamicReflectionHelperforObject<V>
    {
        public delegate T GetPropertyFieldDelegate<T>(V obj);
 
 
        public static GetPropertyFieldDelegate<C> GetP<C>(string memberName)
        {
            Type v = typeof(V);
            PropertyInfo pi = v.GetProperty(memberName);
            FieldInfo fi = v.GetField(memberName);
 
            if (pi != null || fi != null)
            {
                DynamicMethod dm = new DynamicMethod("GetPropertyorField_" + memberName, typeof(C), new Type[] { v }, v.Module);
                ILGenerator il = dm.GetILGenerator();
 
                il.Emit(OpCodes.Ldarg_0); // loaded c, c is the return value
                if (pi != null)
                    il.EmitCall(OpCodes.Call, pi.GetGetMethod(), null);
                else if (fi != null)
                    il.Emit(OpCodes.Ldfld, fi);
                il.Emit(OpCodes.Ret);
 
                return (GetPropertyFieldDelegate<C>)dm.CreateDelegate(typeof(GetPropertyFieldDelegate<C>));
 
            }
            else
                throw new NullReferenceException("No Property or Field");
        }
 
        public delegate void SetPropertyFieldDelegate<T>(V obj, T m_Value);
 
        public static SetPropertyFieldDelegate<C> SetP<C>(string memberName, C mValue)
        {
            Type v = typeof(V);
            PropertyInfo pi = v.GetProperty(memberName);
            FieldInfo fi = v.GetField(memberName);
            if (pi != null || fi != null)
            {
                DynamicMethod dm = new DynamicMethod("SetPropertyorField_" + memberName, typeof(void),
                    new Type[] { v, typeof(C) }, v.Module);
                ILGenerator il = dm.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldarg_1);
                if (pi != null)
                    il.EmitCall(OpCodes.Callvirt, pi.GetSetMethod(), new Type[] { typeof(C) });
                else if (fi != null)
                    il.Emit(OpCodes.Stfld, fi);
                il.Emit(OpCodes.Ret);
 
                return (SetPropertyFieldDelegate<C>)dm.CreateDelegate(typeof(SetPropertyFieldDelegate<C>));
            }
            else
                throw new NullReferenceException("No Property or Field");
        }
    }
}

This project has two main classes, one is generic for working with a specified type, and the other is normal object class in which you need to deal with conversion after using it.