Skip to content

Instantly share code, notes, and snippets.

@jmarnold
Forked from anonymous/2.md
Last active December 20, 2015 02:58
Show Gist options
  • Save jmarnold/6059836 to your computer and use it in GitHub Desktop.
Save jmarnold/6059836 to your computer and use it in GitHub Desktop.
Reflection Kata 2

Reflection Kata 2

  • Level: Beginner
  • Target time: 5 minutes

Setup

Begin by creating a new Console application in Visual Studio called: ReflectionKata2. You will need to create two files:

  1. ReflectionTarget.cs
  2. ReflectionTests.cs

Simply copy the contents of all files (including Program.cs) from their respective companions supplied in this gist.

Beginning form

Replace the body of all methods found in the ReflectionTests class so that the methods perform the instructions described by their name.

Final form

Replace the body of the invoke_public_methods_on_ReflectionTests_that_have_no_parameters() method found in the Program class. Once complete, run the application.

public class ReflectionTarget
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public override string ToString()
{
return string.Format("Name: {0} {1}; Email: {2}", FirstName, LastName, Email);
}
protected bool Equals(ReflectionTarget other)
{
return string.Equals(FirstName, other.FirstName) && string.Equals(LastName, other.LastName) && string.Equals(Email, other.Email);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((ReflectionTarget) obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = (FirstName != null ? FirstName.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (LastName != null ? LastName.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (Email != null ? Email.GetHashCode() : 0);
return hashCode;
}
}
}
public class ReflectionTests
{
public void print_the_object_types()
{
print_object_type_name("Hello, World");
print_object_type_name(new ReflectionTarget());
}
public void are_property_infos_equal()
{
// var name1 = (find the FirstName property from ReflectionTarget)
// var name2 = (find the FirstName property from ReflectionTarget)
// print_equality_result_of_properties(name1, name2)
throw new NotImplementedException();
}
public void object_mapping()
{
var target = new ReflectionTarget
{
FirstName = "Joel",
LastName = "Arnold",
Email = "joel@arnold.com"
};
var clonedObject = use_reflection_to_clone_the_object(target);
Console.WriteLine(target.Equals(clonedObject)); // should be true
}
public void print_object_type_name(object obj)
{
throw new NotImplementedException();
}
private void print_equality_result_of_properties(PropertyInfo property1, PropertyInfo propert2)
{
Console.WriteLine(property1.Equals(propert2));
}
private object use_reflection_to_clone_the_object(object input)
{
// map the properties, don't memberwise clone
throw new NotImplementedException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment