| MapValueAttribute1.cs
using System;
using NUnit.Framework;
using BLToolkit.Mapping;
namespace HowTo.Mapping
{
[TestFixture]
public class MapValue1
{
public class SourceObject
{
public string Value = "Y";
}
public class TestObject1
{
// The attribute is applied to a field/property.
//
[MapValue(true, "Y")]
[MapValue(false, "N")]
public bool Value;
}
[Test]
public void Test1()
{
SourceObject so = new SourceObject();
TestObject1 to = Map.ObjectToObject<TestObject1>(so);
Assert.AreEqual(true, to.Value);
}
// The attribute is applied to a class.
//
[MapValue(true, "Y")]
[MapValue(false, "N")]
public class TestObject2
{
public bool Value;
}
[Test]
public void Test2()
{
SourceObject so = new SourceObject();
TestObject2 to = Map.ObjectToObject<TestObject2>(so);
Assert.AreEqual(true, to.Value);
}
// The attribute is applied to a base class and affects all child classes.
//
[MapValue(typeof(bool), true, "Y")]
[MapValue(typeof(bool), false, "N")]
public class ObjectBase
{
}
public class TestObject3 : ObjectBase
{
public bool Value;
}
[Test]
public void Test3()
{
SourceObject so = new SourceObject();
TestObject3 to = Map.ObjectToObject<TestObject3>(so);
Assert.AreEqual(true, to.Value);
}
}
} |
MapValueAttribute2.cs
using System;
using NUnit.Framework;
using BLToolkit.Mapping;
namespace HowTo.Mapping
{
[TestFixture]
public class MapValue2
{
public enum Gender1
{
[MapValue("F")] Female,
[MapValue("M")] Male,
[MapValue("U")] Unknown,
[MapValue("O")] Other
}
[Test]
public void Test1()
{
object value = Map.EnumToValue(Gender1.Male);
Assert.AreEqual("M", value);
}
[MapValue(Gender2.Female, 1)]
[MapValue(Gender2.Male, 2)]
[MapValue(Gender2.Unknown, 3)]
[MapValue(Gender2.Other, 4)]
public enum Gender2
{
Female,
Male,
Unknown,
Other
}
[Test]
public void Test2()
{
Gender2 g = Map.ToEnum<Gender2>(2);
Assert.AreEqual(Gender2.Male, g);
}
}
} |
| |