Usman's profileTrue IdentityPhotosBlogLists Tools Help

Creative License

Usman Masood Microsoft Certified Technology Specialist

Usman Masood Microsoft Certified Professional

View Usman Masood's profile on LinkedIn

Add to Technorati Favorites

Usman's Stumbleupon
Blog Flux Directory
The Da Vinci Code My Feudal Lord

True Identity

Infinity and Beyond
May 28

How to get property Name using Expression Tree

recently during some dynamic work in ado.net Entity framework i came across a problem…

suppose here is our class

public class User
{
    #region View Properties
    public long Id { get; set; }
    public String Email { get; set; }
    #endregion
}

 

now an instance of our class is always persisted in a Store… and at some point say when system updates the value of User we need to run some custom validation rules and if a property doesn’t comply we need to revert its value to older one….

public static class Store
{
    public static string GetPreviousValue(this class classobject,propertyname)
    {
        //Do something
    }
}

 

but you want to specify something like (don’t confuse i am using here)

userinstance.Email = this.GetPreviousValue(userinstance.Email);
Since Email is a property name it will return value of Email field but we want the property name not the value how this will be done…..
we don’t want to hard code the property names.. so what next…. ummm Reflection?? again a NO because through reflection we will be able to only iterate properties with there names but we don’t know which property is our required one……
 
public static string GetPropertyName()
{
PropertyInfo[] properties = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
 for (int i = 0; i < properties.Length; i++)
    {
        object propValue = properties[i].GetValue(this, null);
        if (object.ReferenceEquals(propValue,value))     
           {
            return properties[i].Name;
            }
    }
return null;
}
one thing its not a good approach 2ndly it will fail in case if a class has more than one same value…. so this is so dead….
what next well after going through a lot i found some thing very interesting using (as it came up in 3.5)
public class TypeOf<T>
    {
        public static string Property<TProp>(Expression<Func<T, TProp>> expression)
        {
            var body = expression.Body as MemberExpression;
            if (body == null) throw new ArgumentException("'expression' should be a member expression");
            return body.Member.Name;
        }
    }

 

and with this it works like a charm…

var propName = Nameof<SampleClass>.Property(e => e.Email);
userinstance.Email= propName;
Shout it kick it on DotNetKicks.com
May 18

New Version of Ajax Control Toolkit

A new version of Ajax Control toolkit has been released and is now available for download on Codeplex.
This release includes three newer controls:
  • HTML Editor
  • Combo Box (behaves like windows Combo)
  • Color Picker

This release also includes a good amount of fixes and has decreased the need for script files.

The ASP.NET website has been updated with new videos and tutorials for these controls.

kick it on DotNetKicks.com

Shout it

April 04

Microsoft SuperPreview (Rich Testing Experience)

superpreviewlogoMulti Browser testing is always a hard area for Developers as well as Testers in an effort to address this Microsoft this year at mix announced a new feature in there upcoming version of Microsoft Expression Web (which is now available as beta) called Microsoft SuperPreview.

 

superpreview-screen01

SuperPreview allows you to compare how your web page will render in different browsers, the good thing about it is it is also available as a stand alone application and free for download from Microsoft.com

 

The full version of SuperPreview appears in the next release of Microsoft Expression Web and will support IE, Firefox, and Safari. The team is eager to work with other vendors so additional browsers may appear as the product evolves.

kick it on DotNetKicks.com Shout it

February 11

JavaScript/Ajax Tools List (Must have Tools)

i have planned to maintain a list of tools from different sources in order to create a Must Have tools list for me.

this list will have java script related tools, if you guys have any other particular tool in mind do let me know.

Java script Tools

Java Script Frameworks

Technorati Tags:
December 16

Microsoft Live Labs: Seadragon Ajax

I came across an effort from Microsoft Live labs named “” :), its same as Silverlight deep zoom just fact is that Seadragon is an Ajax based zooming and is available for installation for free or even you can use Microsoft Provide hosted application and can provide the embed code in your site or blog

following is an example

 

Technorati Tags: ,

 

kick it on DotNetKicks.com Shout it

 
Photo 1 of 3