Archive for category Programming

Peeking into Android with Eclipse

just started to take a peek into Google Android… let’s see what it has got..
Advertisements

Leave a comment

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

1 Comment

Microsoft’s New Enhancements in WCF,WF code name Dublin :)

today while going through my daily blog reading routine, I came across Microsoft Code Name Dublin in Stephen Forte’s Blog.
A major set of enhancements in WCF and its hosting. also developers will be able to develop Rest services via WCF…. also the enhancement will provide ability to develop applications entirely in XAML, from the presentation layer (WPF/SilverLight) to the workflow (WF also supports XAML in Dublin) to the actual WCF services.
The rest base starter kit is going to be released on Codeplex soon.
 
and the CTP for Dublin will be at PDC.
 
more details can be found at his blog


 

kick it on DotNetKicks.com

Share it :

Leave a comment

Creating instance of object dynamically

Yesterday while refactoring some code written by another developer I came across one thing at a lot of places one method was being used again and again

   1:  Public ClassName GetObject()
   2:  {
   3:  if object == null
   4:  create object
   5:  return object
   6:  }

That was essential logic but why rewrite…. there should be a generic way to accomplish this …

I came up with the idea of using Activator class in dot net…. which provides ability to create instances of objects dynamically.

here was my initial code

   1:   public static object CreateObject(Object obj)
   2:   {
   3:     if (obj == null)
   4:     {
   5:     return
Activator.CreateInstance(obj.GetType(),obj);
   6:     }
   7:    return obj;
   8:  }

but during execution I got following error “can’t get type of null object”, The problem was dot net does not allows to get type of null objects..

what now…. well! no worries at this stage generics comes handy by using generics it was just a piece of cake

Final Code:

   1:  public static T CreateObject<T>(T obj)
   2:  {
   3:  if (obj == null)
   4:  {
   5:  return Activator.CreateInstance<T>();
   6:  }
   7:  return obj; 
   8:  }
 
but what if the object passed, its instance creation 
requires parameterized constructor…
program will throw an exception at runtime in such 
cases… Why not prevent this during code writing

1:public static T CreateObject<T>(T obj)
where T : new()

2:  {
3:  if (obj == null)
4:  {
5:  return Activator.CreateInstance<T>();
6:  }
7:  return obj; 
8:  }
by adding new we have forced that the passed type 
should implement a parameter less constructor.

kick it on DotNetKicks.com Shout it

Leave a comment

Honey Pot Captcha Technique

while surfing along my RSS feeds today i read the Honey Pot Captcha Technique its so simple and easy.

First, what is a Captcha?  It is a challenge – response test to help determine that you are actually a human.  You’ve undoubted already came across a few of these during a sign up process of some text with a grid or a line through it asking what it says.

Haack came to the conclusion that most bots don’t do two things, parse CSS and execute JavaScript.  Haack’s solution adds in a field in a hidden div tag that a spam bot would fill then checks it server side during the validation of the submission.  He even wrote a validation control which is on CodePlex.

Leave a comment

14 Tips to Speed Up your web pages.

Marcelo Calbucci has posted very good article on how to speed up Web Pages renderign on Browsers.

Leave a comment

Mac Address in Visual Basic.Net

posting these sort of tips here is actually for future reference.

Dim mc As System.Management.ManagementClass
Dim mo As ManagementObject
mc = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
For Each mo In moc
If mo.Item("IPEnabled") = True Then
ListBox1.Items.Add("MAC address " & mo.Item("MacAddress").ToString())
End If
Next

actual Article: http://dotnetjunkies.com/WebLog/jkirwan/

Leave a comment