Sunday, August 14, 2011

Trying to understand Reflection and Attributes

I am, as the name of this blog implies, trying to understand Reflection and Attributes. I am tinkering around with the app I made here, refined here, and further messed about with here. I decided I wanted to add some polymorphism so that this method doesn't have to have an array of Address passed into it.

public void PrepModel(Person person, Address[] newHomes)

{

   Name = person.Name;

   BirthDay = person.BirthDay.Month.ToString();

   BirthDay = BirthDay + "/" + person.BirthDay.Day;

   BirthDay = BirthDay + "/" + person.BirthDay.Year;

   Age = person.Age.ToString();

   HomeCollection = new Dictionary();

   foreach (Address address in newHomes)

   {

      HomeCollection.Add(address.Id, address.Street);

   }

   Home = person.Home.Id.ToString();

}

 
 

Instead, a method with one parameter can grab Addresses from another source and in this case I'll use reflection to get what I want just to be complicated.

public void PrepModel(Person person)

{

   Name = person.Name;

   BirthDay = person.BirthDay.Month.ToString();

   BirthDay = BirthDay + "/" + person.BirthDay.Day;

   BirthDay = BirthDay + "/" + person.BirthDay.Year;

   Age = person.Age.ToString();

   HomeCollection = new Dictionary();

   AddressAssignmentModel dummyModel = new AddressAssignmentModel();

   dummyModel.trySettingAttribute();

   Type type = typeof(AddressAssignmentModel);

   foreach (MethodInfo method in type.GetMethods())

   {

      object[] attributes = method.GetCustomAttributes(typeof(AddressAttribute), false);

      foreach (Object attribute in attributes)

      {

         AddressAttribute addressAttribute = attribute as AddressAttribute;

         if (null != addressAttribute)

         {

            foreach(Address address in addressAttribute.newHomes)

            {

               HomeCollection.Add(address.Id, address.Street);

            }

         }

      }

   }

   Home = person.Home.Id.ToString();

}

 
 

Alright, AddressAssignmentModel looks like what you see here. I couldn't get my process to work until I put [Address(true)] on a new method and called it instead of using it on my constructor. Maybe constructors are not seen as methods.

using MyApp.Helpers;

namespace MyApp.Models

{

   public class AddressAssignmentModel

   {

      public AddressAssignmentModel()

      {

      }

      

      [Address(true)]

      public void trySettingAttribute()

      {

      }

   }

}

 
 

AddressAttribute looks like this:

using Core;

namespace MyApp.Helpers

{

   using System;

   [AttributeUsage(AttributeTargets.All)]

   public class AddressAttribute : System.Attribute

   {

      public readonly Address[] newHomes;

      

      public AddressAttribute(bool shouldPopulateAddresses)

      {

         if (shouldPopulateAddresses)

         {

            Address austin = new Address();

            austin.Id = new Guid("75b5e18c-13c5-4211-bf8d-9f3d0117d88c");

            austin.Street = "1507 Houston Street #145";

            austin.ZipCode = 78756;

            

            Address haverhill = new Address();

            haverhill.Id = new Guid("bb61cd17-c176-4102-838a-9f3d0117d892");

            haverhill.Street = "5055 Club Road";

            haverhill.ZipCode = 33415;

            

            Address houston = new Address();

            houston.Id = new Guid("31ba86cb-991f-4e71-a2df-9e4000a8b3bc");

            houston.Street = "14795 Memorial Drive";

            houston.ZipCode = 77079;

            

            Address lampasas = new Address();

            lampasas.Id = new Guid("d74e7f2c-ad8d-4522-bb1e-9f3d0117d895");

            lampasas.Street = "1006 East Avenue F";

            lampasas.ZipCode = 76550;

            

            newHomes = new Address[] { austin, haverhill, houston, lampasas };

         } else {

            newHomes = new Address[] { };

         }

      }

   }

}

No comments:

Post a Comment